您的位置:首页 > 其它

【算法学习笔记】79.STL 优先队列 模拟法 SJTU OJ 4012 合并果子

2015-07-22 15:08 309 查看
优先队列自动解决了动态排序问题,非常好用。。。

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int n;
int seeds[10000];
int presum[10000];
priority_queue<int,vector<int>,greater<int> > q;

void init(){

cin>>n;
for (int i = 0; i < n; ++i){
int t;
cin>>t;
q.push(t);
}
}

int build(){
//看错题意结果傻逼了...应该模拟法 数据结构用优先队列....
int ans = 0;
while(q.size()>=2){
int a = q.top(); q.pop();
int b = q.top(); q.pop();
q.push(a+b);
ans += a+b;
}
return ans;
}

int main(int argc, char const *argv[])
{
init();
cout<<build()<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: