免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
(チキン料理間違ったタイトル)羅区間違ったタイトルのレコード
、私は他の誰かのコピーしたコード!侵略は(* /ω\ *)を削除
我々はSTLを持っています!!!
プライオリティキューでのSTL:PRIORITY_QUEUE
定義:
priority_queue<int>q;
小規模から大規模まで:
priority_queue<int,vector<int>,greater<int> >q;
あなたが降順にしたい場合は、演算子をオーバーロードすることができます。
struct Node{
int x,y;
Node(int a=0, int b=0):
x(a), y(b) {}
};
struct cmp{
bool operator()(Node a, Node b){
if(a.x == b.x) return a.y>b.y;
return a.x>b.x;
}
};
priority_queue<Node,vector<Node>,cmp>q;
それとも、レを使用することができます
)■降順
priority_queue<int,vector<int>,less<int> >q;
この最後の質問は、水の言葉です。
#include<bits/stdc++.h>
using namespace std;
int n,x,ans;
priority_queue<int,vector<int>,greater<int> >q;
int main(){
cin>>n;
for(int i=1;i<=n;i++) cin>>x,q.push(x);
while(q.size()>=2){
int a=q.top(); q.pop();
int b=q.top(); q.pop();
ans+=a+b;
q.push(a+b);
}
cout<<ans<<endl;
return 0;
}