您的位置:首页 > 其它

bzoj3039 玉蟾宫【单调栈】

2017-12-18 16:15 232 查看

解题思路:

枚举每一行作为矩形的底,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
<
4000
span class="hljs-keyword">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

,h
,l
,r
,sta
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: