BZOJ1057 [ZJOI2007]棋盘制作

Description

  国际象棋是世界上最古老的博弈游戏之一,和中国的围棋、象棋以及日本的将棋同享盛名。据说国际象棋起源
于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳。而我们的主人公小Q,
正是国际象棋的狂热爱好者。作为一个顶尖高手,他已不满足于普通的棋盘与规则,于是他跟他的好朋友小W决定
将棋盘扩大以适应他们的新规则。小Q找到了一张由N*M个正方形的格子组成的矩形纸片,每个格子被涂有黑白两种
颜色之一。小Q想在这种纸中裁减一部分作为新棋盘,当然,他希望这个棋盘尽可能的大。不过小Q还没有决定是找
一个正方形的棋盘还是一个矩形的棋盘(当然,不管哪种,棋盘必须都黑白相间,即相邻的格子不同色),所以他
希望可以找到最大的正方形棋盘面积和最大的矩形棋盘面积,从而决定哪个更好一些。于是小Q找到了即将参加全
国信息学竞赛的你,你能帮助他么?

Input

  第一行包含两个整数N和M,分别表示矩形纸片的长和宽。接下来的N行包含一个N * M的01矩阵,表示这张矩形
纸片的颜色(0表示白色,1表示黑色)。

Output

  包含两行,每行包含一个整数。第一行为可以找到的最大正方形棋盘的面积,第二行为可以找到的最大矩形棋
盘的面积(注意正方形和矩形是可以相交或者包含的)。

Sample Input

3 3
1 0 1
0 1 0
1 0 0

Sample Output

4
6

HINT

N, M ≤ 2000

 

 

正解:单调栈 or 悬线法

解题报告:

  为了巩固单调栈来写的这道题...

  对于一个题目要求的棋盘我们不是很好直接求,考虑我们可以把其转换成我们熟悉的模型——最大全0子矩阵。对于为0而且横纵坐标奇偶性不同的标为1,为1而且横纵坐标奇偶性相同的标为1;对于为1而且横纵坐标不同的标为0,对于为0而且横纵坐标相同的标为0。题目就转换成了最大全0子矩阵了。然后我们考虑单调栈的做法,维护一个数组,表示每个点最多可以往右拓展多远(1为障碍)。按列做,一行行扫,单调栈里面维护一个拓展宽度递增的值,发现当前行的这一列已经比栈顶元素小了,就弹栈直到合法。注意时刻更新一下答案,和栈中每个元素的实际控制范围(即往上可以到达哪一行)。考虑我们如果栈顶元素为S,那么i到栈顶所在行之间一定都比S大,不然S会在之前已经被弹出栈,所以相当于是S到i之间的这一大块宽度就是S(其余大于S的部分没有用),画一下图就很快可以懂了。

 

   当然,悬线法也是可以的,转完模型就是裸题了。

 

  单调栈:

 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 using namespace std;
14 typedef long long LL;
15 const int inf = (1<<30);
16 const int MAXN = 2011;
17 int n,m,ans,ans2;
18 int a[MAXN][MAXN];
19 int ri[MAXN][MAXN];//可以往右延伸多少
20 int stack[MAXN],top,up[MAXN];
21 
22 inline int getint()
23 {
24     int w=0,q=0; char c=getchar();
25     while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 
26     while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;
27 }
28 inline void getR(){  for(int i=1;i<=n;i++) for(int j=m;j>=1;j--) if(a[i][j]) ri[i][j]=ri[i][j+1]+1; else ri[i][j]=0; }
29 inline void getA(){
30     int to,lin;
31     for(int j=1;j<=m;j++){
32     top=0; 
33     for(int i=1;i<=n;i++) {
34         to=i;//栈内元素的控制范围
35         while(top>0 && stack[top]>=ri[i][j]) {
36         lin=min(stack[top],i-up[top]); lin*=lin;
37         ans=max(ans,lin); lin=stack[top]*(i-up[top]);
38         ans2=max(ans2,lin);//i到栈顶元素之间的每一行能拓展的宽度一定都大于等于当前栈顶,不然当前栈顶会被弹掉(画图可知)
39         to=min(to,up[top]);
40         top--;
41         }
42         stack[++top]=ri[i][j]; up[top]=to;
43     }
44     }
45 }
46 
47 inline void work(){
48     n=getint(); m=getint(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]=getint();
49     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(((i&1)==(j&1) && a[i][j])||((i&1)!=(j&1) && !a[i][j])) a[i][j]=1; else a[i][j]=0;
50     getR(); getA(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]=!a[i][j];
51     getR(); getA(); printf("%d\n%d",ans,ans2);
52 }
53 
54 int main()
55 {
56     work();
57     return 0;
58 }

 

悬线法:

 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 using namespace std;
14 typedef long long LL;
15 const int inf = (1<<30);
16 const int MAXN = 2011;
17 int n,m,ans,ans2;
18 int a[MAXN][MAXN];
19 int topl[MAXN],topr[MAXN],nowl,nowr,up[MAXN];
20 
21 inline int getint()
22 {
23     int w=0,q=0; char c=getchar();
24     while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 
25     while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;
26 }
27 
28 inline void getA(){
29     int lin; memset(up,0,sizeof(up)); //memset(topl,0,sizeof(topl)); memset(topr,0,sizeof(topr)); 
30     for(int i=1;i<=m;i++) topl[i]=1,topr[i]=m;
31     for(int i=1;i<=n;i++){
32     nowl=0,nowr=m+1;
33     for(int j=1;j<=m;j++) {
34         if(a[i][j]) {
35         up[j]=0;//清零
36         topl[j]=1; nowl=j;
37         }
38         else up[j]++,topl[j]=max(nowl+1,topl[j]);
39     }
40     for(int j=m;j>=1;j--) {
41         if(a[i][j]) {
42         topr[j]=m; nowr=j;
43         }
44         else {
45         topr[j]=min(topr[j],nowr-1);
46         lin=min(topr[j]-topl[j]+1,up[j]); lin*=lin;
47         ans=max(ans,lin);lin=(topr[j]-topl[j]+1)*up[j];
48         ans2=max(ans2,lin);
49         }
50     }
51     }
52 }
53 
54 inline void work(){
55     n=getint(); m=getint(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]=getint();
56     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(((i&1)==(j&1) && a[i][j])||((i&1)!=(j&1) && !a[i][j])) a[i][j]=1; else a[i][j]=0;
57     getA(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]=!a[i][j];  getA();
58     printf("%d\n%d",ans,ans2);
59 }
60 
61 int main()
62 {
63     work();
64     return 0;
65 }

 

posted @ 2016-09-28 16:26  ljh_2000  阅读(924)  评论(0编辑  收藏  举报