TYVJ 1066 合并果子【优先队列】
2015-03-21 14:15
281 查看
题意:给出n堆果子,需要将n堆果子合并成一堆,问将所有堆的果子合成一堆所需要花费的最少的力气
因为要使耗费力气最小,即需要每次搬动的那堆重量小,所以可以选取两堆最轻的合并,合并之后再插入还没有合并的堆中,重复这个过程
View Code
话说手写二叉堆还是木有写出来,还是用的优先队列写的,在RQNOJ上交的,AC100是过了的意思么= =
因为要使耗费力气最小,即需要每次搬动的那堆重量小,所以可以选取两堆最轻的合并,合并之后再插入还没有合并的堆中,重复这个过程
#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是过了的意思么= =
相关文章推荐
- STL 优先队列-- 树-堆结构练习——合并果子之哈夫曼树
- Vijos P1097 合并果子【哈夫曼树+优先队列】
- 合并果子,优先队列的使用
- 优先队列——合并果子
- 合并果子优先队列做法
- 合并果子_tyvj1066_vijos1097_codevs1063_贪心+堆
- Tyvj 1066 合并果子(贪心)
- 优先队列 合并果子通俗易懂
- 【vijos】【优先队列】合并果子
- NOIP提高组2004 合并果子(优先队列排序)
- C++优先队列 合并果子
- 1270 药不能停(合并果子,优先队列,堆)
- Vijos P1097 合并果子(优先队列 贪心)
- C++优先队列 合并果子
- 合并果子【哈夫曼树+优先队列】
- Tyvj 1066 合并果子
- 【算法学习笔记】79.STL 优先队列 模拟法 SJTU OJ 4012 合并果子
- 合并果子(优先队列 +或者+哈夫曼)
- tyvj 1066 合并果子(优先队列)
- TYVJ 1066 合并果子 解题报告