解题思路:
枚举每一行作为矩形的底,h[j]表示第j列向上延伸的最多的1,再用两个单调栈维护每列向左和向右第一个h值比它小的列l[j],r[j],那么矩形面积即是h[j]*(r[j]-l[j]-1)。可以证明最优解一定会被统计。
时间复杂度为
O(n2)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int getint()
{
int i=0,f=1;char c;
for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());
if(c=='-')f=-1,c=getchar();
for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
return i*f;
}
const int N=1005;
int n,m,ans;
int a[N][N],h[N],l[N],r[N],sta[N];
char s[2];
int main()
{
//freopen("lx.in","r",stdin);
n=getint(),m=getint();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
scanf("%s",s);
a[i][j]=s[0]=='F'?1:0;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
h[j]=a[i][j]?h[j]+1:0;
sta[0]=0;
for(int j=1,top=0;j<=m;j++)
{
while(top&&h[j]<=h[sta[top]])top--;
l[j]=sta[top]+1,sta[++top]=j;
}
sta[0]=m+1;
for(int j=m,top=0;j;j--)
{
while(top&&h[j]<=h[sta[top]])top--;
r[j]=sta[top]-1,sta[++top]=j;
}
for(int j=1;j<=m;j++)
ans=max(ans,h[j]*(r[j]-l[j]+1));
}
cout<<ans*3<<'\n';
return 0;
}