P1090 合并果子

题面:https://www.luogu.org/problemnew/show/P1090

先把初始果子放进优先队列维护,再将合并后的果子也扔进堆即可。

Code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<queue>
using namespace std;
const int N=10005;
long long n,i,a[N],ans,x,y;
priority_queue<int,vector<int>,greater<int> > q;
int main(){
    scanf("%lld",&n);
    for(i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        q.push(a[i]);
    }
    for(i=1;i<=n-1;i++){
        x=q.top(),q.pop();
        y=q.top(),q.pop();
        q.push(x+y);
        ans+=x+y;
    }
    cout<<ans<<endl;
    return 0;
}
posted @ 2019-07-16 13:42  prestige  阅读(132)  评论(0编辑  收藏  举报