BZOJ1057: [ZJOI2007]棋盘制作

1057: [ZJOI2007]棋盘制作

Time Limit: 20 Sec  Memory Limit: 162 MB
Submit: 1329  Solved: 664
[Submit][Status]

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

对于20%的数据,N, M ≤ 80 对于40%的数据,N, M ≤ 400 对于100%的数据,N, M ≤ 2000

Source

题解:

h[i]表示 i 向上满足黑白相间的高度

首先可以用单调栈求出 h[i]作为区间最小值的左右范围,然后可以左右各扫一边得出横着最远拓展到哪里满足黑白相间

l 取max,r 取min,更新答案即可

犯了几个sb错误:

1.a[i-1,j]写成a[i,j-1]。。。

2.l 本该取 max,取成了 min。。。

代码:

 1 const maxn=2000+100;
 2 var a:array[0..maxn,0..maxn] of longint;
 3     h,l,r,sta,l1,l2,r1,r2:array[0..maxn] of longint;
 4     n,m,i,j,k,ans1,ans2,top,tmp:longint;
 5     function min(x,y:longint):longint;
 6      begin
 7      if x<y then exit(x) else exit(y);
 8      end;
 9     function max(x,y:longint):longint;
10      begin
11      if x>y then exit(x) else exit(y);
12      end;
13 
14 procedure init;
15  begin
16    readln(n,m);
17    fillchar(a,sizeof(a),60);
18    for i:=1 to n do
19     begin
20      for j:=1 to m do read(a[i,j]);
21     end;
22  end;
23 procedure main;
24  begin
25    h[0]:=-1;h[m+1]:=-1;ans1:=0;ans2:=0;
26    for i:=1 to n do
27     begin
28      for j:=1 to m do if a[i,j]=a[i-1,j] xor 1  then inc(h[j]) else h[j]:=1;
29      top:=0;
30      for j:=1 to m+1 do
31       begin
32       while (top>0) and (h[j]<h[sta[top]]) do
33        begin
34        r1[sta[top]]:=j-1;dec(top);
35        end;
36       inc(top);sta[top]:=j;
37       end;
38      top:=0;
39      for j:=m downto 0 do
40       begin
41       while (top>0) and (h[j]<h[sta[top]]) do
42        begin
43        l1[sta[top]]:=j+1;dec(top);
44        end;
45       inc(top);sta[top]:=j;
46       end;
47      for j:=1 to m do
48        begin
49        if a[i,j]<>a[i,j-1] xor 1 then tmp:=j;
50        l2[j]:=tmp;
51        end;
52      for j:=m downto 1 do
53        begin
54        if a[i,j]<>a[i,j+1] xor 1 then tmp:=j;
55        r2[j]:=tmp;
56        end;
57      for j:=1 to m do
58        begin
59        l[j]:=max(l1[j],l2[j]);
60        r[j]:=min(r1[j],r2[j]);
61        ans1:=max(ans1,h[j]*(r[j]-l[j]+1));
62        ans2:=max(ans2,sqr(min(h[j],r[j]-l[j]+1)));
63        end;
64      //for j:=1 to m do writeln(j,' ',l1[j],' ',l2[j],' ',r1[j],' ',r2[j]);
65     end;
66  writeln(ans2);
67  writeln(ans1);
68  end;
69 
70 begin
71   assign(input,'input.txt');assign(output,'output.txt');
72   reset(input);rewrite(output);
73   init;
74   main;
75   close(input);close(output);
76 end.   
View Code
原文地址:https://www.cnblogs.com/zyfzyf/p/3905311.html