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

这么多人贴代码。。那我也贴一个吧。。。STL 16ms。。

Posted by ljfljf2006 at 2009-08-09 10:18:02 on Problem 3253
#include <iostream>
#include <queue>
using namespace std;
priority_queue<int,vector<int>,greater<vector<int>::value_type>> for_huff_tree;
int main()
{
	int length,N;
	__int64 ans=0,sum_length=0;
	scanf("%d",&N);
	for(int i=0;i<N;i++)
	{
		scanf("%d",&length);
		for_huff_tree.push(length);
		sum_length+=length;
	}
	int temp1,temp2;
	for(int i=1;i<N;i++)
	{
		temp1=for_huff_tree.top();
		for_huff_tree.pop();
		temp2=for_huff_tree.top();
		for_huff_tree.pop();
		ans+=temp1+temp2;
		for_huff_tree.push(temp1+temp2);
	}
	ans+=for_huff_tree.top();
	printf("%I64d\n",ans-sum_length);
	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