将原问题转化为求完全由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. Spring多数据源的动态切换

    Spring多数据源的动态切换 目前很多项目中只能配置单个数据源,那么如果有多个数据源肿么办?Spring提供了一个抽象类AbstractRoutingDataSource,为我们很方便的解决了这个问 ...

  2. Android 生命周期 和 onWindowFocusChanged

    转载 http://blog.csdn.net/pi9nc/article/details/9237031 onWindowFocusChanged重要作用 Activity生命周期中,onStart ...

  3. jquery Loading图片延迟加载特效

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 普通用户开启AUTOTRACE 功能

    AUTOTRACE是一个SQL*Plus工具,用于跟踪SQL的执行计划,收集执行时所耗用资源的统计信息.系统账户本身具有AUTOTRACE,其他账户需要通过手动赋予 一. 用系统账户登录(DBA) S ...

  5. android studio 打开github开源代码

    1.最近下载的开源代码全是github来的,一直用eclipse开发,对于android studio来说是全新的 2.在eclipse导入一个工程那是so easy, import选择一下就可以. ...

  6. [摘] SQLPLUS Syntax

    You use the SQLPLUS command at the operating system prompt to start command-line SQL*Plus: SQLPLUS [ ...

  7. X86 复制本地 生成有问题、类型初始值设定项引发异常

    一. 选择项目,右击属性——生成——目标平台 选择x86就可以了. 二. 有的时候你发现你项目中的dll没有生成到本地bin,这时右击它属性,到它引用的地方复制引用dll放到部署环境中,你会发现一样报 ...

  8. phpstorm运行在浏览器中执行php文件报502错误

    原因是之前mac自带的php5.5版本被我升级到了5.6 通过phpinfo()查看到目前php5.6的安装目录 重新制定一些interpreter的路径 /usr/local/php5/bin 就可 ...

  9. EXTJS 4.2 资料 控件之 xtype: "fieldcontainer",追加html

    { xtype: "fieldcontainer", layout: "hbox", items: [ { fieldLabel: '素材目录', name: ...

  10. 【学习总结】UIGestureRecognizer(手势识别器)

    基本知识点 : -> IOS 3.2之后 , 苹果推出了手势识别功能 ( Gesture Recognizer ) 在触摸事件处理方面 , 简化开发难度. -> UIGesture Rec ...