hdu 2527 Safe Or Unsafe
水题一枚,轻松通过。
代码如下:
View Code
1 #pragma comment(linker, "/STACK:102400000,102400000") 2 3 #include <cstdio> 4 #include <cstring> 5 #include <cstdlib> 6 #include <vector> 7 #include <algorithm> 8 #include <set> 9 #include <iostream> 10 #include <queue> 11 #include <map> 12 13 using namespace std; 14 15 #define REP(i, n) for (int i = 0; i < (n); i++) 16 #define REP_1(i, n) for (int i = 1; i <= (n); i++) 17 #define PB push_back 18 #define MPR make_pair 19 #define SZ(x) ((int) (x).size()) 20 #define FI first 21 #define SE second 22 #define _clr(x) memset(x, 0, sizeof(x)) 23 #define ALL(x) (x).begin(), (x).end() 24 #define PRIQ priority_queue 25 26 typedef vector<int> VI; 27 typedef pair<int, int> PII; 28 typedef vector<PII> VPII; 29 30 const int N = 1e5 + 100; 31 32 PRIQ<PII, VPII, greater<PII> > Q; 33 int cnt[26]; 34 35 int work(char *str) { 36 _clr(cnt); 37 while (*str) cnt[*(str++) - 'a']++; 38 while (!Q.empty()) Q.pop(); 39 REP(i, 26) { 40 if (cnt[i]) Q.push(MPR(cnt[i], 0)); 41 } 42 PII a, b; 43 int fi, se; 44 while (Q.size() > 1) { 45 a = Q.top(); 46 Q.pop(); 47 b = Q.top(); 48 Q.pop(); 49 se = (a.SE ? a.SE : a.FI) + (b.SE ? b.SE : b.FI); 50 fi = a.FI + b.FI + a.SE + b.SE; 51 Q.push(MPR(fi, se)); 52 } 53 return Q.top().FI; 54 } 55 56 char buf[10000]; 57 58 int main() { 59 int T, n; 60 cin >> T; 61 REP(i, T) { 62 cin >> n >> buf; 63 // cout << work(buf) << endl; 64 cout << ((n < work(buf)) ? "no" : "yes") << endl; 65 } 66 return 0; 67 }
——written by Lyon