题意
给定一个01矩阵,求面积最大的全1矩阵
分析
先求出对于点
(
i
,
j
)
(i,j)
(i,j)向上全是1最长距离
f
i
,
j
f_{i,j}
fi,j
那么我们对于每一行做一次dp
维护 f f f的递增单调栈,每次弹出后更新一下答案
每次计算的是以新加入点作为结尾的面积,高于当前高度的在以后的计算中一定不产生影响,所以弹出
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2005;
int n,m;
int f[maxn][maxn];
int st[maxn],top;
int len[maxn],h[maxn];
int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
scanf("%d%d",&n,&m);
char cc;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
cin>>cc;
if(cc=='F') f[i][j]=f[i-1][j]+1;
else f[i][j]=0;
}
int ans=0;
for(int i=1;i<=n;i++)
{
top=1; len[1]=1; h[1]=f[i][1];
ans=max(ans,h[1]);
for(int j=2;j<=m;j++)
{
int w=0;
while(top && h[top]>=f[i][j])
{
w+=len[top];
ans=max(ans,w*h[top]);
top--;
}
top++; h[top]=f[i][j]; len[top]=w+1;
}
int w=0;
while(top)
{
w+=len[top];
ans=max(ans,w*h[top]);
top--;
}
}
printf("%d",ans*3);
return 0;
}