[洛谷P4147] 玉蟾宫(悬线法、单调栈)

https://www.luogu.org/problemnew/show/P4147

滚动数组+悬线法

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#define ll long long
#define pb push_back
#define test printf("here!!!")
using namespace std;
const int mx=2000+10;
int mp[mx][mx];
int h[mx];
int l[mx];
int r[mx];
int lo,ro;
int maxn=0;
int getc()
{
    char c=getchar();
    while (c!='F'&&c!='R') c=getchar();
    return c=='F'?1:0;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;++i)
    {
        for (int j=1;j<=m;++j)
        {
            mp[i][j]=getc();
        }
    }
    for (int i=1;i<=m;++i) l[i]=1,r[i]=m;
    for (int i=1;i<=n;++i)
    {
        lo=0;
        for (int j=1;j<=m;++j)
        {
            if (mp[i][j]^1)
            {
                lo=j;
                h[j]=l[j]=0;
            }
            else
            {
                h[j]++;
                l[j]=max(lo+1,l[j]);
            }
        }
        ro=m+1;
        for (int j=m;j>=1;--j)
        {
            if (mp[i][j]^1)
            {
                ro=j;
                r[j]=m+1;
            }
            else
            {
                r[j]=min(ro-1,r[j]);
                maxn=max(maxn,h[j]*(r[j]-l[j]+1));
            }
        }
        //printf("%d %d %d  \n",h[5],l[5],r[5]);
    }
    printf("%d\n",3*maxn);
}

单调栈+二维

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#define ll long long
#define pb push_back
#define test printf("here!!!")
using namespace std;
const int mx=2000+10;
int mp[mx][mx];
int h[mx];//记录行高度,同悬线法的up/h的滚动数组
struct Node
{
    int h;//记录高度
    int pos;//记录下标
}ddz[mx];//模拟单调栈
int maxn=0;
int getc()
{
    char c=getchar();
    while (c!='F'&&c!='R') c=getchar();
    return c=='F'?1:0;
}
int cnt;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;++i)
    {
        for (int j=1;j<=m;++j)
        {
            mp[i][j]=getc();
        }
    }
    for (int i=1;i<=n;++i)
    {
        cnt=0;
        h[m+1]=-1;//最后插入一个高度为-1的来清空栈,保证最后一个也算过
        for (int j=1;j<=m+1;++j)
        {
            if (j!=m+1)
            {
                if (mp[i][j]==1) h[j]++;
                else h[j]=0;
            }//每行滚动高度
            if (!cnt) ddz[++cnt].pos=j,ddz[cnt].h=h[j];//空栈直接插入
            else if (h[j]>ddz[cnt].h)//大于栈顶直接插入
            {
                ddz[++cnt].pos=j;
                ddz[cnt].h=h[j];
            }
            else
            {
                int tp=j;
                while (cnt&&h[j]<=ddz[cnt].h)//小于等于栈顶,持续弹出
                {
                    maxn=max(maxn,ddz[cnt].h*(j-ddz[cnt].pos));//宽度为(j-ddz[cnt].pos),意味着pos的高度可以向右延伸到j-1这里,注意在这里[pos,j-1]都是单调递增的
                    tp=ddz[cnt].pos;//注意这里,意味着新插进来的这块预计可以扩展到.pos这里,因为[pos,j-1]的高度都比h[j]高,所以以h[j]的高度向左可以延伸到pos这里
                    cnt--;
                }
                ddz[++cnt].pos=tp;//插入
                ddz[cnt].h=h[j];
            }
        }
    }
    printf("%d\n",3*maxn);
}

单调栈+二维 参考博客:
https://blog.csdn.net/hopeztm/article/details/7868581
https://blog.csdn.net/sweeterer/article/details/52053962

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值