洛谷 P1169 [ZJOI2007]棋盘制作

题目:棋盘制作

思路:
一点儿都不清真的扫描线。
类似于最大01子矩阵问题。
也许这几组数据能huck掉你的错误解——

INPUT
5 5
1 1 1 1 1
1 0 1 0 1
1 1 0 1 0
1 1 1 0 1
1 1 1 1 1
OUTPUT
9
9

5 6
1 1 1 1 1 0
1 0 1 0 1 1
0 1 0 1 1 0
1 0 1 1 1 1
1 1 1 1 1 0
OUTPUT
9
9

INPUT
5 6
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
OUTPUT
1
1

INPUT
5 6
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
OUTPUT
25
30

INPUT
5 6
1 0 1 1 1 0
0 1 0 1 0 1
1 0 1 1 1 0
1 1 1 1 0 1
1 1 1 1 1 0
OUTPUT
9
10

INPUT
5 5
0 0 1 1 0
0 1 1 1 0
1 0 1 0 1
1 1 0 1 0
0 0 0 1 1
OUTPUT
4
8

代码:

#include
using namespace std;

#define maxn 2000
#define read(x) scanf("%d",&x)

#define a(i,j) a[i][j]
#define u(i,j) Up[i][j]
#define l(i,j) Left[i][j]
#define r(i,j) Right[i][j]

int n,m;
int a[maxn+5][maxn+5];

int Up[maxn+5][maxn+5],Left[maxn+5][maxn+5],Right[maxn+5][maxn+5];

int main() {
	read(n),read(m);
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=m; j++) {
			read(a[i][j]);
		}
	}
	
	int s1=0,s2=0;
	
	for(int i=1; i<=n; i++) {
		int lo=0,ro=0;
		for(int j=1; j<=m; j++) {
			if(a(i,j)!=a(i-1,j)) u(i,j)=u(i-1,j)+1;
			else u(i,j)=1;
			if(j==1) l(i,j)=0;
			else if(a(i,j)!=a(i,j-1)) l(i,j)=min(lo+1,u(i,j)==1?(int)1e9:l(i-1,j)),lo++;
			else lo=0;
		}
		for(int j=m;j>=1;j--) {
			if(j==m) r(i,j)=0;
			else if(a(i,j)!=a(i,j+1)) r(i,j)=min(ro+1,u(i,j)==1?(int)1e9:r(i-1,j)),ro++;
			else ro=0;
			s1=max(s1,min(l(i,j)+r(i,j)+1,u(i,j))*min(l(i,j)+r(i,j)+1,u(i,j)));
			s2=max(s2,u(i,j)*(l(i,j)+r(i,j)+1));
		}
	}
	
	printf("%d\n%d",s1,s2);

	return 0;
}

你可能感兴趣的:(常见技巧,动态规划)