首页 > 其他 > 详细

hdu 4546 比赛难度

时间:2015-04-22 13:51:09      阅读:163      评论:0      收藏:0      [点我收藏+]

优先队列,也是看了别人的代码才有的思路

#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;
}


hdu 4546 比赛难度

原文:http://blog.csdn.net/zafkiel_nightmare/article/details/45193979

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!