题解 P1090 【合并果子】

· · 题解

我用优先队列写的;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> > str;//优先队列里面的比较方式可以自己手写,在这里我直接调用了;
int n,Ans,tot,num;
void init(){
    int x;
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> x;
        str.push(x);
    }
}
int main(){
    init();
    while(str.empty() == false)
    {
        tot += str.top();
        str.pop(); num++;
        if(num % 2 == 0)
        {
            Ans += tot;
            str.push(tot);
            tot = 0;
        }
    }
    cout << Ans;
    return 0;
}