Max answer(The Preliminary Contest for ICPC China Nanchang National Invitational)
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 nn 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
题解:
枚举每个元素作为最小值时的最优答案,然后取最大值。首先根据单调队列的思想,求出每个元素作为最小值时能够管辖的范围。
如果这个元素是正数或零,那么就是它所管辖的区间的值的和乘上该元素,因为他是正数,所以它所管辖的区间内的值均为正数。
如果这个元素是负数,那我们只需要找到
从它到它能影响到的范围的最右边前缀和的最小值-从它左边第一个元素到它能影响到的最左边的左边第一个元素这个区间前缀和的最大值,然后乘上该元素。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+;
int n,ri[maxn],le[maxn],st[maxn];
ll ans=-1e18,a[maxn],sum[maxn],mi[maxn*],ma[maxn*];
void build(int L,int R,int rt)
{
if(L==R)
{
mi[rt]=ma[rt]=sum[L];
return ;
}
int mid=(R+L)/;
build(L,mid,*rt);
build(mid+,R,*rt+);
mi[rt]=min(mi[*rt],mi[*rt+]);
ma[rt]=max(ma[*rt],ma[*rt+]);
}
ll qmi(int L,int R,int rt,int l,int r)
{
if(l<=L && R<=r) return mi[rt];
int mid=(L+R)/;
ll ret=1e18;
if(l<=mid) ret=min(ret,qmi(L,mid,*rt,l,r));
if(r>=mid+) ret=min(ret,qmi(mid+,R,*rt+,l,r));
return ret;
}
ll qma(int L,int R,int rt,int l,int r)
{
if(l<=L && R<=r) return ma[rt];
int mid=(L+R)/;
ll ret=-1e18;
if(l<=mid) ret=max(ret,qma(L,mid,*rt,l,r));
if(r>=mid+) ret=max(ret,qma(mid+,R,*rt+,l,r));
return ret;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
sum[i]=sum[i-]+a[i];
}
build(,n,);
for(int i=;i<=n;i++)
{
int cnt=i-;
while(cnt>= && a[cnt]>=a[i])
{
cnt=le[cnt];
}
le[i]=cnt;
}
for(int i=;i<=n;i++) le[i]++;
for(int i=n;i>=;i--)
{
int cnt=i+;
while(cnt<=n && a[cnt]>=a[i])
{
cnt=ri[cnt];
}
ri[i]=cnt;
}
for(int i=;i<=n;i++) ri[i]--;
/*int top=0;
for(int i=1;i<=n;i++)
{
while(top && a[st[top]]>a[i])
{
ri[st[top]]=i-1;
top--;
}
st[++top]=i;
}
while(top) ri[st[top--]]=n;
for(int i=n;i>=1;i--)
{
while(top && a[st[top]]>a[i])
{
le[st[top]]=i+1;
top--;
}
st[++top]=i;
}
while(top) le[st[top--]]=1;*/
for(int i=;i<=n;i++)
{
if(a[i]>=) ans=max(ans,a[i]*(sum[ri[i]]-sum[le[i]-]));
else
{
if(le[i]==)
{
if(i==) ans=max(ans,a[i]*a[i]);
else ans=max(ans,a[i]*(qmi(,n,,i,ri[i])-max(0ll,qma(,n,,le[i],i-))));
}
else
{
ans=max(ans,a[i]*(qmi(,n,,i,ri[i])-qma(,n,,le[i]-,i-)));
}
}
}
printf("%lld\n",ans);
return ;
}
Max answer(The Preliminary Contest for ICPC China Nanchang National Invitational)的更多相关文章
- 2019The Preliminary Contest for ICPC China Nanchang National Invitational
The Preliminary Contest for ICPC China Nanchang National Invitational 题目一览表 考察知识点 I. Max answer 单调栈+ ...
- 计蒜客 38228. Max answer-线段树维护单调栈(The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer 南昌邀请赛网络赛) 2019ICPC南昌邀请赛网络赛
Max answer Alice has a magic array. She suggests that the value of a interval is equal to the sum of ...
- The Preliminary Contest for ICPC China Nanchang National Invitational
目录 Contest Info Solutions A. PERFECT NUMBER PROBLEM D. Match Stick Game G. tsy's number H. Coloring ...
- The Preliminary Contest for ICPC China Nanchang National Invitational and International Silk-Road Programming Contest
打网络赛 比赛前的准备工作要做好 确保 c++/java/python的编译器能用 打好模板,放在桌面 A. PERFECT NUMBER PROBLEM #include <cstdio> ...
- 2019 The Preliminary Contest for ICPC China Nanchang National Invitational(A 、H 、I 、K 、M)
A. PERFECT NUMBER PROBLEM 题目链接:https://nanti.jisuanke.com/t/38220 题意: 输出前五个完美数 分析: 签到.直接百度完美数输出即可 #i ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)
题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...
- 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题
Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
随机推荐
- 1 Groovy
1.1 什么是Groovy? groovy 是一个弱类型,动态语言,并且运行在JVM之上.它与java联系紧密.它是一个功能丰富和友好的java语言. Groovy源代码,通过Groovy编译器编译 ...
- 数据层——ImageData层
layer { name: "data" type: "ImageData" top: "data" top: "label&qu ...
- mc04_IntelliJ IDEA常用设置
字体设置 File --> Settings --> Font 项目编码设置 File --> Settings --> File Encodings 项目依赖 即一个项目引用 ...
- 运行结果:Spring Bean的生命周期
详见:http://www.cnblogs.com/zrtqsk/p/3735273.html https://blog.csdn.net/qq_23473123/article/details/76 ...
- 局部安装webpack时,使用webpack命令时提示webpack不是内部命令解决方法
现在js发展太快了,根本看不懂啊.于是乎想做做功课,于是乎看到了这些“奇怪”的写法,原来好多都是遵循了 ECMASCRIPT6,好吧,在本地看看怎么用的吧.写在本地的环境下, 发现各种报错,根本不能用 ...
- 宋宝华: 关于Linux进程优先级数字混乱的彻底澄清
宋宝华: 关于Linux进程优先级数字混乱的彻底澄清 原创: 宋宝华 Linux阅码场 9月20日 https://mp.weixin.qq.com/s/44Gamu17Vkl77OGV2KkRmQ ...
- android shape.xml 属性详解
转载源:http://blog.csdn.net/harvic880925/article/details/41850723 一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标 ...
- spring boot基本认识
大家眼中的spring boot:https://www.zhihu.com/question/39483566-------------------------------------------- ...
- rake 任务参数传递问题解决
原文 : https://robots.thoughtbot.com/how-to-use-arguments-in-a-rake-task namespace :tweets do desc 'S ...
- MVC HtmlHelper listbox用法
主要实现MVC listbox左右移动,搜索左边用户 controller List<userinfo> lstUserInfo = new List<userinfo>( ...