[ZJOI2007] board production - hanging line method

Title Description

Chess is one of the world's oldest game of chance, and Chinese chess, chess and the chess the equally famous in Japan. It is said that chess originated in the idea of the I Ching, the board is a \ (8 \ times 8 \) black and white square size, corresponding to 88 64 hexagrams, yin and yang, black and white correspond.
And our little hero Q, what aficionados of chess. As a leading expert, he was not satisfied with the general rules of the board, so he decided to follow his good friend small checkerboard W will expand to accommodate their new rules.
I found a small Q by the \ (N \ times M \) a rectangular sheet of lattice squares, each of which is coated with a grid of black and white one of two colors. Q want to cut a small part of this paper as a new board, of course, he hopes this board as large as possible.
But small Q has not been decided is to find a checkerboard square or a rectangle board (of course, no matter what, the board must be black and white, that is adjacent lattice different color), so he hopes to find the maximum square area and chessboard the largest rectangular area of the board, to decide which is better.
So you found a small Q will participate in the national informatics contest, can you help him?

Thinking

The title is suspended line used method
uses suspension wires Method: solving a given matrix satisfies a condition of maximum submatrix
need to use these things
left [i] [j]: Representative from (i, j) can reach leftmost position
right [i] [j]: representatives from the rightmost position (i, j) can reach
up [i] [j]: Representative from (i, j) the maximum length of the scale-up.
then this recursive
left [I] [J] = max (left [I] [J], left [I-. 1] [J])
right [I] [J] = min (right [I] [J], right [I-. 1 ] [J])

Code

/************************************************
*Author        :  lrj124
*Created Time  :  2019.08.26.08:17
*Mail          :  [email protected]
*Problem       :  luogu1169
************************************************/
#include <algorithm>
#include <cstdio>
using std :: max;
using std :: min;
const int maxn = 2000 + 10;
int n,m,a[maxn][maxn],left[maxn][maxn],right[maxn][maxn],up[maxn][maxn],ans_sq,ans;
int main() {
    scanf("%d%d",&n,&m);
    for (int i = 1;i <= n;i++)
        for (int j = 1;j <= m;j++) {
            scanf("%d",&a[i][j]);
            up[i][j] = 1;
            left[i][j] = right[i][j] = j;
        }
    for (int i = 1;i <= n;i++)
        for (int j = 2;j <= m;j++)
            if (a[i][j] ^ a[i][j-1]) left[i][j] = left[i][j-1];
    for (int i = 1;i <= n;i++)
        for (int j = m-1;j;j--)
            if (a[i][j] ^ a[i][j+1]) right[i][j] = right[i][j+1];
    for (int i = 1;i <= n;i++)
        for (int j = 1,tmp;j <= m;j++) {
            if (i > 1 && a[i][j]^a[i-1][j]) {
                up[i][j] = up[i-1][j]+1;
                left[i][j] = max(left[i][j],left[i-1][j]);
                right[i][j] = min(right[i][j],right[i-1][j]);
            }
            tmp = right[i][j]-left[i][j]+1;
            ans = max(ans,tmp*up[i][j]);
            ans_sq = max(ans_sq,min(up[i][j],tmp)*min(up[i][j],tmp));
        }
    printf("%d\n%d",ans_sq,ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/lrj124/p/11666368.html