luogu1090合并果子

https://www.luogu.com.cn/problem/P1090

优先队列

小根堆

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

大根堆

priority_queue <int > q;

 

#include<bits/stdc++.h>
using namespace std;

//priority_queue <int > q; 这是大根堆 
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
    int n;
    cin>>n;
    int temp;
    int a1,a2,a3;
    int ans=0;
    for(int i=0;i<n;i++){
        scanf("%d",&temp);
        q.push(temp);
    }
    while(q.size()>1){
        a1=q.top();
        q.pop();
        a2=q.top();
        q.pop();
        a3=a1+a2;
        q.push(a3);
        ans+=a3;
    }
    cout<<ans;
     
    
    return 0;
 } 

 

posted on 2020-03-05 13:04  lyyyt  阅读(118)  评论(0编辑  收藏  举报