codeforces 335A Banana(贪心)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
分析:贪心,先取一遍所有的字母,每次优先去当前所需最多的块数的字母。
1 //##################### 2 //Author:fraud 3 //Blog: http://www.cnblogs.com/fraud/ 4 //##################### 5 #include <iostream> 6 #include <sstream> 7 #include <ios> 8 #include <iomanip> 9 #include <functional> 10 #include <algorithm> 11 #include <vector> 12 #include <string> 13 #include <list> 14 #include <queue> 15 #include <deque> 16 #include <stack> 17 #include <set> 18 #include <map> 19 #include <cstdio> 20 #include <cstdlib> 21 #include <cmath> 22 #include <cstring> 23 #include <climits> 24 #include <cctype> 25 using namespace std; 26 #define XINF INT_MAX 27 #define INF 0x3FFFFFFF 28 #define MP(X,Y) make_pair(X,Y) 29 #define PB(X) push_back(X) 30 #define REP(X,N) for(int X=0;X<N;X++) 31 #define REP2(X,L,R) for(int X=L;X<=R;X++) 32 #define DEP(X,R,L) for(int X=R;X>=L;X--) 33 #define CLR(A,X) memset(A,X,sizeof(A)) 34 #define IT iterator 35 typedef long long ll; 36 typedef pair<int,int> PII; 37 typedef vector<PII> VII; 38 typedef vector<int> VI; 39 40 int num[1010]; 41 char str[1010]; 42 struct node{ 43 int num,t; 44 char a; 45 node(int _num,char _a){ 46 num=_num,a=_a,t=1; 47 } 48 friend bool operator<(node y,node x){ 49 return (y.num+y.t-1)/y.t < ((x.num+x.t-1)/x.t); 50 } 51 }; 52 int main() 53 { 54 ios::sync_with_stdio(false); 55 string s; 56 int n; 57 cin>>s; 58 cin>>n; 59 int len=s.length(); 60 int tot=0; 61 for(int i=0;i<len;i++)num[s[i]]++; 62 priority_queue<node> q; 63 int now=0; 64 for(int i=0;i<1010;i++){ 65 if(num[i]){ 66 str[now++]=i; 67 q.push(node(num[i],i)); 68 } 69 } 70 if(now>n){ 71 cout<<-1<<endl; 72 return 0; 73 } 74 for(;now<n;){ 75 node p=q.top(); 76 q.pop(); 77 str[now++]=p.a; 78 p.t++; 79 q.push(p); 80 } 81 node p=q.top(); 82 int ans=(p.num+p.t-1)/p.t; 83 cout<<ans<<endl; 84 cout<<str<<endl; 85 86 return 0; 87 }