STL优先队列

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;

priority_queue<ll, vector<ll>, greater<ll> > a;
									//greater<ll>表示从大到小排序,形成小根堆
									//less<ll>表示从小到大排序,形成大根堆
int main(){
    long long ans=0, n, t;
    scanf("%lld", &n);
    for(int i=1;i<=n;i++)
	{
        scanf("%lld", &t);
        a.push(t);
    }
    for(int i=1; i<n; i++){
        int c,d;
        c=a.top();
        a.pop();
        d=a.top();
        a.pop();					//每次取最小的两个数
        ans+=c+d;					
		a.push(c+d);
	}								//本题几乎等同于“合并果子” 
    printf("%lld",ans);
    return 0;
}
posted @ 2019-03-04 12:05  LFYZOI题解  阅读(133)  评论(0编辑  收藏  举报