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 \le n \le 5 \times 10 ^5n(1≤n≤5×105).

Second line contains nn integers represent the array a (-10^5 \le a_i \le 10^5)a(−105≤ai​≤105).

Output

One line contains an integer represent the answer of the array.

样例输入复制

5
1 2 3 4 5

样例输出复制

36

题意:给你一个长度为n的序列,让你找一段区间使得该段 区间和 和 它的区间最小值 乘积最大,并输出最大值。

思路:因为我们不知道当最小值是多少时 以它为最小值的区间 的和 与 它的乘积最大,所以我们只能枚举每个位置,以每个位置为最小值,求出以它为最小值的最大值

当我们以当前位置的值作为最小值时,我们怎么求出以它为最小值的最大区间和呢
首先,我们要知道以当前值为区间最小值时,它可能的区间范围

 

以上图为例 ,当我们以第三个位置的值为最小值时,它的区间范围为(2,5)
我们可以在区间(2,5)内找到一段区间和最大的区间(一定要包含第三号位置),就找到了以第三个位置为最小值的题目要找的最大值。
找这段和最大的区间时我们可以用前缀和建线段树来求出 当当前位置值为正数时,我们从当前位置的左边的区间找出最小的前缀和,从当前位置的右边区间找出最大的前缀和,那么,用右边找到的值减去左边的值就是我们满足我们要求的最大的区间和
(这样用右边找到的前缀和减去左边找到的前缀和不就是在中间我们要找的包含当前位置的区间最大和吗,负数的时候相反) 代码:
#include<cstdio>
#include<algorithm>
#include<stack>
#define ll long long
using namespace std;
struct point{
ll a;//值
ll x;//位置
}s[];
stack<point> st;
struct{
ll l,r;//记录以当前位置为最小值区可以取区间的范围
}bd[];
ll sum[];//前缀和
struct{
ll mx,mn;//记录前缀和的最大值和最小值
}tree[];
void build(ll l,ll r,ll k){
if(l==r){
tree[k].mn=tree[k].mx=sum[l];
return ;
}
ll mid=(l+r)>>;
build(l,mid,k*);
build(mid+,r,k*+);
tree[k].mx=max(tree[k*].mx,tree[k*+].mx);
tree[k].mn=min(tree[k*].mn,tree[k*+].mn);
return ;
}
ll query1(ll l,ll r,ll k,ll L,ll R){//找最大值
//printf("WW%d %d %d %d\n",l,r,L,R);
if(l>=L&&r<=R){
return tree[k].mx;
}
ll a,b;
a=-1e18;
b=-1e18;
ll mid=(l+r)>>;
if(L<=mid)
a=query1(l,mid,k*,L,R);
if(R>mid)
b=query1(mid+,r,k*+,L,R);
return max(a,b);
}
ll query2(ll l,ll r,ll k,ll L,ll R){//找最小值
if(l>=L&&r<=R){
return tree[k].mn;
} //printf("EE%d %d %d %d\n",l,r,L,R);
ll a,b;
a=1e18;
b=1e18;
ll mid=(l+r)>>;
if(L<=mid)
a=query2(l,mid,k*,L,R);
if(R>mid)
b=query2(mid+,r,k*+,L,R);
return min(a,b);
}
int main(){
ll n;
scanf("%lld",&n);
sum[]=;
for(ll i=;i<=n;i++){
scanf("%lld",&s[i].a);
s[i].x=i;
sum[i]=sum[i-]+s[i].a;
}
for(ll i=;i<=n;i++){//单调栈求范围
if(st.empty()){//如果当前栈为空,当前要放入位置的左极限位置就是自己的位置
bd[s[i].x].l=i;
st.push(s[i]);
}
else{
point tp=st.top();
if(tp.a==s[i].a){//如果当前位置的值与栈顶的值一样,那么当前要放入位置的左极限就和栈顶位置的左极限相同
bd[s[i].x].l=bd[tp.x].l;
st.push(s[i]);
}
else if(tp.a<s[i].a){//如果当前放入位置的值大于栈顶位置的值,那么当前放入位置的左极限位置就是自己的位置
bd[s[i].x].l=i;
st.push(s[i]);
}
else{//如果当前放入位置的值小于栈顶位置的值
st.pop();
ll r=tp.x;
ll l=bd[tp.x].l;
bd[tp.x].r=r;//栈顶位置的右极限就是当前栈顶位置的位置
if(!st.empty())
tp=st.top();
while(!st.empty()&&tp.a>s[i].a){//如果下一个栈顶位置的值还是大于当前位置
st.pop();
bd[tp.x].r=r;//后面的栈顶可以到的右极限都和第一次栈顶位置的右极限相同,因为栈是从小到上递增的
l=bd[tp.x].l;//更新要放入的当前位置可以到的左极限
if(!st.empty())
tp=st.top();
}
bd[s[i].x].l=l;
st.push(s[i]);
}
}
}
if(!st.empty()){//更新
point tp=st.top();
ll r=tp.x;
while(!st.empty()){
tp=st.top();
st.pop();
bd[tp.x].r=r;
}
}
// for(ll i=1;i<=n;i++){
// printf("%d %d %d\n",i,bd[i].l,bd[i].r);
// }
build(,n,);
ll ans=-1e18;
// printf("OK");
for(ll i=;i<=n;i++){
ll r1,l1;
if(s[i].a>){
r1=query1(,n,,s[i].x,bd[s[i].x].r);
l1=query2(,n,,bd[s[i].x].l-,s[i].x-);
//printf("%lld %lld\n",l1,r1);
ans=max((r1-l1)*s[i].a,ans);
}
else if(s[i].a<){
r1=query2(,n,,s[i].x,bd[s[i].x].r);
l1=query1(,n,,bd[s[i].x].l-,s[i].x-);
//printf("%lld %lld\n",l1,r1);
ans=max((r1-l1)*s[i].a,ans);
}
else
ans=max(0LL,ans);
}
printf("%lld\n",ans);
}

The Preliminary Contest for ICPC China Nanchang National Invitational I题的更多相关文章

  1. 2019The Preliminary Contest for ICPC China Nanchang National Invitational

    The Preliminary Contest for ICPC China Nanchang National Invitational 题目一览表 考察知识点 I. Max answer 单调栈+ ...

  2. 计蒜客 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 ...

  3. 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 ...

  4. The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)

    题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...

  5. The Preliminary Contest for ICPC China Nanchang National Invitational and International Silk-Road Programming Contest

    打网络赛 比赛前的准备工作要做好 确保 c++/java/python的编译器能用 打好模板,放在桌面 A. PERFECT NUMBER PROBLEM #include <cstdio> ...

  6. 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 ...

  7. The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈

    题面 题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值 题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok 我们 ...

  8. 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 ...

  9. 计蒜客 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 ...

随机推荐

  1. Spring Boot监控与管理的实现

    认识Actuator 在SpringBoot应用中引入spring-boot-starter-actuator依赖,它可以为开发团队提供系统运行的各项监控指标. 在项目中引入依赖配置如下: appli ...

  2. 亚马逊VE账号运营

    VE劲爆内幕大揭秘!“仿牌+Amazon VE”跟卖之路 Amazon Vendor Express 是Amazon.com2015年下旬推出的新的供应商平台,商家通过这个平台可以把产品卖给Amazo ...

  3. 批处理脚本+adb命令

    app的测试过程中,有一些重复性的繁琐工作,可以采用用批处理脚本+adb命令方式来代替 说明: (1)等待时间我用的ping命令替代的,比较简单直观 (2)我采取的是用坐标定位,后续会使用控件来定位 ...

  4. A*算法介绍

    你是否在做一款游戏的时候想创造一些怪兽或者游戏主角,让它们移动到特定的位置,避开墙壁和障碍物呢? 如果是的话,请看这篇教程,我们会展示如何使用A星寻路算法来实现它! 在网上已经有很多篇关于A星寻路算法 ...

  5. ES9新内容概括

    本文主要介绍ES2018 新出的一些特性 1.异步迭代 允许 async/await 与 for-of 一起使用,以串行的方式运行异步操作,达到异步函数的迭代效果. async function pr ...

  6. 判断PDF文件是否相同(通过二进制流判断)

    一.Java代码 1.将PDF转为字节流    /*     * @step     *  1.使用BufferedInputStream和FileInputStream从File指定的文件中读取内容 ...

  7. Netty(一)——Netty入门程序

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7447618.html 有兴趣的可先了解下:4种I/O的对比与选型 主要内容包括: Netty开发环境的搭建 ...

  8. JRebel springboot部署idea

    JRebel springboot部署idea     http://127.0.0.1:8888/88414687-3b91-4286-89ba-2dc813b107ce   ctrl+shift+ ...

  9. 【css3】画‘百分比圆’

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 使用JS判断不同的终端设备

    const ua: string = window.navigator.userAgent; const isWeixin: boolean = /MicroMessenger/i.test(ua); ...