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 ...
随机推荐
- Python学习笔记_零碎知识
1. 变量本身类型不固定的语言称之为动态语言,与之对应的是静态语言.静态语言在定义变量时必须指定变量类型,如果赋值的时候类型不匹配,就会报错. 2. Python有两种除法: /除法计算结果是浮点数, ...
- Linux Vi/Vim 在插入模式下键盘右边数字键输入异常
问题:Linux在 Vi/Vim 在编辑文件时遇到一个问题,当我在 Insert 模式下进行修改文件内容的时候,用到了键盘(104键,右边带数字键那种)进行数字输入,当我输入数字 “5” 时,插入的数 ...
- java——arr == null || arr.length == 0
这两者是不同的: arr == null; int[] arr = null; arr.length == 0; int[] arr =new int[0];
- windows系统打开火狐浏览器提示“无法加载你的firefox配置文件”
win7系统自带IE浏览器,还是有部分用户使用不习惯,选择下载第三方浏览器,比如:火狐.谷歌.360浏览器等.最近有Win7系统用户在重新安装火狐浏览器后发现打不开,并提示“无法加载你的firefox ...
- 研磨设计模式学习笔记4--单例模式Signleton
需求:加载配置文件,由于配置文件全局唯一,所以不用过多对象,建一个就可以了. 优点:单例模式本质就是为了控制实例数目. 一.饿汉式 public class Singleton { private S ...
- hadoop和spark比较
http://blog.51cto.com/13943588/2165946 3.hadoop和spark的都是并行计算,那么他们有什么相同和区别? 两者都是用mr模型来进行并行计算,hadoop的 ...
- 在使用clone()时id保持一致
大家都知道,同一个HTML页面中,不宜出现1个以上相同名称的id.但有时候需要使用jQuery框架的clone()来复制相同内容(附带样式),假如是使用了id号的获取方式,即$(‘#***’) 那么复 ...
- 使用Myeclipse导入IDEA项目
问题描述:使用Myeclipse导入IDEA创建的Web项目,成功导入,但是显示的是一个普通的JAVA项目,无法加载到tomcat下. 解决方案:右键项目Properties,选择Myeclipse- ...
- .NET标准化题目
1. 下面对FxCop的描述中,错误的是:(D) A. FxCop是一个静态代码分析工具. B. 可以定制自己的规则加入FxCop引擎. C. FxCop主要是对.NET中托管代码的assembly进 ...
- POJ 1860——Currency Exchange——————【最短路、SPFA判正环】
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...