More than 2019 cattle off summer school camp (Session 8) A.All-one Matrices (dp)

Meaning of the questions: is the largest of the matrix model 01 is the number of time looking for a great 0/1 Matrix

Ideas: We like to handle the maximum 01 matrix because we did something about the border on the left and right can no longer continue to expand and we only need to prefix the record about whether you can extend downward (ie, below the current judgment about whether the border has 1),

But also with an array of tags recorded about the same matrix

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int N = 3e3+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
typedef long long ll;
const ll mod = 1e7+9;
int n,m;
int h[N][N],sum[N][N],l[N],r[N];
bool jug[N][N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            char a; cin>>a;
            if(a=='1') sum[i][j]=sum[i][j-1]+1,h[i][j]=h[i-1][j]+1;
            else sum[i][j]=sum[i][j-1],h[i][j]=0;
        }
    int ans=0;
    for(int i=1;i<=n;i++){
        l[0]=1,r[m+1]=m;
        h[i][0]=h[i][m+1]=-1;
        for(int j=1;j<=m;j++){
            l[j]=j;
            while(h[i][l[j]-1]>=h[i][j])
                l[j]=l[l[j]-1];
        }
        for(int j=m;j>=1;j--){
            r[j]=j;
            while(h[i][r[j]+1]>=h[i][j])
                r[j]=r[r[j]+1];
        }
        for(int j=1;j<=m;j++){
            if(!h[i][j]||jug[l[j]][r[j]]) continue;
            if(sum[i+1][r[j]]-sum[i+1][l[j]-1]!=r[j]-l[j]+1){
                ans++;
                jug[l[j]][r[j]]=1;
            }
        }
        for(int j=1;j<=m;j++) jug[l[j]][r[j]]=0;
    }
    cout<<ans<<endl;
}

 

Guess you like

Origin www.cnblogs.com/wmj6/p/11334923.html