首页 > 其他 > 详细

hdu 2527 Safe Or Unsafe

时间:2015-04-22 22:17:29      阅读:196      评论:0      收藏:0      [点我收藏+]

优先队列,注意队列开始时就只有一个数的特殊情况

#include<iostream>
#include<queue>
using namespace std;
int m;
int rem[26];
priority_queue<int,vector<int>,greater<int> >mapp;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int m;
		string ans;
		cin>>m>>ans;
		fill(rem,rem+26,0);
		while(mapp.size()) mapp.pop();
		for(int i=0;i<ans.size();i++) rem[ans[i]-'a']++;
		for(int i=0;i<26;i++)
		{
			if(rem[i]) mapp.push(rem[i]);
		}
		int sum=0;
		if(mapp.size()==1) sum=mapp.top();//特殊情况 
		while(mapp.size()>1)
		{
			int x=mapp.top();
			mapp.pop();
			int y=mapp.top();
			mapp.pop();
			sum+=x+y;
			mapp.push(x+y); 
		}
		if(sum<=m) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
} 


hdu 2527 Safe Or Unsafe

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

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