poj3494 Largest Submatrix of All 1’s (最大全1矩阵 单调栈)

Largest Submatrix of All 1’s

Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements.

Input

The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on m lines each with n numbers. The input ends once EOF is met.

Output

For each test case, output one line containing the number of elements of the largest submatrix of all 1’s. If the given matrix is of all 0’s, output 0.

Sample Input

2 2
0 0
0 0
4 4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0

Sample Output

0
4

分析:

前置技能:poj2559
在这里插入图片描述
转化成:
在这里插入图片描述

在这里插入图片描述
就变成poj2559求最大矩形面积了,对每行都求一次最大矩形面积,更新答案就行了

code1:

#include
#include
#include
#include
#include
using namespace std;
const int maxm=2e3+5;
int a[maxm];
int n,m;
int stk[maxm];
int l[maxm],r[maxm];
signed main(){
    while(scanf("%d%d",&m,&n)==2){
        for(int i=1;i<=n;i++)a[i]=0;
        a[0]=a[n+1]=-1;
        int ans=0;
        for(int k=1;k<=m;k++){
            for(int i=1;i<=n;i++){
                int t;
                scanf("%d",&t);
                if(t){
                    a[i]++;
                }else{
                    a[i]=0;
                }
            }
            int head=0;
            for(int i=1;i<=n+1;i++){
                while(head&&a[i]<a[stk[head]]){
                    r[stk[head]]=i-1;
                    head--;
                }
                stk[++head]=i;
            }
            head=0;
            for(int i=n;i>=0;i--){
                while(head&&a[i]<a[stk[head]]){
                    l[stk[head]]=i+1;
                    head--;
                }
                stk[++head]=i;
            }
            for(int i=1;i<=n;i++){
                ans=max(ans,(r[i]-l[i]+1)*a[i]);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

code2:

因为有修改原数组的操作,所以再开一个数组存数据

#include
#include
#include
#include
#include
using namespace std;
const int maxm=2e3+5;
int h[maxm],a[maxm];
int stk[maxm];
int n,m;
signed main(){
    while(scanf("%d%d",&m,&n)!=EOF){
    	for(int i=1;i<=n;i++)h[i]=0;
        int ans=0;
        a[n+1]=-1;
        for(int k=1;k<=m;k++){
            for(int i=1;i<=n;i++){
                int t;
                scanf("%d",&t);
                if(t){
                    h[i]++;
                }else{
                    h[i]=0;
                }
                a[i]=h[i];
            }
            int head=0;
            for(int i=1;i<=n+1;i++){
                if(head==0||a[i]>=a[stk[head]]){
                    stk[++head]=i;
                }else{
                    int last=i;
                    while(head&&a[i]<a[stk[head]]){
                        last=stk[head];
                        head--;
                        ans=max(ans,(i-last)*a[last]);
                    }
                    stk[++head]=last;
                    a[last]=a[i];
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(单调栈)