SRM 394(1-250pt)
DIV1 250pt
题意:给定一个字符串s('a'-'z'),计其中出现次数最多和最少的字母分别出现c1次和c2次,若在s中去掉最多k个字母,求去掉以后c1 - c2的最小值。
解法:做题的时候,想到了用dfs暴力枚举,然后TLE了。然后想到了枚举c2,求c1的最小值,最后写了比较麻烦的代码,过了。然后看了题解才发现,枚举c1和c2。。。。。
真的是看到'a' - 'z'就应该想到这种方法。。。。
tag:think, brute-force
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "RoughStrings.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define clr0(x) memset(x, 0, sizeof(x))
#define clr1(x) memset(x, -1, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz(v) ((int)(v).size())
#define all(t) t.begin(),t.end()
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<a<<" "
#define tst1(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef pair<int, int> pii;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int inf = / ; int num[], n2[], n1[];
int idx[], all;
pii an[]; int gao(int k)
{
clr0 (n2);
for (int i = ; i < ; ++ i)
++ n2[num[i]];
int pos = ;
while (!n2[pos]) -- pos;
while (k > ){
if (pos < ) return ;
k -= n2[pos];
n2[pos-] += n2[pos];
n2[pos--] = ;
}
if (!k) return pos;
return pos + ;
} class RoughStrings
{
public:
int minRoughness(string s, int k){
clr0 (num);
for (int i = ; i < sz(s); ++ i)
++ num[s[i] - 'a'];
all = ;
for (int i = ; i < ; ++ i)
if (num[i]) n1[all++] = num[i];
if (all == ) return ;
sort(n1, n1+all);
int ans = max(gao(k) - n1[], ), use = ;
for (int i = ; i < all; ++ i){
use += n1[i];
if (k < use) break;
if (i == all-) ans = ;
else ans = min(ans, max(gao(k-use) - n1[i+], ));
}
return ans;
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); if ((Case == -) || (Case == )) test_case_5(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
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; } }
void test_case_0() { string Arg0 = "aaaaabbc"; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, minRoughness(Arg0, Arg1)); }
void test_case_1() { string Arg0 = "aaaabbbbc"; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, minRoughness(Arg0, Arg1)); }
void test_case_2() { string Arg0 = "veryeviltestcase"; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, minRoughness(Arg0, Arg1)); }
void test_case_3() { string Arg0 = "gggggggooooooodddddddllllllluuuuuuuccckkk"; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, minRoughness(Arg0, Arg1)); }
void test_case_4() { string Arg0 = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, minRoughness(Arg0, Arg1)); }
void test_case_5() { string Arg0 = "bbbccca"; int Arg1 = ; int Arg2 = ; verify_case(, Arg2, minRoughness(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
RoughStrings ___test;
___test.run_test(-);
return ;
}
// END CUT HERE
网上看到的代码:

SRM 394(1-250pt)的更多相关文章
- SRM593(1-250pt,500pt)
SRM 593 DIV1 250pt 题意:有如下图所示的平面,每个六边形有坐标.将其中一些六边形染色,要求有边相邻的两个六边形不能染同一种颜色.给定哪些六边形需要染色,问最少需要多少种颜色. 解法: ...
- SRM475 - SRM479(1-250pt,500pt)
SRM 475 DIV1 300pt 题意:玩游戏.给一个棋盘,它有1×n(1行n列,每列标号分别为0,1,2..n-1)的格子,每个格子里面可以放一个棋子,并且给定一个只含三个字母WBR,长度为n的 ...
- SRM468 - SRM469(1-250pt, 500pt)
SRM 468 DIV1 250pt 题意:给出字典,按照一定要求进行查找. 解法:模拟题,暴力即可. tag:water score: 0.... 这是第一次AC的代码: /* * Author: ...
- SRM470 - SRM474(1-250pt,500pt)(471-500pt为最短路,474-500pt未做)
SRM 470 DIV1 250pt 题意:有n个房间排成一排,相邻两个房间之间有一扇关闭着的门(共n-1扇),每个门上都标有‘A’-‘P’的大写字母.给定一个数n,表示第n个房间.有两个人John和 ...
- SRM 609(1-250pt, 1-500pt)
嗯....还是应该坚持写题解的好习惯啊... DIV1 250pt 这难度是回到srm 300+的250了嘛...略 // BEGIN CUT HERE /* * Author: plum rain ...
- SRM 442(1-250pt, 1-500pt)
DIV1 250pt 题意:将一个数表示成质因子相乘的形式,若乘式所含数字的个数为质数,则称A为underprime.比如12 = 2*2*3,则含3个数字,是underprime.求A, B之间un ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- SRM 513 2 1000CutTheNumbers(状态压缩)
SRM 513 2 1000CutTheNumbers Problem Statement Manao has a board filled with digits represented as St ...
- SRM 510 2 250TheAlmostLuckyNumbersDivTwo(数位dp)
SRM 510 2 250TheAlmostLuckyNumbersDivTwo Problem Statement John and Brus believe that the digits 4 a ...
随机推荐
- 关键词:CodeSmith工具、Money类型、__UNKNOWN__
问题描述: 当数据库列类型有Money类型的时候,CodeSmith生成数据访问层会出错.有不能识别的类型.解决方法: 通过查找资料得知,数据库中的Money类型在DbType中是Currency(货 ...
- 安卓工程修改包名后 Failed to find provider info for...问题
安卓工程修改包名后 Failed to find provider info for com.android...provider问题 原因: 1. 多处含包名文件须同时更改 2. Manifest里 ...
- WinForm中的事件触发机制学习
在一个Form窗体中拖个按钮,双击后系统自动生成代码: private void button1_Click(object sender, EventArgs e) { } 同时在窗体的Initial ...
- 【原创】ZeroClipboard的时代或许已经过去了
曾经,一个网页上要用Javascript实现网页内容的复制,其实是很麻烦的一件事情.虽然在这个问题上IE有其高大上的 window.clipboardData 方法支持直接复制指定内容,Firefox ...
- 在Xcode4中给程序提供命令行参数(转)
网上xcode4的资料实在是不多,再加上xcode4相对3的改动还那么大,并且还只有英文版.我为了这个问题头痛了很久.后来终于找到了...方法如下 xcode菜单的Product->EditSc ...
- APP被Rejected 的各种原因翻译(转)
原文:http://www.cnblogs.com/sell/archive/2013/02/16/2913341.html Terms and conditions(法律与条款) 1.1 As a ...
- Extjs4 关于设置form中所有子控件为readOnly属性的解决方案
之前在网上找了一堆,但那些确实没法用,后来考虑了一下,发现主要是网上提供的假设form中只有一层控件,没有考虑到布局稍微复杂的form情形,此处采用递归的形式实现对form中所有控件(grid及but ...
- php之购物车类思路及代码
<?php /* 购物车类 1.整站范围内,购物车--全局有效 解决:把购物车的信息,放在session里 2.既然全局有效,购物车的实例只有一个 解决:单例模式 技术选型:session+单例 ...
- workerman需要的php模块posix、pcntl、sysvshm、sysvmsg缺少,怎么办
如果您的php是源码编译,那么请进到php的源码目录,再进入ext目录下,分别找到相应的php模块目录,进行编译 1. 假设php目录为/usr/local/php, 进到相应的php模块目录,执行 ...
- input里面的查找标记 ő
<i id="J_SearchIcon" class="iconfont">ő</i> .iconfont {ont-family: i ...