南昌网络赛 I. Max answer 单调栈
Max answer
题目链接
https://nanti.jisuanke.com/t/38228
Describe
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≤n≤5*10^5)
Second line contains n integers represent the array a(−10^5≤ai≤10^5)
Output
One line contains an integer represent the answer of the array.
样例输入
5
1 2 3 4 5
样例输出
36
题意
给你一个序列,对于每个连续子区间,有一个价值,等与这个区间和×区间最小值,
求所有子区间的最大价值是多少。
题解
a[i]表示第i位的权值,L[i]表示第i个数左边第一个比他小的数,R[i]表示右边第一个比它小的数。
我们可以用单调站求出来,不会可以先做洛谷的完美序列, https://www.luogu.org/problemnew/show/P2659
然后对于每一位i,如果a[i]为正数,我们可以求包含i的区间和最大的子区间,然后再乘上这个值,当然不能越过L[i]和R[i]。
我们怎么求呢?
我们先定义sum(i,j)为i到j的区间和,再定义lmax[i]为以i 为右端点的使得区间和和最大的左端点位置,(即求一个l<=i,使得sum(l,i)最大)。
那么,lmax[i]可以通过lmax[i-1]更新,这个就是一个贪心吧,分两种情况:
1.sum(lmax[i-1],i-1)<0, 那么lmax[i]=i.
2.sum(lmax[i-1],i-1)>0,lmax[i+1]=lmax[i].
相似的我们可以求出lmax[i],lmin[i],rmax[i]。对于i ,若a[i]>0,其最大贡献就是sum(max(L[i]+1,lmax[i]),min(R[i]-1,rmax[i])*a[i]。负数类似。
这样题目就做完啦,时间复杂度O(n)
代码:
#include<bits/stdc++.h>
using namespace std;
#define N 3000050
#define INF 0x7f7f7f7f
#define ll long long
template<typename T>void read(T&x)
ll n,a[N],s[N],sk[N],L[N],R[N];
ll Rmin[N],Lmin[N],Rmax[N],Lmax[N],ans,top;
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
ios::sync_with_stdio(false);
cin>>n;
for(ll i=;i<=n;i++)cin>>a[i],s[i]=a[i]+s[i-];
ans=a[]*a[];
sk[top=]=;
a[]=-INF-;
a[n+]=-INF;
for(ll i=;i<=n+;i++)
{
while(a[i]<a[sk[top]])
R[sk[top--]]=i;
L[i]=sk[top];
sk[++top]=i;
}
Lmin[]=;
Lmax[]=;
Rmin[n]=n;
Rmax[n]=n;
for(ll i=;i<=n;i++)
{
if (s[i-]-s[Lmin[i-]-]>)Lmin[i]=i;
else Lmin[i]=Lmin[i-];
if (s[i-]-s[Lmax[i-]-]<)Lmax[i]=i;
else Lmax[i]=Lmax[i-];
}
for(ll i=n-;i>=;i--)
{
if (s[Rmin[i+]]-s[i]>)Rmin[i]=i;
else Rmin[i]=Rmin[i+];
if (s[Rmax[i+]]-s[i]<)Rmax[i]=i;
else Rmax[i]=Rmax[i+];
}
ll l,r;
for(ll i=;i<=n;i++)
{
if (a[i]<)
{
l=max(L[i]+,Lmin[i]);
r=min(R[i]-,Rmin[i]);
}
else
{
l=max(L[i]+,Lmax[i]);
r=min(R[i]-,Rmax[i]);
}
ans=max((s[r]-s[l-])*a[i],ans);
}
cout<<ans;
}
南昌网络赛 I. Max answer 单调栈的更多相关文章
- 2019ICPC南昌邀请赛网络赛 I. Max answer (单调栈+线段树/笛卡尔树)
题目链接 题意:求一个序列的最大的(区间最小值*区间和) 线段树做法:用单调栈求出每个数两边比它大的左右边界,然后用线段树求出每段区间的和sum.最小前缀lsum.最小后缀rsum,枚举每个数a[i] ...
- 南昌网络赛 I. Max answer (单调栈 + 线段树)
https://nanti.jisuanke.com/t/38228 题意给你一个序列,对于每个连续子区间,有一个价值,等与这个区间和×区间最小值,求所有子区间的最大价值是多少. 分析:我们先用单调栈 ...
- 网络赛 I题 Max answer 单调栈+线段树
题目链接:https://nanti.jisuanke.com/t/38228 题意:在给出的序列里面找一个区间,使区间最小值乘以区间和得到的值最大,输出这个最大值. 思路:我们枚举每一个数字,假设是 ...
- 南昌邀请赛I.Max answer 单调栈+线段树
题目链接:https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value of a in ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)
题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...
- HDU 5033 Building(北京网络赛B题) 单调栈 找规律
做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time L ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈
题面 题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值 题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok 我们 ...
- 线段树+单调栈+前缀和--2019icpc南昌网络赛I
线段树+单调栈+前缀和--2019icpc南昌网络赛I Alice has a magic array. She suggests that the value of a interval is eq ...
- dp--2019南昌网络赛B-Match Stick Game
dp--2019南昌网络赛B-Match Stick Game Xiao Ming recently indulges in match stick game and he thinks he is ...
随机推荐
- Tmux (转)
Tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于OpenBSD,采用BSD授权.使用它最直观的好处就是,通过一个终端登录远程主机并运行tmux后,在其中可以开启多个控制台而无需再“ ...
- 开发团队(Team)的主要职责和特征
角色介绍 开发团队是Scrum团队的三个角色之一. 开发团队包括架构师.开发工程师.测试人员.数据库管理员和UI设计师等,这几类人的跨职能组合.具备的技能足以实现产品开发. Team的主要职责 1.S ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 16—Recommender Systems 推荐系统
Lecture 16 Recommender Systems 推荐系统 16.1 问题形式化 Problem Formulation 在机器学习领域,对于一些问题存在一些算法, 能试图自动地替你学习到 ...
- 如何用Elasticsearch实现类似SQL中的IN查询实例
我想实现类似如下sql语句的效果: select * from table1 where rw_id in ('7a482589-e52e-0887-4dd5-5821aab77eea','c68ac ...
- jqgrid 自动换行
<style>.ui-jqgrid tr.jqgrow td { white-space: normal !important; height: auto; vertical-align: ...
- python3企业微信群组报警
公司提出一个需求需要做一个企业微信的一个消息推送,需要将消息发送到聊天群里详细信息如下. 如何创建应用请阅读我的上篇文章:https://www.cnblogs.com/wangyajunblog/p ...
- HttpClient由Client客户端上传File文件流至Server服务端
客户端方法 public static void main(String[] args) { File file=new File("E:\\lucene\\rev\\全年平台受理量.doc ...
- CocoaPods安装/更新报错While executing gem ... (OpenSSL::SSL::SSLError)解决方案
今天给新买的MacBook Pro更新CocoaPods,结果上来就报错,出师不利. HeinocdeMacBook-Pro:~ Heinoc$ sudo gem update --system Pa ...
- ubuntu18 tensorflow cpu fast_rcnn
(flappbird) luo@luo-All-Series:~/MyFile/TensorflowProject/tf-faster-rcnn/lib$ makepython setup.py bu ...
- jquery的get()方法
通过检索匹配jQuery对象得到对应的DOM元素. .get( [index ] ) index 类型: Integer 从0开始计数,用来确定获取哪个元素. .get() 方法允许我们直接访问jQu ...