TYVJ 1066 合并果子【优先队列】

题意:给出n堆果子,需要将n堆果子合并成一堆,问将所有堆的果子合成一堆所需要花费的最少的力气

因为要使耗费力气最小,即需要每次搬动的那堆重量小,所以可以选取两堆最轻的合并,合并之后再插入还没有合并的堆中,重复这个过程

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath> 
 5 #include<stack>
 6 #include<vector>
 7 #include<map> 
 8 #include<queue> 
 9 #include<algorithm>  
10 #define mod=1e9+7;
11 using namespace std;
12 
13 typedef long long LL;
14 
15 int main(){
16     priority_queue<int,vector<int>,greater<int> > pq;
17     int n,a,ans=0;
18     scanf("%d",&n);
19     for(int i=1;i<=n;i++){
20         scanf("%d",&a);
21         pq.push(a);
22     }
23     
24     while(pq.size()>1){
25         int x=pq.top();pq.pop();
26         int y=pq.top();pq.pop();
27         ans+=x+y;
28         pq.push(x+y);
29     }
30     printf("%d\n",ans);
31     return 0;
32 }
View Code

 

 

 

话说手写二叉堆还是木有写出来,还是用的优先队列写的,在RQNOJ上交的,AC100是过了的意思么= =

posted @ 2015-03-21 14:15  sequenceaa  阅读(199)  评论(0编辑  收藏  举报