[ZJOI2007] Checkerboard production (monotonic stack)

Source: Internet
Author: User

[ZJOI2007] Board production title 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 Ching, the chessboard is a 8 \times 88x8 size of black and white square, corresponding to 8,864 gua, black and white corresponding yin and yang.

And our hero 小Q is a chess enthusiast. As a top player, he was not content with the usual chessboard and rules, so he and his best friend 小W decided to expand the board to fit their new rules.

小QA rectangular piece of paper with a square of n \times mnxm is found, each of which is painted with one of two colors in black and white. 小Qwant to cut a part of this paper as a new chessboard, of course, he wants the chessboard as large as possible.

However, it 小Q has not been decided whether 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, thus deciding which is better.

So 小Q I found you in the National Information Science competition, can you help him?

Input/output format

Input format:

contains two integers nn and mm, respectively, representing the length and width of the rectangular paper. The next nn line contains a 0101 matrix of n \times mnxm, which represents the color of the rectangle paper (00 for white and 11 for black).

Output format:

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).

Input/Output sample

Input Sample # #:

3 3
1 0 1
0 1 0
1 0 0

Sample # # of output:

4
6

Description

For \ (20\%\) data,\ (N, m≤80n,m≤80\)

For \ (40\%\)% of data,\ (N, m≤400n,m≤400\)

For \ (100\%\) data,\ (N, m≤2000n,m≤2000\)

Solution

Before we get to that, we first know a data structure-a monotone stack
Monotonic stacks in the calculation of the maximum sub-matrix This convenience is very useful

So what is the monotony stack?
In fact, as the name implies, is a monotonic stack
If you keep the monotonically increasing
Then, when an element is smaller than the top of the stack, we will constantly pop up the top of the stack until the current element is larger than the top of the stack, which is usually updated in the process.
Otherwise directly into the stack

monotonically decreasing the same

So look at this topic: there are several rectangles above a horizontal line, the area of the largest rectangle contained within the set of these rectangles (the shaded part is the answer), the number of rectangles \ (<=10^5\)

Determine the monotonic stack, it should be well written, because the width of the rectangle w[] is 1, long s[] given
So we set up a monotone stack to hold several rectangles, the height of which is monotonically increasing, and the rectangles are scanned from left to right.
If the current rectangle is higher than the top rectangle of the stack, directly into the stack
Otherwise pops up the top of the stack, in the process of the stack, we accumulate the sum of the width of each rectangle, and then use the height of the current rectangle multiplied by this sum, the maximum value is the answer, after the end of the stack process, we put the height of the current height, width as the cumulative value of the new matrix into the stack

The general idea is this, then this problem should be a little bit better think about it
The legal height of each column is preprocessed, then each row is enumerated, the size of the largest sub-matrix is calculated, and the square is a special rectangle, so it can be calculated

Specific details: If we encounter an illegal element, we are going to calculate the current stack element accumulation width, but because the long rectangle may be satisfied with the length of the case, not with the previous rectangle 01, so we need another method of calculation, but because this method is not very good, the following code , please draw the reader to understand (there is time to update later)

Code
 #include <bits/stdc++.h> #define RG Register#define x (H[s[top]) #define Y (pos-s[top-1]-1) #define Z min (x, y ) using namespace Std;const int N=2e3+10;int n,m,top,ans1,ans2;int a[n][n],s[n],h[n];int main () {Ios::sync_with_stdio (0 ); cin>>n>>m; for (RG int i=1;i<=n;i++) for (RG int j=1;j<=m;j++) cin>>a[i][j]; for (RG int i=1;i<=n;i++) {for (RG int j=1;j<=m;j++) {if (a[i][j]!=a[i-1][j]) h[j]++; else h[j]=1; } int Pos=1; while (pos<=m) {s[top]=pos-1,s[++top]=pos++; while (Pos<=m && A[i][s[top]]!=a[i][pos]) {while (top && h[pos]
Bo Master Konjac Konjac, feel free to reprint. But the original link must be attached http://www.cnblogs.com/real-l/

[ZJOI2007] Checkerboard production (monotonic stack)

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.