P2659 美丽的序列 [单调栈]

编程入门 行业动态 更新时间:2024-10-27 15:28:50

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

P2659 美丽的序列 [单调栈]

传送门

水题

#include<bits/stdc++.h>
#define N 2000050
#define LL long long
using namespace std;
int n,a[N],l[N],r[N],sta[N],top; LL ans;
int read(){int cnt=0;char ch=0;while(!isdigit(ch))ch=getchar();while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();return cnt;
}
int main(){n=read();for(int i=1;i<=n;i++) a[i]=read();for(int i=1;i<=n;i++){while(top && a[sta[top]] >= a[i]) top--;if(top==0) l[i] = 0;else l[i] = sta[top];sta[++top] = i;} top=0;for(int i=n;i>=1;i--){while(top && a[sta[top]] >= a[i]) top--;if(top==0) r[i] = n+1;else r[i] = sta[top];sta[++top] = i;}for(int i=1;i<=n;i++) ans = max(ans,(LL)a[i]*(r[i]-l[i]-1));printf("%lld",ans); return 0;
}

 

更多推荐

P2659 美丽的序列 [单调栈]

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

发布评论

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

>www.elefans.com

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