bzoj1057: [ZJOI2007] Board making

Source: Internet
Author: User

Dp. Square Chessboard Needless to say, each point of a rectangular chessboard maintains the maximum distance V that can be reached first. The DP then finds the maximum distance l,r with its own v-min to the left to the right.

Because the width of the maximum rectangular checkerboard is certainly equal to the V of a point, we have also found the longest distance (r-l+1) corresponding to each V, so correctness is guaranteed.

There is also a little trick, which puts the horizontal ordinate and the odd points to vary or. You can turn the original problem into the largest 01 matrix.

#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intMAXN = -+Ten;intA[MAXN][MAXN],F[MAXN][MAXN],V[MAXN][MAXN],L[MAXN][MAXN],R[MAXN][MAXN];intn,m,res1=0, res2=0;intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++)     for(intj=1; j<=m;j++) {scanf ("%d",&A[i][j]); if((i+j)%2) a[i][j]^=1; //printf ("a[%d][%d] =%d\n", i,j,a[i][j]);    }     for(intI=1; i<=n;i++)     for(intj=1; j<=m;j++) {        if(i==1&&j==1) f[i][j]=1; Else if(a[i][j]!=a[i][j-1] || A[I][J]! = a[i-1][J]) f[i][j]=1; ElseF[i][j] = min (f[i-1][j-1],min (f[i-1][j],f[i][j-1]))+1; Res1=Max (res1,f[i][j]); //printf ("f[%d][%d] =%d\n", i,j,f[i][j]);} printf ("%d\n", res1*res1);  for(intI=1; i<=n;i++)     for(intj=1; j<=m;j++) {        if(i==1) v[i][j]=1; Else if(a[i-1][J]!=A[I][J]) v[i][j]=1; Elsev[i][j]=v[i-1][j]+1; }     for(intI=1; i<=n;i++)     for(intj=1; j<=m;j++) {        if(j==1) l[i][j]=1; Else if(v[i][j]>v[i][j-1] || a[i][j]!=a[i][j-1]) l[i][j]=J; Else{L[i][j]=l[i][j-1];  while(l[i][j]>1&& v[i][j]<=v[i][l[i][j]-1] && a[i][l[i][j]-1]==A[I][J]) l[i][j]=l[i][l[i][j]-1]; }    }     for(intI=1; i<=n;i++)     for(intj=m;j>=1; j--) {        if(j==m) r[i][j]=m; Else if(v[i][j]>v[i][j+1] || A[I][J]! = a[i][j+1]) r[i][j]=J; Else{R[i][j]= r[i][j+1];  while(R[i][j]<n && v[i][j]<=v[i][r[i][j]+1] && a[i][r[i][j]+1]==A[I][J]) r[i][j]=r[i][r[i][j]+1]; }    }     for(intI=1; i<=n;i++)     for(intj=1; j<=m;j++) Res2=max (res2,v[i][j]* (r[i][j]-l[i][j]+1)); printf ("%d\n", Res2); return 0;}

bzoj1057: [ZJOI2007] Board making

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.