BZOJ1296【动态规划】

/* I will wait for you */

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <string>
#define make(a,b) make_pair(a,b)
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef map<int, int> mii;

const int maxn = 60;
const int maxm = 3000;
const int maxs = 26;
const int inf = 0x3f3f3f3f;
const int P = 1000000007;
const double error = 1e-9;

inline ll read()
{
	ll x = 0, f = 1; char ch = getchar();
	while (ch > '9' || ch < '0' )
		f = (ch == '-' ? -1 : 1), ch = getchar();
	while (ch <= '9' && ch >= '0')
		x = x * 10 + ch - '0', ch = getchar();
	return f * x;
}

int ma[maxn][maxn], f[maxn][maxn][maxn], dp[maxn][maxm],
    h[maxn][maxn], sum[maxn][maxn][maxn][2];

int main()
{
	int n = read(), m = read(), t = read();

	for (int i = 1; i <= n ;i++) {
		char s[100]; scanf("%s", s);
		for (int j = 1; j <= m; j++)
			ma[i][j] = s[j - 1];
	}

	for (int i = 1; i <= n; i++)
		for (int s = 1; s <= m; s++)
			for (int t = 1; t <= m; t++)
				for (int j = s; j <= t; j++) {
					sum[i][s][t][0] += (ma[i][j] == '0');
					sum[i][s][t][1] += (ma[i][j] == '1');
				}
	
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			for (int k = 1; k <= j; k++)
				for (int l = k; l <= j; l++) {
					f[i][j][k] = 
		max(f[i][j][k], f[i][l - 1][k - 1] + sum[i][l][j][0]);
					f[i][j][k] = 
		max(f[i][j][k], f[i][l - 1][k - 1] + sum[i][l][j][1]);
				}

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			h[i][j] = f[i][m][j];

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= t; j++)
			for (int k = 0; k <= j; k++)
				dp[i][j] = 
		max(dp[i][j], dp[i - 1][j - k] + h[i][k]);

	printf("%d\n", dp[n][t]);
		
	return 0;
}

你可能感兴趣的:(BZOJ1296【动态规划】)