2019.7.18 P1090合并果子(优先队列)

it2022-05-05  114

合并果子

用优先队列一直取出最小的就好

#include<bits/stdc++.h> using namespace std; priority_queue<int,vector<int>,greater<int> >p; int main(){ int n,t,ans=0; cin>>n; for(int i=0;i<n;i++){ cin>>t; p.push(t); } int f=0,cnt=1; while(!p.empty()){ if(cnt&1){ f=p.top(); p.pop(); }else { f+=p.top(); p.pop(); ans+=f; p.push(f); } cnt++; } cout<<ans; return 0; }

明天再写树的差分。。。 今天水了


最新回复(0)