码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-21 15:24:33      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

题意:给出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是过了的意思么= =

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

标签:

原文地址:http://www.cnblogs.com/wuyuewoniu/p/4355547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!