南昌网络赛 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 ...
随机推荐
- npm安装elasticsearch-reindex
由于新版的nodejs已经集成了npm,所以之前npm也一并安装好了.同样可以通过输入 "npm -v" 来测试是否成功安装. npm -v 你可以使用以下命令来查看所有全局安装的 ...
- C#中的goto
int i = 9;if (i % 2 == 0) goto Found;else goto NoFound; NoFound: Console.WriteLine(i.ToSt ...
- re模块之research
2. re.research re.research扫描整个字符串并返回第一个成功的匹配. 2.1函数语法: re.search(pattern, string, flags=0) 参数 描述 pat ...
- kubernetes configmap
ConfigMaps允许您将配置工件与image内容分离,以保持容器化应用程序的便携性. 本页面提供了一系列使用示例,演示如何使用ConfigMaps中存储的数据创建ConfigMaps和配置Pod. ...
- 高性能Web服务器Nginx的配置与部署研究(4)Nginx常用命令
1. 启动 Nginx poechant@ubuntu:sudo ./sbin/nginx 2. 停止 Nginx poechant@ubuntu:sudo ./sbin/nginx -s stop ...
- linux 安装天气插件
1. 用命令 sudo apt install gnome-shell-extension-weather 安装插件 但是在那之前你安装了 GNOME Shell Extensions 如果没有安装 ...
- iOS中NSFileManager文件常用操作整合
//获取Document路径 + (NSString *)getDocumentPath { NSArray *filePaths = NSSearchPathForDirectoriesInDoma ...
- cs api 之一
无法创建 无法创建网络 执行顺序
- javascript对变量和函数的声明提前‘hoist’
hoist vt.升起,提起; vi.被举起或抬高; n.起重机,升降机; 升起; <俚>推,托,举; 原文地址:http://www.bootcss.com/article/variab ...
- shiro 权限集成 sessionManager 配置 学习记录(三)
1.shiro配置文件增加sessionManager管理 <!-- 6.shiro结合Session会话管理器 start --> <bean id="sessionMa ...