其他分享
首页 > 其他分享> > poj3494Largest Submatrix of All 1’s

poj3494Largest Submatrix of All 1’s

作者:互联网

Largest Submatrix of All 1’s
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 9943   Accepted: 3538
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

Source

POJ Founder Monthly Contest – 2008.01.31, xfxyjwf   #include<iostream>
#include<cstring>
#include<stack>
#include<cstdio>
using namespace std;
const int N = 2e3+50;

int mp[N][N], dp[N][N];
int main()
{
    int m,n;
    while(scanf("%d%d",&n,&m)!=EOF){
    int ans=0;
    memset(dp,0,sizeof(dp));
    //预处理
    for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
    scanf("%d",&mp[i][j]);
    if(mp[i][j]==1)
    dp[i][j]=dp[i-1][j]+1;
}
dp[i][0]=dp[i][m+1]=-1;
}
for(int i=1;i<n;++i){
stack<int> s;
int r[N];
s.push(m+1);
for(int j=m;j;--j){
while(dp[i][s.top()]>=dp[i][j]) s.pop();
r[j]=s.top();
s.push(j);
}
while(!s.empty())s.pop();
s.push(0);
for(int j=1;j<=m;++j){
while(dp[i][s.top()]>=dp[i][j])s.pop();
ans=max(ans,dp[i][j]*(r[j]-s.top()-1));
s.push(j);
}
}
printf("%d\n",ans);
}
}

标签:int,top,Submatrix,poj3494Largest,while,ans,push,dp
来源: https://www.cnblogs.com/fanyu1/p/12423090.html