序列"/>
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 美丽的序列
发布评论