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 shi546886446 at 2014-02-09 20:25:51 on Problem 3253
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
#define ll long long
using namespace std;

int main()
{
	int n,i,a;
	priority_queue<int, vector<int>, greater<int> > q;
	while(scanf("%d",&n)!=EOF)
	{
		while(!q.empty())
			q.pop();
		if(n==1)
		{
			scanf("%d",&a);
			printf("0\n");
		}
		else
		{
			for(i=0;i<n;i++)
			{
				scanf("%d",&a);
				q.push(a);
			}
			ll sum=0;
			while(1)
			{
				int num=q.top();
				q.pop();
				num+=q.top();
				q.pop();
				sum+=num;
				if(!q.empty())
					q.push(num);
				else
					break;
			}
			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