P2659 美丽的序列 (单调栈)

题目链接

Solution

直接考虑单调栈处理出每一个点作为最小值的区间长度.
然后 \(O(n)\) 找一遍最大值即可. 记得开 long long,以及要注意 \(0\) 的问题.

Code

#include<bits/stdc++.h>
#define ll long long
#define N 2000001
#define in(x) x=read()
using namespace std;

ll n,a[N];
ll L[N],R[N],sta[N],top;

ll read()
{
    char ch=getchar();ll f=1,w=0;
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){w=w*10+ch-'0';ch=getchar();}
    return f*w;
}

int main()
{
    in(n);
    for(ll i=1;i<=n;i++) in(a[i]);
    for(ll i=1;i<=n;i++)
    {
        if(a[i]==0){L[i]=i;continue;}
        if(a[sta[top]]<a[i]){sta[++top]=i,L[i]=i-1;continue;}
        while(a[sta[top]]>=a[i])top--;
        L[i]=sta[top]; sta[++top]=i;
    }top=0;
    for(ll i=n;i>=1;i--)
    {
      if(a[i]==0){R[i]=i;continue;}
      if(a[sta[top]]<a[i]){sta[++top]=i,R[i]=i+1;continue;}
      while(a[sta[top]]>=a[i])top--;
      R[i]=sta[top]; if(top==0)R[i]=n+1;
      sta[++top]=i;
    }
    ll ans=0;
    for(ll i=1;i<=n;i++)
    //cout<<L[i]<<' '<<R[i]<<endl;
    ans=max(ans,a[i]*(R[i]-L[i]-1));
    cout<<ans<<endl;

}

转载于:https://www.cnblogs.com/Kv-Stalin/p/9849765.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值