将原问题转化为求完全由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. JavaScript jQuery 入门回顾

    ​$符号 $是著名的jQuery符号.实际上,jQuery把所有功能全部封装在一个全局变量jQuery中,而$也是一个合法的变量名,它是变量jQuery的别名: window.jQuery; // j ...

  2. 译文:javascript function中的this

    个人理解+google翻译.如有错误,请留言指正.原文来自MDN: this 简介 Javascript中一个函数的this关键字的行为相对其它语言有些不同.在严格模式和非严格模式间也有区别. 在大多 ...

  3. 【转】JS函数的定义与调用方法

    JS函数调用的四种方法:方法调用模式,函数调用模式,构造器调用模式,apply,call调用模式 1.方法调用模式:先定义一个对象,然后在对象的属性中定义方法,通过myobject.property来 ...

  4. Class<Object>与Class<?>有何区别呢

    1.? 和 Object 差不多,不过还是有差别.在这种情况下: class<? extends SomeClass> , Object就不能用了 Object是一个具体的类名,而?是一个 ...

  5. WCF、WebAPI、WCF REST、Web Service之间的区别

    在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下,你有很多的选择来构建一个HTTP Services.我分享一下我对 ...

  6. CSS制作图片水平垂直居中 亲测推荐

    空白标签实现图片的垂直居中 这种方法很有意思,也很有独特之处,我的思路也是来自于张鑫旭-鑫空间-鑫生活写的<大小不固定的图片.多行文字的水平垂直居中>一文中的使用空白图片实现垂直对齐.他主 ...

  7. C语言socket编程--每日签到

    前几天写了个python的每日签到,你运行还得借助crontab,很是不爽.....正好前几天看了个关于c编写daemon进程,加上自己那点可怜的socket知识,于是我们重操旧页,C语言版的每日签到 ...

  8. 深层次详解Exception

    所有的异常类都继承自System.Exception类,当异常产生时,CLR将创建该异常类的实例对象,将从最底层依次寻找合适的异常类型,同时若存在catch语句时将会选择最合适的语句进行处理. cat ...

  9. EXTJS 4.2 资料 控件之Grid 那些事

    最近在学习Extjs4.2 ,积累文章,看得不错,再此留年: //表格数据最起码有列.数据.转换原始数据这3项 Ext.onReady(function(){ //定义列 var columns = ...

  10. windows 2008 R2 Activition

    无需破解:Windows Server 2008 R2 至少免费使用 900天 1.首先安装后,有一个180天的试用期. 2.在180天试用期即将结束时,使用下面的评估序列号激活Svr 2008 R2 ...