博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SRM 394(1-250pt)
阅读量:7239 次
发布时间:2019-06-29

本文共 4114 字,大约阅读时间需要 13 分钟。

DIV1 250pt

题意:给定一个字符串s('a'-'z'),计其中出现次数最多和最少的字母分别出现c1次和c2次,若在s中去掉最多k个字母,求去掉以后c1 - c2的最小值。

解法:做题的时候,想到了用dfs暴力枚举,然后TLE了。然后想到了枚举c2,求c1的最小值,最后写了比较麻烦的代码,过了。然后看了题解才发现,枚举c1和c2。。。。。

   真的是看到'a' - 'z'就应该想到这种方法。。。。

tag:think, brute-force

1 // BEGIN CUT HERE  2 /*  3  * Author:  plum rain  4  * score :  5  */  6 /*  7   8  */  9 // END CUT HERE 10 #line 11 "RoughStrings.cpp" 11 #include 
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 #include
22 #include
23 #include
24 #include
25 #include
26 #include
27 #include
28 #include
29 #include
30 #include
31 #include
32 #include
33 #include
34 35 using namespace std; 36 37 #define clr0(x) memset(x, 0, sizeof(x)) 38 #define clr1(x) memset(x, -1, sizeof(x)) 39 #define pb push_back 40 #define mp make_pair 41 #define sz(v) ((int)(v).size()) 42 #define all(t) t.begin(),t.end() 43 #define zero(x) (((x)>0?(x):-(x))
VI; 50 typedef vector
VS; 51 typedef vector
VD; 52 typedef pair
pii; 53 typedef long long int64; 54 55 const double eps = 1e-8; 56 const double PI = atan(1.0)*4; 57 const int inf = 2139062143 / 2; 58 59 int num[500], n2[500], n1[500]; 60 int idx[500], all; 61 pii an[100]; 62 63 int gao(int k) 64 { 65 clr0 (n2); 66 for (int i = 0; i < 26; ++ i) 67 ++ n2[num[i]]; 68 int pos = 50; 69 while (!n2[pos]) -- pos; 70 while (k > 0){ 71 if (pos < 0) return 0; 72 k -= n2[pos]; 73 n2[pos-1] += n2[pos]; 74 n2[pos--] = 0; 75 } 76 if (!k) return pos; 77 return pos + 1; 78 } 79 80 class RoughStrings 81 { 82 public: 83 int minRoughness(string s, int k){ 84 clr0 (num); 85 for (int i = 0; i < sz(s); ++ i) 86 ++ num[s[i] - 'a']; 87 all = 0; 88 for (int i = 0; i < 26; ++ i) 89 if (num[i]) n1[all++] = num[i]; 90 if (all == 1) return 0; 91 sort(n1, n1+all); 92 int ans = max(gao(k) - n1[0], 0), use = 0; 93 for (int i = 0; i < all; ++ i){ 94 use += n1[i]; 95 if (k < use) break; 96 if (i == all-1) ans = 0; 97 else ans = min(ans, max(gao(k-use) - n1[i+1], 0)); 98 } 99 return ans;100 }101 102 // BEGIN CUT HERE103 public:104 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }105 private:106 template
string print_array(const vector
&V) { ostringstream os; os << "{ "; for (typename vector
::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }107 void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }108 void test_case_0() { string Arg0 = "aaaaabbc"; int Arg1 = 1; int Arg2 = 3; verify_case(0, Arg2, minRoughness(Arg0, Arg1)); }109 void test_case_1() { string Arg0 = "aaaabbbbc"; int Arg1 = 5; int Arg2 = 0; verify_case(1, Arg2, minRoughness(Arg0, Arg1)); }110 void test_case_2() { string Arg0 = "veryeviltestcase"; int Arg1 = 1; int Arg2 = 2; verify_case(2, Arg2, minRoughness(Arg0, Arg1)); }111 void test_case_3() { string Arg0 = "gggggggooooooodddddddllllllluuuuuuuccckkk"; int Arg1 = 5; int Arg2 = 3; verify_case(3, Arg2, minRoughness(Arg0, Arg1)); }112 void test_case_4() { string Arg0 = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; int Arg1 = 17; int Arg2 = 0; verify_case(4, Arg2, minRoughness(Arg0, Arg1)); }113 void test_case_5() { string Arg0 = "bbbccca"; int Arg1 = 2; int Arg2 = 0; verify_case(5, Arg2, minRoughness(Arg0, Arg1)); }114 115 // END CUT HERE116 117 };118 119 // BEGIN CUT HERE120 int main()121 {122 // freopen( "a.out" , "w" , stdout ); 123 RoughStrings ___test;124 ___test.run_test(-1);125 return 0;126 }127 // END CUT HERE
View Code

 网上看到的代码:

转载于:https://www.cnblogs.com/plumrain/p/srm_394.html

你可能感兴趣的文章
ASP.NET Web API 全局权限和异常处理
查看>>
从头来之【iOS及历史版本特性介绍】
查看>>
C语言——指针
查看>>
JobControl 的实现原理
查看>>
【leetcode】Rotate Image(middle)
查看>>
Android之listview运用(美团美食列表)
查看>>
【转】Mybatis/Ibatis,数据库操作的返回值
查看>>
iOS UITextView 输入内容实时更新cell的高度
查看>>
SQL删除重复数据方法
查看>>
Sql Server函数全解<一>字符串函数
查看>>
2015年第9本:别让猴子跳回背上
查看>>
动态设置表格[GridView]在编辑时 只读。
查看>>
ExtJS4 自己定义基于配置的高级查询1
查看>>
C#调用杀毒软件MSE扫描指定目录或文件
查看>>
SQL中declare申明变量
查看>>
datatable 的ajax修改参数,post可以传参处理
查看>>
远程控制编写之屏幕传输 MFC实现 屏幕截图 发送bmp数据 显示bmp图像
查看>>
公有云与私有云的差别(转)
查看>>
Facebook为什么使用PHP编程语言?
查看>>
PHP图片上传程序(完整版)
查看>>