Max answer(单调栈+ST表)
Max answer
https://nanti.jisuanke.com/t/38228
Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values in the interval, multiplied by the smallest value in the interval.
Now she is planning to find the max value of the intervals in her array. Can you help her?
Input
First line contains an integer n(1 \le n \le 5 \times 10 ^5n(1≤n≤5×105).
Second line contains nn integers represent the array a (-10^5 \le a_i \le 10^5)a(−105≤ai≤105).
Output
One line contains an integer represent the answer of the array.
样例输入复制
5
1 2 3 4 5
样例输出复制
36
题意:给定n个数,求 区间最小值*区间和 的值最大
思路:先求前缀和,然后用st表求出区间最大最小值,再用单调栈求出每个数的左右边界,然后枚举最小值和它的区间和即可
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
#define maxn 500005
int n;
ll sum[maxn],R[maxn],L[maxn],a[maxn],Max[maxn][],Min[maxn][]; ll queryMax(int x,int y){
int k=log2(y-x+);
return max(Max[x][k],Max[y-(<<k)+][k]);
} ll queryMin(int x,int y){
int k=log2(y-x+);
return min(Min[x][k],Min[y-(<<k)+][k]);
} int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
sum[i]=sum[i-]+a[i];
Max[i][]=sum[i];
Min[i][]=sum[i];
}
for(int i=;i<;i++){
for(int j=;j+(<<i)-<=n;j++){
Max[j][i]=max(Max[j][i-],Max[j+(<<(i-))][i-]);
Min[j][i]=min(Min[j][i-],Min[j+(<<(i-))][i-]);
}
} stack<int>st;
for(int i=;i<=n;i++){
while(!st.empty()&&a[i]<=a[st.top()]){
st.pop();
}
if(st.empty()){
L[i]=;
}
else{
L[i]=st.top()+;
}
st.push(i);
}
while(!st.empty()) st.pop();
for(int i=n;i>=;i--){
while(!st.empty()&&a[i]<=a[st.top()]){
st.pop();
}
if(st.empty()){
R[i]=n;
}
else{
R[i]=st.top()-;
}
st.push(i);
}
ll ans=-0x3f3f3f3f3f3f3f3f;
for(int i=;i<=n;i++){
if(a[i]<){
ll minr=queryMin(i,R[i]);
ll maxl=queryMax(L[i]-,i);
ans=max(ans,(minr-maxl)*a[i]);
}
else if(a[i]>){
ll maxr=queryMax(i,R[i]);
ll minl=queryMin(L[i]-,i);
ans=max(ans,(maxr-minl)*a[i]);
}
else{
ans=max(ans,0LL);
}
}
printf("%lld\n",ans);
}
Max answer(单调栈+ST表)的更多相关文章
- BZOJ4199 [Noi2015]品酒大会 【后缀数组 + 单调栈 + ST表】
题目 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发"首席品 酒家"和"首席猎手"两个奖项,吸 ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈
题面 题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值 题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok 我们 ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)
题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...
- 南昌邀请赛I.Max answer 单调栈+线段树
题目链接:https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value of a in ...
- BZOJ3879:SvT(后缀数组,单调栈,ST表)
Description (我并不想告诉你题目名字是什么鬼) 有一个长度为n的仅包含小写字母的字符串S,下标范围为[1,n]. 现在有若干组询问,对于每一个询问,我们给出若干个后缀(以其在S中出现的起始 ...
- 网络赛 I题 Max answer 单调栈+线段树
题目链接:https://nanti.jisuanke.com/t/38228 题意:在给出的序列里面找一个区间,使区间最小值乘以区间和得到的值最大,输出这个最大值. 思路:我们枚举每一个数字,假设是 ...
- 2019ICPC南昌邀请赛网络赛 I. Max answer (单调栈+线段树/笛卡尔树)
题目链接 题意:求一个序列的最大的(区间最小值*区间和) 线段树做法:用单调栈求出每个数两边比它大的左右边界,然后用线段树求出每段区间的和sum.最小前缀lsum.最小后缀rsum,枚举每个数a[i] ...
- BZOJ 4540 [Hnoi2016]序列 (单调栈 + ST表 + 莫队算法)
题目链接 BZOJ4540 考虑莫队算法. 这题难在$[l, r]$到$[l, r+1]$的转移. 根据莫队算法的原理,这个时候答案应该加上 $cal(l, r+1) + cal(l+1, r+1) ...
- [bzoj4540][Hnoi2016][序列] (莫队算法+单调栈+st表)
Description 给定长度为n的序列:a1,a2,…,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是指序列:al,al+1,…,ar-1,ar.若1≤l≤s≤t≤r≤n,则称a ...
随机推荐
- Windows计划任务实现MYSQL冷备份
BAT代码 @ECHO OFF for %%i in (%0) do (set "name=%%~ni") title %name% set targetsql="%~d ...
- php变量函数
这个东西相当于C语言中的函数指针,C#里的委托 function come() { //定义com函数 echo "来了<p>" ...
- let const区别!
这次做项目在申明变量的时候用到let const 总结下这两个区别 : 首先 let与const都是只在声明所在的块级作用域内有效. let声明的变量可以改变,值和类型都可以改变,没有限制.const ...
- ct
b80e00u9dxwpqw7bt98rm5zmlxt08cxs A3WKXKBHWDUOEOP3EVJA2YRM6JSZPJWGTCQ5BSYAWI4GMSIXOAT2IQ
- [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher 问题--MyEclipse设置JDK版本
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML doc ...
- VUE插件总结
亲们支持我的新博客哦==>地址(以后更新会尽量在新博客更新,欢迎大家访问加入我的后宫w) ) UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和W ...
- 关于Unsupported major.minor version 52.0解决方案的补充
参考:https://blog.csdn.net/jingtianyiyi/article/details/80455916 补充: 这个设置比较容易忽略: 在eclipse中新建tomcat或在原有 ...
- Android 开发 PopupWindow弹窗
简介 PopupWindow,顾名思义弹窗.PopupWindow是与AlertDialog在形式上类似的弹窗功能,都是为了在activity最上层显示一个弹窗.但是区别是PopupWindow可以自 ...
- 2018-2019-2 网络对抗技术 20164313 杜桂鑫 Exp1 PC平台逆向破解
实验目标: 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 实践的目标就是运行程序中另一代码片段ge ...
- linux中ip命令使用介绍
ifconfig是CentOS 5.6系统中经典的配置网络的命令,但是到了CentOS 7的时候,命令就变成了ip了,我们也要学习,我们也要与时俱进.跟随高手一起学习 查看链路 ip link sho ...