P1090 合并果子

题目传送门

总结:
1、哈夫曼编码模板题

2、使用了STL中的优先队列
小根堆

priority_queue<int, vector<int>, greater<int> > q;

默认大根堆

priority_queue<int, vector<int>> q;

可多知识可以参考:https://www.cnblogs.com/zwfymqz/p/7800654.html

#include <bits/stdc++.h>

using namespace std;
int n, x, ans;
priority_queue<int, vector<int>, greater<int> > q;

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> x, q.push(x);
    while (q.size() >= 2) {
        int a = q.top();
        q.pop();
        int b = q.top();
        q.pop();
        ans += a + b;
        q.push(a + b);
    }
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/littlehb/p/15034753.html