将原问题转化为求完全由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()

【HDOJ】4328 Cut the cake的更多相关文章

  1. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  2. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  3. AC日记——【模板】Link Cut Tree 洛谷 P3690

    [模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...

  4. LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测

    UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...

  5. (RE) luogu P3690 【模板】Link Cut Tree

    二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...

  6. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  7. HDU 4328 Cut the cake

    Cut the cake Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  8. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  9. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

随机推荐

  1. 【winform】如何在DateTimePicker中显示时分秒

    1. 首先属性里面的Format属性value设置为Custom(默认为Long) 2. 对应的Custom属性value设置为yyyy-MM-dd HH:mm:ss

  2. JS禁用和启用鼠标滚轮滚动事件

    // left: 37, up: 38, right: 39, down: 40, // spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: ...

  3. angularJs--$apply和$watch,$digest方法的意思

    这个视图对应的控制器是 这样的话,这个date变量,是不会发生改变的,没有触发脏检查,所以这时候要$apply方法,所有自定义的方法都要用$apply来触发脏检查 这样那个日期就会变化了 $diges ...

  4. js中的异常处理try...catch使用介绍

    在JavaScript可以使用try...catch来进行异常处理. 例如: try { foo.bar();} catch (e) { alert(e.name + ": " + ...

  5. Python问题之奇怪诡异的Bug

    最近又重新装上了windows 7感觉还是那样,主要是想用M8SDK写些程序.也想在windows上玩玩,一直都觉得用C写一些常用的东东很复杂,只有借助于解释性语言了,在python, ruby间选择 ...

  6. Android Studio 导入第三方jar包

    1.先将AS切换到Project 2.在app-main-src下建一个libs目录,将jar包拷到里面 3.右击jar,add as Library

  7. EXTJS 4.2 资料 Grid嵌套

    如图: var ParentContCateId = 0; var start = 0; var limit = 20; DistributionPointForm = function () { E ...

  8. codeblocks常用快捷键

    CodeBlocks常用操作快捷键编辑部分:Ctrl + A:全选Ctrl + C:复制Ctrl + X: 剪切Ctrl + V:粘贴Ctrl + Z:撤销Ctrl + S:保存Ctrl + Y / ...

  9. 关于mapreduce过程中出现的错误:Too many fetch-failures

    Reduce task启动后第一个阶段是shuffle,即向map端fetch数据.每次fetch都可能因为connect超时,read超时,checksum错误等原因而失败.Reduce task为 ...

  10. oracle 求两个时间点直接的分钟、小时数

    select )) h, )) m, )) s from gat_data_record gdr where gdr.enddt between to_date('2011-1-1','yyyy-mm ...