NYOJ 55 (优先队列)

View Code
 1 #include<iostream>
2 #include<cstdio>
3 #include<queue>
4 using namespace std;
5 priority_queue <int ,vector<int>,greater<int> > q;
6 int main()
7 {
8 int t,n,x,y,sum;
9 int i;
10 scanf("%d",&t);
11 while(t--)
12 {
13 while(!q.empty())q.pop();
14 sum=0;
15 scanf("%d",&n);
16 for(i=0;i<n;++i)
17 {
18 scanf("%d",&x);
19 q.push(x);
20 }
21 while(!q.empty())
22 {
23 x=q.top();//注意是q.top()
24 q.pop();
25 if(q.empty())break;
26 y=q.top();
27 q.pop();
28 sum+=x+y;
29 q.push(x+y);
30 }
31 printf("%d\n",sum);
32 }
33 system("pause");
34 return 0;
35 }

 

你可能感兴趣的:(优先队列)