hdu 懒省事的小明
来源:互联网 发布:airserver激活码 mac 编辑:程序博客网 时间:2024/06/06 14:20
很简单的一道优先队列问题,思路如下:
每次合并最小的两堆,并把新堆放入队列中,直到只剩下一堆为止
注意:
1 优先队列让最小的在队首要用greater<>,最大的要用less
2每次从优先队列取出两个值取两个值,因此当只剩下一个值的时候无法再取两个值了
代码如下:
#include<iostream>#include<string>#include<queue>#include<algorithm>#include<set>using namespace std;struct cmp{ bool operator ()(long long &a,long long &b){ return a>b; } }; int main(){int test;cin>>test;while(test--){int n;cin>>n;priority_queue<long long ,vector<long long>,cmp>q;for(int i=1;i<=n;i++){int temp;cin>>temp;q.push(temp);}long long ans=0;while(!q.empty()){int t1,t2;t1=q.top();q.pop();if(q.empty())break;t2=q.top();q.pop();ans+=t1+t2;q.push(t1+t2);//cout<<ans<<" ";}cout<<ans<<endl;}}
0 0
- hdu 懒省事的小明
- 懒省事的小明
- 懒省事的小明
- 懒省事的小明
- 懒省事的小明
- 懒省事的小明
- 懒省事的小明
- 懒省事的小明
- 懒省事的小明
- 懒省事的小明
- 懒省事的小明
- nyoj_55 懒省事的小明
- 优先队列 懒省事的小明
- NYOJ 55 懒省事的小明
- NYOJ 55-懒省事的小明
- nyoj 55 懒省事的小明
- 【55 懒省事的小明 】
- ACM 懒省事的小明
- Java同步集合
- Apache Cordova + Ionic 环境搭建
- CodeForces 448D Multiplication Table(二分,CodeForces,矩阵中找数字)
- andriod sqlite
- php正则总结
- hdu 懒省事的小明
- ubuntu下安装sogou输入法和consolas字体
- 拓扑排序算法
- GF-动态规划dp-多维费用的背包
- oracle数据库日期格式转换及replace用法
- python 画折线图
- 关于try,finally里面的return,到底谁先执行的问题
- 倒计时-
- Eclipse LogCat相关 (1.Unexpected value from nativeGetEnabledTags: 0 2.LogCat日志不出现 3.建立日志分类快速查询标签)