优先队列,也是看了别人的代码才有的思路
#include<iostream> #include<algorithm> #include<queue> #define maxn 100000+5 using namespace std; struct stu { int sum,id; friend bool operator<(stu x,stu y) { return x.sum>y.sum; } }; priority_queue<stu>mapp; int n,m; int rem[maxn]; stu x,y; int main() { int Case=1,t; cin>>t; while(t--) { cin>>n>>m; fill(rem,rem+maxn,100000);//很重要 for(int i=0;i<n;i++) cin>>rem[i]; sort(rem,rem+n); while(mapp.size()) mapp.pop(); cout<<"Case #"<<Case++<<": "; int d=0; x.sum=rem[0]; x.id=0; mapp.push(x); while(mapp.size()) { x=mapp.top(); mapp.pop(); d++; if(d==m){cout<<x.sum<<endl;break;} y.sum=x.sum+rem[x.id+1]; //cout<<y.sum<<" "<<y.id<<endl; y.id=x.id+1; mapp.push(y); y.sum=x.sum-rem[x.id]+rem[x.id+1]; y.id=x.id+1; mapp.push(y); } } return 0; }
时间: 2024-11-02 09:17:37