cv1159 all-0 sub-matrix solution to a problem report

Topic Portal

[Title] effect

Given a $ n * n $ matrix of 01, is seeking a full sub-matrix contains the maximum number of 0 0.

[Analysis] ideas

In fact, it said to be monotonous stack maintenance? But the question is ...... explanation I found hanging by-line method

For a certain location $ (i, j) $, seeking the upward up to the number of consecutive zeros up to how many consecutive left 0, right up to the number of consecutive zeros to ensure that when the (left and right extension on is legal)

So you over it? But it is still very simple ...... see separate fact a line similar to this question , and then if you want to reduce the space, then rolling it on an array of friends, just one dimension can be, but because I'm lazy, so the direct use of two-dimensional array.

【Code】

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #define g() getchar()
 7 #define rg register
 8 #define go(i,a,b) for(rg int i=a;i<=b;i++)
 9 #define back(i,a,b) for(rg int i=a;i>=b;i--)
10 #define db double
11 #define ll long long
12 #define il inline
13 #define pf printf
14 #define sf scanf
15 using namespace std;
16 int fr(){
17     int w=0,q=1;
18     char ch=g();
19     while(ch<'0'||ch>'9'){
20         if(ch=='-') q=-1;
21         ch=g();
22     }
23     while(ch>='0'&&ch<='9') w=(w<<1)+(w<<3)+ch-'0',ch=g();
24     return w*q;
25 }
26 const int N=2002;
27 int n,l[N][N],r[N][N],h[N][N],a[N][N],ans;
28 int main(){
29     sf("%d",&n);
30     go(i,1,n) go(j,1,n)
31         sf("%d",&a[i][j]),l[i][j]=r[i][j]=h[i][j]=!a[i][j];
32 //Initialization, if the current location is 1 then it is surely not legitimate, if it is 0 then add their own before the first extension 
33      Go (i, 1 , the n-) Go (J, 1 , the n-) [i] H [J] A = [i] [J] == 0 H [I-? 1 ] [J] + 1 : 0 ;
 34      // current position as 0 directly transferred from the just above the otherwise illegal 
35      Go (i, 1 , n-) Go (J, . 1 , n-) { // left extension 
36          RG int K = J- . 1 ;
 37 [          the while (K> = . 1 && H [I] [K]> = H [I] [J]) L [I] [J] ++, K-- ;
 38 is      }
 39      Go (I, . 1 , n-) Back (J, n-, . 1) { // extended to the right (Note that the second layer cyclic order) 
40          RG int K = J + . 1 ;
 41 is          the while (K <= n-&& H [I] [K]> = H [I] [J]) R & lt [I ] [J] ++, K ++ ;
 42 is      }
 43 is      Go (I, . 1 , n-) Go (J, . 1 , n-) // statistical answer 
44 is          ANS = max (ANS, H [I] [J] * (R & lt [ I] [J] + L [I] [J] - 1 ));
 45  // Save 1 because the current position (i, j) is recorded twice, once to repeat subtracted when calculating the width 
46 is      PF ( " % D \ n- " , ANS);
 47      return  0 ;
 48 }
Code poke here

Guess you like

Origin www.cnblogs.com/THWZF/p/11530356.html