SRM 551 div2

错过了。。。当练习做的。

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[N][N][N][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;
    }
};

 

 

 

 

 

 

posted @ 2012-08-12 16:30  AC_Von  阅读(204)  评论(0编辑  收藏  举报