[第1章 1.2t5] 良好的感觉

113 字
1 分钟
[第1章 1.2t5] 良好的感觉
//单调栈问题注意2个stack与a[n]哨兵
//前一个确保top可访问,并且初始位置可被计算
//第二个确保最后全部位置都被清算,这里设计的很棒
//注意要在本轮res计算完成后push
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll n;
cin>>n;
vector<ll> a(n+2,0),sum(n+2,0),res(n+2,0);
for(ll i=1;i<=n;i++)cin>>a[i];
n++;
stack<ll> st;
st.push(0);
ll ans=0;
for(ll i=1;i<=n;i++){
sum[i]=sum[i-1]+a[i];
while(!st.empty()&&a[st.top()]>a[i]){
ll tp=st.top();
st.pop();
res[tp]+=(sum[i-1]-sum[tp]);
}
res[i]=(sum[i]-sum[st.top()]);
st.push(i);
}
for(ll i=1;i<=n;i++){
ans=max(ans,res[i]*a[i]);
}
cout<<ans<<endl;
return 0;
}

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
[第1章 1.2t5] 良好的感觉
https://hecloud.top/posts/12t5-良好的感觉/
作者
贺小云
发布于
2026-06-26
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
贺小云
一个热爱技术与折腾的博客,serverless起高楼,静态构建一键走,专注于CDN调优,2秒之内到德州。
公告
欢迎来到我的博客!这是一则示例公告。
分类
标签
碎碎念
站点统计
文章
46
分类
4
标签
0
总字数
23,903
运行时长
0
最后活动
0 天前
站点信息
构建平台
Local
博客版本
Firefly v6.16.5
文章许可
CC BY-NC-SA 4.0

当前页面没有目录