您的位置:首页 > 其它

SRM 551 div2

2012-08-12 16:30 369 查看
错过了。。。当练习做的。

250pt 略

500pt

怎么开始就没敢写暴力呢。。。。看别人的思路,挺巧妙的。Orz

class ColorfulChocolates {
public:
int maximumSpread(string c, int m) {
int l = c.length(), i, j, num, cnt;
int s[3000];
int sum, ans = 0, res;

REP(i, l) {
num = 0; cnt = 0;
for(j = i - 1; j >= 0; --j) {
if(c[i] == c[j])    s[cnt++] = num;
else    num++;
}
num = 0;
for(j = i + 1; j < l; ++j) {
if(c[i] == c[j])    s[cnt++] = num;
else    num++;
}
sort(s, s + cnt);
sum = 0; res = 1;
for(j = 0; j < cnt; ++j) {
if(sum + s[j] > m)    break;
sum += s[j];
res++;
}
ans = max(ans, res);
}
return ans;
}
};


950pt (对我来说是好题。。。)

挺好的dp。可是偶不会。。。根本不敢写。一个五维的状态 dp[A的个数][B的个数][C的个数][左端点(AorBorC)][右端点(AorBorC)] ;

然后一个很巧妙的记忆化搜索。

solve(pos, A, B, C, head, tail)  //表示用掉pos个字符后,A的当前个数,B的当前个数,C的当前个数,串头的值,串尾的值。

class ColorfulCupcakesDivTwo {
public:
LL dp

[3][3];
int n;

LL dfs(int pos, int a, int b, int c, int f, int l) {
if(a < 0 || b < 0 || c < 0)    return 0;
if(pos == n)    return f != l;
if(dp[a][b][c][f][l] != -1)    return dp[a][b][c][f][l];

LL res = 0;

if(l == 0) {
res = (res + dfs(pos + 1, a, b-1, c, 1, f))%mod;
res = (res + dfs(pos + 1, a, b, c-1, 2, f))%mod;
}
if(l == 1) {
res = (res + dfs(pos + 1, a-1, b, c, 0, f))%mod;
res = (res + dfs(pos + 1, a, b, c-1, 2, f))%mod;
}
if(l == 2) {
res = (res + dfs(pos + 1, a-1, b, c, 0, f))%mod;
res = (res + dfs(pos + 1, a, b-1, c, 1, f))%mod;
}
return dp[a][b][c][f][l] = res;
}
int countArrangements(string c) {
int i, a[4] = {0};
n = c.size();
REP(i, n)    a[c[i]-'A']++;
int ans = 0;
CL(dp, 0XFF);
ans = (ans + dfs(1, a[0] - 1, a[1], a[2], 0, 0))%mod;
ans = (ans + dfs(1, a[0], a[1] - 1, a[2], 1, 1))%mod;
ans = (ans + dfs(1, a[0], a[1], a[2] - 1, 2, 2))%mod;
return ans;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: