【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵。
挺经典的通过dp将n^3转化为n^2。

 /* 4328 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
char s[maxn][maxn];
int a[maxn][maxn];
int L[maxn][maxn], R[maxn][maxn], T[maxn][maxn];
int n, m; int calc() {
int ret = ; rep(j, , m+) {
T[][j] = ;
L[][j] = ;
R[][j] = m;
} rep(i, , n+) {
int l;
l = ;
rep(j, , m+) {
if (a[i][j] == ) {
T[i][j] = T[i-][j] + ;
L[i][j] = max(L[i-][j], l+);
} else {
T[i][j] = ;
L[i][j] = ;
l = j;
}
} int r;
r = m + ;
per(j, , m+) {
if (a[i][j] == ) {
R[i][j] = min(R[i-][j], r-);
} else {
R[i][j] = m;
r = j;
}
} rep(j, , m+) {
if (a[i][j]) {
int h = T[i][j];
int w = R[i][j] - L[i][j] + ;
ret = max(ret, w+h);
}
}
} return ret << ;
} void solve() {
int ans = , tmp; // blue
rep(i, , n+)
rep(j, , m+)
a[i][j] = s[i][j] == 'B';
tmp = calc();
ans = max(ans, tmp); // red
rep(i, , n+)
rep(j, , m+)
a[i][j] = s[i][j] == 'R';
tmp = calc();
ans = max(ans, tmp); rep(i, , n+) {
rep(j, , m+) {
if ((i+j) & ) {
a[i][j] = s[i][j]!='R';
} else {
a[i][j] = s[i][j]=='R';
}
}
}
tmp = calc();
ans = max(ans, tmp); rep(i, , n+) {
rep(j, , m+) {
if ((i+j) & ) {
a[i][j] = s[i][j]=='R';
} else {
a[i][j] = s[i][j]!='R';
}
}
}
tmp = calc();
ans = max(ans, tmp); printf("%d\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t; scanf("%d", &t);
rep(tt, , t+) {
scanf("%d %d", &n, &m);
rep(i, , n+)
scanf("%s", s[i]+);
printf("Case #%d: ", tt);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

数据生成器。

 from copy import deepcopy
from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t = 10
bound = 10**18
op = "RB"
fout.write("%d\n" % (t))
for tt in xrange(t):
n = randint(100, 1000)
m = randint(100, 1000)
fout.write("%d %d\n" % (n, m))
for i in xrange(n):
line = ""
for j in xrange(m):
idx = randint(0, 1)
line += op[idx]
fout.write("%s\n" % (line)) def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()
上一篇:DEV获取GridControl当前行


下一篇:Java与C++简单对比