More than 2019 cattle off summer school camp (Session 8) -A All-one Matrices (+ prefix and monotonous stack)

Topic link: https: //ac.nowcoder.com/acm/contest/888/A

That Italy: 01 to the n × m matrix, the matrix is ​​obtained in which the whole number of 1 is (1 other full matrix is ​​not included).

Ideas: the second field and the second largest sub-matrix multi-school similar. A two-dimensional one-dimensional reduction, through the rows, the behavior of the i-th matrix to bottom, left to right to obtain a matrix to extend the scope of l [j] and r [j] by a monotone stack, and then key in the de-emphasis. Repeat two, first row of the matrix is ​​the same, may be two-dimensional matrix vis [l [j]] [r [j]] of the matrix of this range mark has been recorded. For repeated column it is because the matrix can be expanded in the next line to get the line, so to obtain pre-prefix '1' and the number of the next row of num [j], is determined by the num [r [j] -1] - num [l [j]] == r [j] -l [j] -1 to determine whether the line can be extended to the next, it can not currently recorded.

AC Code:

#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn=3005;
int n,m,ans;
int a[maxn][maxn],stk1[maxn],stk2[maxn];
int l[maxn],r[maxn],num[maxn],vis[maxn][maxn];
char c[maxn][maxn];

int main(){
    scanf("%d%d",&n,&m);
    for(register int i=1;i<=n;++i){
        a[i][0]=a[i][m+1]=-1;
        scanf("%s",c[i]+1);
        for(register int j=1;j<=m;++j){
            register int tmp=c[i][j]-'0';
            if(tmp) a[i][j]=a[i-1][j]+1;
        }
    }
    stk1[0]=0,stk2[0]=m+1;
    for(register int i=1;i<=n;++i){
        register int p1=1,p2=1;
        for(register int j=1;j<=m;++j){
            while(a[i][stk1[p1-1]]>=a[i][j]) --p1;
            l[j]=stk1[p1-1];
            stk1[p1++]=j;
        }
        for(register int j=m;j>=1;--j){
            while(a[i][stk2[p2-1]]>=a[i][j]) --p2;
            r[j]=stk2[p2-1];
            stk2[p2++]=j;
        }
        for(int j=1;j<=m;++j)
            num[j]=num[j-1]+(c[i+1][j]=='1');
        for(register int j=1;j<=m;++j){
            if(!a[i][j]) continue;
            if(vis[l[j]+1][r[j]-1]==i) continue;
            if(num[r[j]-1]-num[l[j]]==r[j]-l[j]-1) continue;
            ++ years; 
            am [t [j] + 1 ] [r [j] - 1 ] = i; 
        } 
    } 
    Printf ( " % d \ n " , year);
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11333189.html