bzoj3039 玉蟾宫【单调栈】

解题思路:

枚举每一行作为矩形的底,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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值