Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 999 Accepted Submission(s): 378
#include<iostream> #include<queue> #include<cstdio> #include<cstring> using namespace std; priority_queue<int,vector<int>,greater<int> > q; char str[100010]; int huf[26]; int main(){ //freopen("input.txt","r",stdin); int t,n; scanf("%d",&t); while(t--){ memset(huf,0,sizeof(huf)); while(!q.empty()) q.pop(); scanf("%d",&n); scanf("%s",str); for(int i=0;str[i]!='\0';i++) huf[str[i]-'a']++; for(int i=0;i<26;i++) if(huf[i]) q.push(huf[i]); if(q.size()==1){ if(q.top()>n) printf("no\n"); else printf("yes\n"); continue; } int ans=0,a,b; while(q.size()!=1){ a=q.top();q.pop(); b=q.top();q.pop(); q.push(a+b); ans+=(a+b); } if(ans<=n) printf("yes\n"); else printf("no\n"); } return 0; }