还一道区间DP -- MZOJ 1346: 不老的传说

http://10.37.2.111/problem.php?id=1346

与上一道染色基本一样,就加了个限制条件(一次最多刷maxd)

#include <bits/stdc++.h>
#define read read()
#define up(i,l,r) for(int i = (l);i <=(r); i++)
using namespace std;
int read
{
    int x = 0; char ch = getchar();
    while(ch < 48 || ch > 57) ch = getchar();
    while(ch >= 48 && ch <= 57) {x = 10 * x + ch - 48; ch = getchar();}
    return x;
}
const int N = 500;
int n,c,maxn,f[N][N],a[N],ans = INT_MAX;
int main()
{
    //freopen("spring.in","r",stdin);
    memset(f,0x3f,sizeof(f));
    n = read; c = read; maxn = read;
    up(i,1,n) a[i] = a[i + n] = read;
    up(i,1,((n<<1) - 1)) f[i][i] = 1;
    up(L,2,n)
        up(i,1,(n<<1) - L)
        {
            int j = i + L - 1;
            if(a[i] == a[j])
            {
                if(j - i < maxn)//一定要加,最多能刷maxn; //-> 不加88(绵中数据太水了) 
                f[i][j] = min(f[i + 1][j],f[i][j - 1]); 
            }
            else up(k,i,j - 1) f[i][j] = min(f[i][j],f[i][k] + f[k + 1][j]);            
        }
    up(i,1,n) ans = min(ans,f[i][i + n - 1]);
    printf("%d",ans);
    return 0;
}

 

posted @ 2019-01-25 18:34  陈星卿  阅读(111)  评论(0编辑  收藏  举报