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<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
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<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值