其他分享
首页 > 其他分享> > P1090 [NOIP2004 提高组] 合并果子

P1090 [NOIP2004 提高组] 合并果子

作者:互联网

知识点:贪心,霍夫曼编码,优先队列

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;

int main() {
    int n;
    while (cin >> n && n) {
        priority_queue<int, vi, greater<int> > q;
        for (int i = 0; i < n; i++) { int x; cin >> x; q.push(x); }
        ll ans = 0;
        for (int i = 1; i < n; i++) {
            int a = q.top(); q.pop();
            int b = q.top(); q.pop();
            q.push(a + b);
            ans += a + b;
        }
        cout << ans << endl;
    }
    return 0;
}

标签:NOIP2004,typedef,果子,int,pop,P1090,ans,push,define
来源: https://blog.csdn.net/qq_33942309/article/details/122760456