BZOJ1057 [ZJOI2007]棋盘制作 【最大同色矩形】

1057: [ZJOI2007]棋盘制作

Time Limit: 20 Sec  Memory Limit: 162 MB
Submit: 3248  Solved: 1636
[Submit][Status][Discuss]

Description

国际象棋是世界上最古老的博弈游戏之一，和中国的围棋、象棋以及日本的将棋同享盛名。据说国际象棋起源

Input

第一行包含两个整数N和M，分别表示矩形纸片的长和宽。接下来的N行包含一个N * M的01矩阵，表示这张矩形

Output

包含两行，每行包含一个整数。第一行为可以找到的最大正方形棋盘的面积，第二行为可以找到的最大矩形棋

3 3
1 0 1
0 1 0
1 0 0

4
6

HINT

N, M ≤ 2000

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)
using namespace std;
const int maxn = 2005,maxm = 100005,INF = 1000000000;
inline int RD(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 1) + (out << 3) + c - '0'; c = getchar();}
return out * flag;
}
int A[maxn][maxn],sum[maxn][maxn],N,M,up[maxn],down[maxn],ans1 = 0,ans2 = 0;
bool vis[maxn];
struct node{int i,len;}e[maxn];
inline bool operator < (const node& a,const node& b){return a.len > b.len;}
inline int findu(int u){return u == up[u] ? u : up[u] = findu(up[u]);}
inline int findd(int u){return u == down[u] ? u : down[u] = findd(down[u]);}
void solve(){
for (int i = 1; i <= N; i++){
sum[i][M] = 1;
for (int j = M - 1; j > 0; j--)
sum[i][j] = A[i][j] == A[i][j + 1] ? sum[i][j + 1] + 1 : 1;
}
for (int j = 1; j <= M; j++){
for (int i = 1; i <= N; i++){
e[i].i = i; e[i].len = sum[i][j]; down[i] = up[i] = i; vis[i] = false;
}
sort(e + 1,e + 1 + N);
for (int i = 1; i <= N; i++){
int k = e[i].i; vis[k] = true;
//printf("(%d,%d)  len = %d   %d\n",k,j,e[i].len,ans1);
if (k > 1 && vis[k - 1] && A[k - 1][j] == A[k][j]){
up[k] = findu(k - 1);
down[k - 1] = k;
}
if (k < N && vis[k + 1] && A[k + 1][j] == A[k][j]){
down[k] = findd(k + 1);
up[k + 1] = k;
}
int u = findu(k),d = findd(k),ed = min(e[i].len,d - u + 1);
//printf("%d %d\n",u,d);
ans1 = max(ans1,ed * ed);
ans2 = max(ans2,e[i].len * (d - u + 1));
}
}
}
int main(){
N = RD(); M = RD();
REP(i,N) REP(j,M) A[i][j] = RD() ^ ((i & 1) ^ (j & 1));
//REP(i,N){REP(j,M) cout<<A[i][j]<<' ';cout<<endl;}
solve();
cout<<ans1<<endl<<ans2<<endl;
return 0;
}

posted @ 2017-12-09 12:56  Mychael  阅读(117)  评论(0编辑  收藏