您的位置:首页 > Web前端

hdu 2527 Safe Or Unsafe

2015-04-22 20:30 218 查看
优先队列,注意队列开始时就只有一个数的特殊情况

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: