Luogu P2659 美丽的序列

编程入门 行业动态 更新时间:2024-10-28 01:25:59

Luogu P2659 美丽的<a href=https://www.elefans.com/category/jswz/34/1769864.html style=序列"/>

Luogu P2659 美丽的序列

题链

很容易想到,对于每个数,

先求出最后一个在原来的数左边的比它小的数,

再求出第一个在原来的数右边的比它小的数

然后循环统计答案

 

问题就变成了如何求最后一个在原来数左边比它小的

 

可以发现具有单调性:设存在,满足,则可舍去

所以用单调的数据结构维护(队列、栈……)

 

这道题不用维护头,所以用栈维护

记得开long long

 

#include<cstdio>
#include<iostream>
#define ll long long
using namespace std;void read(int &x)
{x=0;char ch=getchar();while(ch<'0'||ch>'9') ch=getchar();while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
}const int N=2e6+5;
int n,a[N],top,l[N],r[N],q[N];
ll ans;int main()
{read(n);for(int i=1;i<=n;i++)read(a[i]);top=0;a[0]=-1,a[n+1]=-1;for(int i=0;i<=n;i++){while(top&&a[q[top]]>=a[i]) top--;l[i]=!top?i-1:q[top];q[++top]=i;}top=0;for(int i=n+1;i;i--){while(top&&a[q[top]]>=a[i]) top--;r[i]=!top?i+1:q[top];q[++top]=i;}ans=0;for(int i=1;i<=n;i++)ans=max(ans,(ll)a[i]*(r[i]-l[i]-1));printf("%lld\n",ans);return 0;
}

 

更多推荐

Luogu P2659 美丽的序列

本文发布于:2024-02-11 23:47:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1684376.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   美丽   Luogu

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!