Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这个应该不用构造哈夫曼树,每次找两个最小的就行

Posted by ly50247 at 2010-05-31 16:14:30 on Problem 3253
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstdio>
#include <climits>
#include <queue>
#include <map>
#include <list>
#include <vector>
using namespace std;

struct cmp
{
	bool operator()(long long a, long long b)
	{
		return a > b;
	}
};

int main()
{
	priority_queue<long long, vector<long long>, cmp> pq;
	int n; 
	scanf("%d", &n);
	while (n--)
	{
		int t;
		scanf("%d", &t);
		pq.push(t);
	}
	long long sum = 0;

	while (pq.size() != 1)
	{
		long long a = pq.top();
		pq.pop();
		long long b = pq.top();
		pq.pop();
		sum += a + b;
		pq.push(a+b);
	}
	printf("%lld\n", sum);

	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator