zoukankan      html  css  js  c++  java
  • CodeForces

    和紫书上的Blocks UVA - 10559 几乎是同一道题,只不过是得分计算不同

    不过看了半天紫书上的题才会的,当时理解不够深刻啊

    不过这是一道很好区间DP题

    细节看代码

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define endl "
    "
    #define maxn 100+5
    int n;
    char s[maxn];
    long long a[maxn];
    long long f[maxn][maxn][maxn];
    long long f1[maxn];
    bool vis[maxn][maxn][maxn];
    long long DP(int l,int r,int k)
    {
        if(l>r) return 0;
        if(vis[l][r][k]) return f[l][r][k];
        vis[l][r][k]=1;
        long long& ans=f[l][r][k];
        int cnt=0;
        int i=r;
        while(i>=l&&s[i]==s[r]) i--,cnt++;
        i++;
        ans=DP(l,i-1,0)+f1[cnt+k];//直接消除
        for(int j=l;j<i;j++){
            if(s[j]==s[r]&&s[j]!=s[j+1]) {
                ans=max(ans,DP(l,j,cnt+k)+DP(j+1,i-1,0));
            }
        }
        return ans;
    }
    main()
    {
        scanf("%d",&n);
        scanf("%s",s+1);
        for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
        for(int i=1;i<=n;i++){
            f1[i]=a[i];
            for(int j=0;j<=i;j++){
                f1[i]=max(f1[i],f1[j]+f1[i-j]);
            }
    
        }
        cout<<DP(1,n,0);
    }
  • 相关阅读:
    MFC绘图基础
    MFC绘图基础
    MFC坐标问题
    利用Graphziv帮助理解复杂的类层次关系
    priority_quenue
    1060. Are They Equal (25)
    1057. Stack (30)
    1056. Mice and Rice (25)
    1053. Path of Equal Weight (30)
    1051. Pop Sequence (25)
  • 原文地址:https://www.cnblogs.com/033000-/p/10459500.html
Copyright © 2011-2022 走看看