优先队列

一:模板题与解释

1.仅有定义即可完成优先队列(题面

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    priority_queue<int,vector<int>,greater<int> >pq;
    int n,t;
    cin>>n;
    for(int i=0;i<n;++i){
        cin>>t;
        pq.push(t);
    }
    ll ans=0;
    while(pq.size()>=2){
        int a,b;
        a=pq.top(),pq.pop();
        b=pq.top(),pq.pop();
        ans+=a+b;
        pq.push(a+b);
    }
    cout<<ans<<endl;
    return 0;
}

2.优先队列具有多种定义方法,目前还不会在结构体内定义优先队列的operatior操作:推荐

posted @ 2020-01-28 16:02  waryan  阅读(129)  评论(0)    收藏  举报