基准时间限制:1 秒 空间限制:131072 KB 分值: 80
难度:5级算法题
给出1个M*N的矩阵M1,里面的元素只有0或1,找出M1的一个子矩阵M2,M2中的元素只有1,并且M2的面积是最大的。输出M2的面积。
Input
第1行:2个数m,n中间用空格分隔(2 <= m,n <= 500) 第2 - N + 1行:每行m个数,中间用空格分隔,均为0或1。
Output
输出最大全是1的子矩阵的面积。
Input示例
3 3 1 1 0 1 1 1 0 1 1
Output示例
4
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cmath>
#include<string>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4+10;
char str[1100][1100];
int sum[1100][1100], d[1010][1010];
struct node
{
int id, x;
};
stack<node>st;
int main()
{
int n, m, x;
scanf("%d %d", &n, &m);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
scanf("%d", &x);
d[i][j]=x;
}
}
int ans=0;
memset(sum,0,sizeof(sum));
for(int i=1; i<=n; i++)
{
if(i==6)
{
int h=1;
}
while(!st.empty()) st.pop();
for(int j=1; j<=m+1; j++)
{
if(d[i][j]) sum[i][j]=sum[i-1][j]+1;
int id=-1;
while(!st.empty()&&st.top().x>sum[i][j])
{
ans=max(ans,st.top().x*(j-st.top().id));
id=st.top().id;
st.pop();
}
if(st.empty()) ans=max(ans,sum[i][j]*j);
if(id==-1) id=j;
st.push((node)
{
id,sum[i][j]
});
}
}
cout<<ans<<endl;
return 0;
}