BZOJ1057 [ZJOI2007] board making (Max thought)

Source: Internet
Author: User

1057: [ZJOI2007] board Making

Time limit:20 Sec Memory limit:162 MB
submit:1848 solved:936
[Submit] [Status] [Discuss]

Description

Chess is one of the oldest game games in the world, with Chinese Weiqi, chess and Japanese chess. It is said that chess originated from the thought of the I Ching, the chessboard is a 8*8 size of black and white square, corresponding to 8,864 gua, black and white corresponding yin and yang. And our protagonist, the little Q, is a chess enthusiast. As a top-notch player, he was not content with the usual chessboard and rules, so he and his good friend, Little W, decided to expand the chessboard to fit their new rules. Little Q found a rectangular piece of paper made up of squares of n*m, each of which was painted with one of the two colors of black and white. Little Q wanted to cut some of this paper as a new chessboard, and of course he wanted the chessboard to be as big as possible. But little Q has not decided to find a square chessboard or a rectangular chessboard (of course, the chessboard must be black and white, that is, the adjacent lattice is different), so he hopes to find the largest square checkerboard area and the largest rectangular checkerboard area, which will determine which is better. So little Q found the upcoming National Information Science Competition you, can you help him?

Input

The first line contains two integers n and m, each representing the length and width of the rectangular paper. The next n rows contain a 01 matrix of N * m, representing the color of the rectangle paper (0 for White and 1 for black).

Output

Contains two lines, each containing an integer. The first behavior can be found in the area of the largest square checkerboard, and the second behavior can be found in the area of the largest rectangular checkerboard (note that squares and rectangles can intersect or contain).

Sample Input

3 3

1 0 1

0 1 0

1 0 0

Sample Output

4

6

HINT

For 100% of data, N, m≤2000

Source

Ideas

The idea of maximum.

The first question of the topic is the classic DP problem.

For the second question, we solve it with the greatest thought. The suspension line Up[i][j] represents the maximum value that IJ can extend upward, and l[i][j] represents the largest subscript that the IJ overhang can extend to the left, and R is the same. For each row to be scanned from left to right, maintain the subscript at the far right of the non-extending at the same time to push L, similar to solve R.

Obviously, the first question can also be solved when we solve the second question while maintaining the maximum side length.

About recursion:

If G[i][j]==g[i-1][j]

Up[i][j]=1;

L[i][j]= (I,J) to the left to extend the maximum subscript lo.

R[i][j]= (I,J) to the right to extend the smallest subscript ro.

Else

Up[i][j]=up[i-1][j]+1

L[i][j]=max (L[i-1][j],lo);

R[i][j]=min (R[i-1][j],ro);
Code

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 Const intMAXN = -+Ten;7 8 intW[MAXN][MAXN];9 intn,m;Ten  One intRead_int () { A     CharC=GetChar (); -      while(!isdigit (c)) c=GetChar (); -     intx=0; the      while(IsDigit (c)) { -x=x*Ten+c-'0'; -C=GetChar (); -     } +     returnx; - } +  A /* at inline bool Can (int I,int j) { - return (w[i][j]^w[i-1][j-1]==0 && w[i-1][j]^w[i][j-1]==0 && w[i][j]!=w[i-1][j]); - } - int D[MAXN][MAXN]; - void Get_ans1 () { - int ans=0; in for (int i=1;i<=n;i++) - for (int j=1;j<=m;j++) to        { + d[i][j]=1; - if (i>1 && j>1 && can (I,J)) the               { * d[i][j]+=min (D[i-1][j-1],min (d[i-1][j],d[i][j-1)); $ Ans=max (Ans,d[i][j]*d[i][j]);Panax Notoginseng               } -        } the cout<<ans<< "\ n"; + } A */ the  + intL[MAXN][MAXN],UP[MAXN][MAXN],R[MAXN][MAXN]; - voidGet_ans () { $     intans1=0, ans2=0; $      for(intI=1; i<=n;i++) -     { -         intlo=0, ro=m+1; the          for(intj=1; j<=m;j++) -         {Wuyi             if(j==1|| w[i][j-1]==W[I][J]) lo=J; the             if(i==1|| w[i][j]==w[i-1][J]) up[i][j]=1, l[i][j]=Lo; -             Else { Wuup[i][j]=up[i-1][j]+1; -L[i][j]=max (l[i-1][j],lo); About             } $         } -          for(intj=m;j;j--) -         { -             if(J==m | | w[i][j+1]==W[I][J]) ro=J; A             if(i==1|| w[i][j]==w[i-1][J]) r[i][j]=ro; +             Else { theR[i][j]=min (r[i-1][j],ro); -Ans1=max (Ans1,min (up[i][j],r[i][j]-l[i][j]+1)); $Ans2=max (ans2,up[i][j]* (r[i][j]-l[i][j]+1)); the             } the         } the     } thecout<<ans1*ans1<<"\ n"; -cout<<ans2<<"\ n"; in } the intMain () { the      AboutN=read_int (); m=read_int (); the      the      for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) w[i][j]=read_int (); the       + Get_ans (); -      the     return 0;Bayi}

PS: On the idea of maximizing, see Wang Zhiqun " talking about solving maximal sub-rectangle problem with maximal thought "

BZOJ1057 [ZJOI2007] board making (Max thought)

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.