hdu 2527 Safe Or Unsafe

本文提供了一种简洁高效的算法解决方案,用于解决字符计数问题,并通过堆排序优化结果输出。适用于编程竞赛或日常算法练习。

Problem - 2527

  水题一枚,轻松通过。

代码如下:

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

转载于:https://www.cnblogs.com/LyonLys/archive/2013/03/22/hdu_2527_Lyon.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值