题目描述
国际象棋是世界上最古老的博弈游戏之一,和中国的围棋、象棋以及日本的将棋同享盛名。据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳。
而我们的主人公小Q,正是国际象棋的狂热爱好者。作为一个顶尖高手,他已不满足于普通的棋盘与规则,于是他跟他的好朋友小W决定将棋盘扩大以适应他们的新规则。
小Q找到了一张由N*M个正方形的格子组成的矩形纸片,每个格子被涂有黑白两种颜色之一。小Q想在这种纸中裁减一部分作为新棋盘,当然,他希望这个棋盘尽可能的大。
不过小Q还没有决定是找一个正方形的棋盘还是一个矩形的棋盘(当然,不管哪种,棋盘必须都黑白相间,即相邻的格子不同色),所以他希望可以找到最大的正方形棋盘面积和最大的矩形棋盘面积,从而决定哪个更好一些。
于是小Q找到了即将参加全国信息学竞赛的你,你能帮助他么?
【题目分析】
行列奇偶性不同,取反,然后分别单调栈找最大子矩阵。
【代码】
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct node{int hi,wi;}sta[2001];
int n,m,map[2005][2005],h[2005][2005];
int top=0,max1=0,max2=0;
void solve()
{
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j)
h[i][j]=map[i][j]?h[i-1][j]+1:0;
// for (int i=1;i<=n;++i)
// {
// for (int j=1;j<=m;++j)
// printf("%d ",h[i][j]);
// printf("\n");
// }
// printf("\n");
for (int i=1;i<=n;++i)
{
top=0;
for (int j=1;j<=m+1;++j)
{
while (top&&sta[top].hi>h[i][j])
{
if ((!(top-1))||(sta[top-1].hi<h[i][j]))
{
max2=max(max2,sta[top].wi*sta[top].hi);
max1=max(max1,min(sta[top].wi,sta[top].hi)*min(sta[top].wi,sta[top].hi));
sta[top].hi=h[i][j];
}
else{
max2=max(max2,sta[top].wi*sta[top].hi);
max1=max(max1,min(sta[top].wi,sta[top].hi)*min(sta[top].wi,sta[top].hi));
top--;
sta[top].wi+=sta[top+1].wi;
}
}
if (top&&sta[top].hi==h[i][j]) sta[top].wi++;
else
{
top++;
sta[top].hi=h[i][j];
sta[top].wi=1;
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j)
{
scanf("%d",&map[i][j]);
if ((i^j)&1) map[i][j]^=1;
}
// for (int i=1;i<=n;++i)
// {
// for (int j=1;j<=m;++j)
// printf("%d ",map[i][j]);
// printf("\n");
// }
// printf("\n");
solve();
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j)
map[i][j]^=1;
// for (int i=1;i<=n;++i)
// {
// for (int j=1;j<=m;++j)
// printf("%d ",map[i][j]);
// printf("\n");
// }
// printf("\n");
solve();
printf("%d\n%d\n",max1,max2);
}