poj 3494 Largest Submatrix of All 1’s

来源:互联网 发布:db2端口 编辑:程序博客网 时间:2024/05/07 13:59
Largest Submatrix of All 1’s
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 4903 Accepted: 1828Case Time Limit: 2000MS

Description

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

Input

The input contains multiple test cases. Each test case begins with m andn (1 ≤ m, n ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order onm 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 20 00 04 40 0 0 00 1 1 00 1 1 00 0 0 0

Sample Output

04

这道题也是单调栈的应用,一开始不太容易看出来,这里枚举每一行,以该行为底边能够组成的最大1矩阵。这下题目就变成了矩阵最大面积。
具体参考:题解     矩阵最大面积:poj 2559

代码:
#include<cstdio>#include<iostream>#define Maxn 2010using namespace std;int st[Maxn],idx[Maxn],a[Maxn][Maxn];int dstack(int x,int n){    int top=-1,maxx=0,ans;    for(int i=0;i<=n;i++){        int id=i;        while(top!=-1&&st[top]>a[x][i]){            id=idx[top];            ans=(i-id)*st[top--];            maxx=max(maxx,ans);        }        st[++top]=a[x][i];        idx[top]=id;    }    return maxx;}int main(){    int n,m;    while(~scanf("%d%d",&n,&m)){        for(int i=0;i<n;i++)            for(int j=0;j<m;j++)                scanf("%d",&a[i][j]);        for(int i=0;i<n;i++)            a[i][n]=-1;        for(int i=n-2;i>=0;i--)            for(int j=0;j<n;j++)                if(a[i][j]) a[i][j]+=a[i+1][j];        int res=0;        for(int i=0;i<n;i++)            res=max(res,dstack(i,n));        printf("%d\n",res);    }return 0;}


0 0