您的位置:首页 > 其它

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

2015-03-21 14:15 281 查看
题意:给出n堆果子,需要将n堆果子合并成一堆,问将所有堆的果子合成一堆所需要花费的最少的力气

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
using namespace std;

typedef long long LL;

int main(){
priority_queue<int,vector<int>,greater<int> > pq;
int n,a,ans=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a);
pq.push(a);
}

while(pq.size()>1){
int x=pq.top();pq.pop();
int y=pq.top();pq.pop();
ans+=x+y;
pq.push(x+y);
}
printf("%d\n",ans);
return 0;
}


View Code

话说手写二叉堆还是木有写出来,还是用的优先队列写的,在RQNOJ上交的,AC100是过了的意思么= =
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: