P2659 美丽的序列 (单调栈)
2024-08-29 19:36:46
题目链接
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;
}
最新文章
- Alljoyn瘦客户端库介绍(官方文档翻译)
- Hp自学整理html
- java12-6 冒泡排序法和选择排序法
- hadoop 序列化源码浅析
- python正则表达式例子说明
- BZOJ:4816: [Sdoi2017]数字表格
- springboot 返回json字符串格式化问题
- DRF框架之 用户角色权限与访问频率的权限设置
- MySQL-查询结果缓存
- 第二章 STM32的结构和组成
- 推荐一个好工具:P/Invoke Interop Assistant【转】
- 安卓constraintLayout中app:srcCompat设置的图片显示不出来
- Windows下修改Tomcat黑窗口标题
- 深入理解 mysql 索引
- WebSlides - 轻松制作漂亮的 HTML 幻灯片(演讲稿)
- asserts文件存到外部SD卡里
- JDBC事务提交/回滚实例
- Matlab——filter函数用法
- B. Vile Grasshoppers
- RocketMQ之NameServer学习笔记