题目链接:这里
思路:
我们可以先求出每块木板分t次,每次的最大值,进而转化为机器分配问题。
代码:
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
template < typename T > void read(T &x)
{
int f = 1;x = 0;char c = getchar();
for (;!isdigit(c);c = getchar()) if (c == '-') f = -f;
for (; isdigit(c);c = getchar()) x = x * 10 + c - '0';
x *= f;
}
int a[55][55];
int b[55];
int dp[55][2505];
int f[2505];
int f2[55][2505];
int len[55];
int main()
{
//freopen("paint.in", "r", stdin);
//freopen("paint.out", "w", stdout);
int n, m, t;
cin >> n >> m >> t;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
{
char c;
cin >> c;
a[i][j] = c - '0';
if(j == 1 || a[i][j] != a[i][j - 1])
len[i]++;
}
for(int x = 1;x <= n;x++)
{
memset(dp, 0, sizeof(dp));
for(int i = 1;i <= m;i++)
for(int j = 1;j <= len[x];j++)
{
int sum = 0;
for(int k = i;k >= j;k--)
{
if(a[x][k] == a[x][i]) sum++;
dp[i][j] = max(dp[i][j], dp[k - 1][j - 1] + sum);
dp[i][j] = max(dp[i][j], dp[k - 1][j - 1] + i - k + 1 - sum);
}
f2[x][j] = max(f2[x][j], dp[i][j]);
}
}
for(int i = 1;i <= n;i++)
for(int j = t;j >= 1;j--)
for(int k = 1;k <= min(j, len[i]);k++)
f[j] = max(f[j], f[j - k] + f2[i][k]);
cout << f[t] << endl;
return 0;
}