bzoj1057: [ZJOI2007] chessboard making

topic

answer:

step1:

For all the chessboards on the graph, they must belong to the following two types:
1. The parity of the black squares is the same, and the white squares are different
. 2. The parity of the white squares is the same, but the black squares are different.
When inputting, it belongs to the first case and assigns 1. The problem of assigning 0 in the second case
is transformed into the largest 1 or 0 rectangle and square

step2:

square:

g1[i][j] represents the side length of all 1 square with (i, j) as the lower right corner
g2[i][j] represents the side length of all 0 square with (i, j) as the lower right corner
g[i] [j]=min(g[i-1][j-1],min(g[i-1][j],g[i][j-1]))+1
ans for all g1[i] [j], the maximum value of g2[i][j] The
answer ans*ans

rectangle:

f1[i][j] indicates that there are several points with the same color as (i,j) above (i,j) (including (i,j))
f2[i][j] indicates that (i,j) Below (including (i,j)) there are several points with the same color as (i,j)
f1[i][j]=(a[i][j]==a[i-1][j]) *f1[i-1][j]+1;
f2[i][j]=(a[i][j]==a[i+1][j])*f2[i+1][j ]+1;
Then enumerate i, then enumerate j, record k during the process of enumerating j, to ensure that the colors of k…j are the same
Calculate c1=min(f1[i][k…j]), c2=min( f2[i][k...j]
) keep updating ans with (j-k+1)*(c1+c2-1)

doubt:

If there is a k'>k, and c1'+c2'>c1+c2, then (j-k'+1) (c1'+c2'-1) may be greater than (j-k+1) (c1+ c2-1), then ans is not optimal. The
popular understanding is: our rectangle is to ensure that the length is the largest and the width is as large as possible. If there is a rectangle with a smaller length and a larger area than ours, this How to deal with this situation?
In fact, such a situation will be considered before or after, that is, when i' is greater than i or i' is less than i, you can draw a picture to understand the specific situation, I can't give a very strict proof

Standard range:

#include<bits/stdc++.h>
using namespace std;
#define o [2003][2003]
int n,m,i,j,a o,f o,g1 o,g2 o,f1 o,f2 o,ans,c1,c2,k;
int main(){
    cin>>n>>m;
    for (i=1;i<=n;i++)
        for (j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
            a[i][j]^=(i^j)&1;
            f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+a[i][j];
            if (a[i][j]) g1[i][j]=min(g1[i-1][j-1],min(g1[i-1][j],g1[i][j-1]))+1;
            else g2[i][j]=min(g2[i-1][j-1],min(g2[i-1][j],g2[i][j-1]))+1;
            ans=max(ans,max(g1[i][j],g2[i][j]));
            f1[i][j]=(a[i][j]==a[i-1][j])*f1[i-1][j]+1;
        }
    cout<<ans*ans<<endl;
    ans=0;
    for (i=n;i>=1;i--)
        for (j=1;j<=m;j++) f2[i][j]=(a[i][j]==a[i+1][j])*f2[i+1][j]+1;
    for (i=1;i<=n;i++){
        c1=f1[i][1],c2=f2[i][1];k=1;
        for (j=1;j<=m;j++){
            ans=max(ans,(j-k+1)*(c1+c2-1));
            if (a[i][j]==a[i][j+1]) c1=min(c1,f1[i][j+1]),c2=min(c2,f2[i][j+1]);
            else c1=f1[i][j+1],c2=f2[i][j+1],k=j+1;
        }
    }
    cout<<ans;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326761931&siteId=291194637