您的位置:首页 > 其它

poj 3494 Largest Submatrix of All 1’s

2014-08-20 15:17 435 查看
Largest Submatrix of All 1’s

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 4903 Accepted: 1828
Case Time Limit: 2000MS
Description

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


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


代码:
#include<cstdio>
#include<iostream>
#define Maxn 2010
using 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]
=-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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: