Http://blog.csdn.net/zhang20072844/article/details/12925581
Given a matrix M1 of M * n, the element in it is only 0 or 1. Find out a child matrix m2 of M1. The element in M2 is only 1, and the area of M2 is the largest. Area of M2 output. Input
1st rows: 2 numbers m, n are separated by spaces (2 <= m, n <= 100) 2nd-n + 1 rows: the number of m rows, separated by spaces in the middle, both are 0 or 1.
Output
The maximum output is the area of the Child matrix of 1.
Input example
3 31 1 01 1 10 1 1
Output example
4
Question Analysis:
When N = 100, the minimum submatrix and O (N ^ 3) can be obtained. However, when n is 1000, an order of magnitude is required.
Here we can find one row.
For example, the first line 1 1 0 1 1 1 0 1
Then we sum B [] =}
For example, the second behavior is 1 1 0 0 0 1 1
Then we continue to sum B [] = {, 0 }......
That is to say:
If (Map [I] [k])
B [k] ++;
Else
B [k] = 0;
In this way, a histogram is obtained. I wonder if you have obtained the maximum histogram area.
Is the histogram of different heights. How can we find the largest rectangular area of this histogram?
For example, if the histogram height is 1 2 3 2, what is the maximum area?
When the first one is used as the benchmark, the height is 1, and the subsequent rectangles can all form a rectangle, so the area is 4. when the second is used as the benchmark, only the last three items are allowed, with an area of 6. likewise .... The maximum area is 6.
The same is true here. After obtaining the height from the first row to the current row, you can find the current maximum area.
We create two arrays, L [] and R [], respectively, to represent the left and right boundary when B [J] is used as the benchmark, then, the area of B [J] is changed to B [J] * (R [J]-l [J] + 1), and the largest one is given.
For l [], R [], we can use the following calculation method:
# Include <iostream> # include <cstdlib> # include <cstring> using namespace STD; const int maxn = 510; int map [maxn] [maxn]; int B [maxn], L [maxn], R [maxn]; int M, N; int main () {While (CIN> m> N) {int max = 0; memset (map, 0, sizeof (MAP); For (INT I = 1; I <= m; ++ I) {for (Int J = 1; j <= N; ++ J) {CIN> map [I] [J] ;}} for (Int J = 0; j <= n + 1; ++ J) {B [J] = 0 ;}for (INT I = 1; I <= m; ++ I) // I indicates taking row I as the bottom {for (int K = 1; k <= N; ++ K) {If (Map [I] [k]) B [k] ++; elseb [k] = 0 ;}/ * For (Int J = 1; j <= N; ++ J) {// This method can also be used. I think int P = J; while (P> = 1 & B [J] <= B [p --]); int q = J; while (q <= N & B [J] <= B [q ++]); If (B [J] & (B [J] * (q-p-1)> max) {max = B [J] * (Q-p) ;}}/For (Int J = 1; j <= N; ++ J) {L [J] = J; while (L [J]-1> = 1 & B [J] <= B [L [J]-1]) {L [J] = L [L [J]-1]; // The write speed is faster, write l [J] = L [J]-1 slow} For (Int J = N; j> = 1; -- j) {R [J] = J; while (B [J] <= B [R [J] + 1] & R [J] + 1 <= N) {R [J] = R [J] + 1] ;}}for (Int J = 1; j <= N; ++ J) {If (B [J] & (B [J] * (R [J]-l [J] + 1)> MAX )) {max = B [J] * (R [J]-l [J] + 1) ;}}} cout <max <Endl ;}return 0 ;}
Daquan 1 submatrix