南昌网络赛 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 ...
随机推荐
- iOS开发基础控件--UIButton
01 //这里创建一个圆角矩形的按钮 02 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 03 ...
- 插入后获取到id
第一种方法: insert INTO student(name) VALUES("南亚");SELECT @@identity 第二种方法: insert INTO student ...
- spring中bean 的属性id与name
- Android——eclipse共享library以及导出jar包[转]
目录(?)[-] 一apk之间共享Class 二apk导出jar包 android的apk在在eclipse上进行开发的时候,有时候需要import其它包中的一些class,正常的方法就是在jav ...
- C++——代码运行过程详解
#include <iostream> using namespace std; ;//初始化的全局变量:保存在数据段 char *p1;//未初始化的全局变量:保存在BSS段 int m ...
- 使用lucene query的CharFilter 去掉字符中的script脚本和html标签
1.准备数据,这里我从数据库读取一个带有html标签和script脚本的数据
- 集合_java集合框架
转载自http://blog.csdn.net/zsw101259/article/details/7570033 Java集合框架图 简化图: Java平台提供了一个全新的集合框架.“集合框架”主要 ...
- 用Redis解决互联网项目的数据读取难点
Redis在很多方面与其他数据库解决方案不同:它使用内存提供主存储支持,而仅使用硬盘做持久性的存储:它的数据模型非常独特,用的是单线程.另一个大区别在于,你可以在开发环境中使用Redis的功能,但却不 ...
- RotbotFrameWork接口测试
一.添加接口测试Library 二.参数说明 三.关键字使用说明 Set Varibae: 参数化接口地址 Connect To Databse Using Custom Param: 连接数据库(需 ...
- 面向对象的JavaScript-001
一. Question是父类,MultipleChoiceQuestion和DragDropQuestion是子类 二. 1. <script> // 面向对象 function Ques ...