A. All-one Matrices 2019 cattle off summer school more than an eighth field (monotonous stack)

A. All-one Matrices

Meaning of the questions: can not seek re-increase the maximum number of sub-matrix

Ideas: monotonous stack problem. Here we see a simple graph can know when the height of the i-th column to the right than i, the cell block column i is contained therein, when the height is less than will not be included.

Up value for each stack, but also need to maintain a farthest position Left can expand it to the left

Whenever a unstack element, provided de-stacked elements (Up, Left), you can get a full matrix 1 (i-Up + 1, Left) - (i, j)

This matrix can then be determined larger rectangle nested

code:

#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int maxn=3005; char a[maxn][maxn]; int up[maxn][maxn]; stack<pii> s; int n,m; int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%s",a[i]+1); } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++) up[i][j] = a[i][j]-'0'?up[i-1][j]+a[i][j]-'0':0; } int ans=0; for(int i=1;i<=n;i++){ int tmp=-1; while(!s.empty()) s.pop(); for(int j=1;j<=m+1;j++){ int pos=j; while(!s.empty()&&s.top().first>up[i][j]){ if(s.top().second<=tmp) ans++; pos=s.top().second; s.pop(); } if(!up[i+1][j]) tmp=j; if(up[i][j]&&(s.empty()||s.top().first<up[i][j])) s.push(make_pair(up[i][j],pos)); } } printf("% d \ n " , year);}

Guess you like

Origin www.cnblogs.com/Tianwell/p/11372805.html