P4158 [SCOI2009]粉刷匠

/*
    序列DP
    对于每一行 f[i][j]=max(f[k][j-1]+max(蓝 红,[k,i])
    对于每一行 dp[i][j],i表示行号,
        dp[i][j]=max(dp[i][j-k]+f[m][k]);
    答案 dp[n][T]; 
*/
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;

const int A = 1e7+10;
const int B = 1e6+10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
  for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
  return x * f;
}

int a[3000][3000],b[3000][3000],n,m,t,f[3000][3000],dp[3000][3000],sum[3000][3000];

int main()
{
    char x;
    n=read(),m=read(),t=read();
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            cin>>x;
//            if (x == '1') b[i][j]=b[i][j-1]+1;
//            if (x == '0') a[i][j]=a[i][j-1]+1; 
            sum[i][j]=sum[i][j-1]+(x=='1');
        }
    }
    
    for (int i=1;i<=n;i++)
    {
        memset(f,0,sizeof(f));
        for (int l=1;l<=m;l++)
            for (int r=1;r<=m;r++)
            {
                //f[1][r]=1;
                for (int k=0;k<l;k++)
                {
//                    int s1=a[i][l]-a[i][k];
//                    int s2=b[i][l]-b[i][k];
                    int js=sum[i][l]-sum[i][k];
                    f[l][r]=max(f[l][r],f[k][r-1]+max(js,l-js-k));
                }
            }
            
        for (int j=1;j<=t;j++)
        {
            for (int k=1;k<=min(j,m);k++)
                dp[i][j]=max(dp[i][j],dp[i-1][j-k]+f[m][k]);
        } 
    }
    
    int  ans=0;
    for (int i=1;i<=t;i++) ans=max(ans,dp[n][i]);
    printf("%d",ans);
}
posted @ 2021-03-13 10:11  zxsoul  阅读(29)  评论(0编辑  收藏  举报