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 ...
随机推荐
- [UE4]模拟物理
一.默认情况下Actor的Mobility(可移动性)是Static(静止),Simulate Physics(模拟物理):如果勾选Simulate Physics,则Mobility会自动变成Mov ...
- [UE4]显示落地箭头
一.Set Hidden in Game:隐藏对象 Propagate to Children:是否修改容器子对象的可见性. 二.添加2个Static Mesh,分别命名为:StaitcMeshArr ...
- connected standby
参考链接 https://docs.microsoft.com/en-us/windows-hardware/design/device-experiences/modern-standby http ...
- JAVA之Mybatis基础入门二 -- 新增、更新、删除
上一节说了Mybatis的框架搭建和简单查询,这次我们来说一说用Mybatis进行基本的增删改操作: 一. 插入一条数据 1.首先编写USER.XML(表的xml)使用insert元素,元素写在map ...
- Tomcat修改service.xml性能调优 增加最大并发连接数
详细配置: <Connector executor="tomcatThreadPool" port="80" protocol ...
- 服务注册中心,Eureka比Zookeeper好在哪里?
著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性).A(可用性).和P(分区容错性).由于分区容错性P在分布式系统中必须要保证的,因此我们只能在A和C之间进行权衡. 因此: Zookeep ...
- 升级openssl 操作记录
openssl 是一群黑客最爱研究搞怪的一个软件为啥,据说openssl是一群数学家编写的一套算法 哈哈 好,说正事 openssl 经常发布补丁包,因为升级是避免不了的 步骤: 查看当前openss ...
- 解决wxParse空格不解析的问题
遇到的问题: 相似问题:https://blog.csdn.net/qq_41619741/article/details/85774865 http://html51.com/info-41786- ...
- gentoo virtual couldnt download
今天在更新系统的时候,提示 virtualbox-bin 从原始地址下载不了,经过一番摸索,通过下面的方式即可正常安装. http://download.virtualbox.org/virtualb ...
- jersey 用FastJson替换掉默认的Jackson
@Bean public ResourceConfig resourceConfig() { ResourceConfig resourceConfig = new ResourceConfig(); ...