您的位置:首页 > 其它

聪明的木匠 取最小

2017-09-13 20:51 127 查看
题目:https://cn.vjudge.net/contest/178387#problem/G

可以利用优先队列,每次取最小的两个值。

#include <iostream>
#include <queue>
using namespace std;
int main() {
priority_queue<int, vector<int>, greater<int> > q;
int n, tmp;
while (cin >> n) {
for (int i = 0; i<n; i++) {
cin >> tmp;
q.push(tmp);
}
int ans = 0;
while (!q.empty()) {
int x = q.top();
q.pop();
if (q.empty()) break;
int y = q.top();
q.pop();
q.push(x + y);
ans += (x + y);
}
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  贪心