https://nanti.jisuanke.com/t/38228

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

Second line contains nnn integers represent the array a(−105≤ai≤105)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
题意:求区间和乘以区间内最小的一个数的最大值,包含正负 题解:前缀和预处理,单调栈找到每一个数向左延伸的最大距离和向右延伸的最大距离,分别记录这两个坐标。
求最大值的时候分正负处理:
正数:mx=max(mx,a[i]*(sum[r[i]]-sum[l[i]]+a[l[i]];
负数:暴力加和,[l[i],r[i]]区间内的数都加起来,每次更新最小和(因为是负数,乘以a[i]后就是最大值)
#include<iostream>
#include<string.h>
#include<stack>
#define ll long long
using namespace std;
ll l[], r[], a[], sum[];
ll n,mx;
int main()
{
while (~scanf("%lld", &n))
{
for (int i = ; i < n; i++)//刚开始是从1开始输入的,然后前缀和也是从1开始处理的,一位可以简单一点,但是因为
{ //栈是从0开始处理的,下面的a[p.top()]可能为a[0],导致一直TLE,找这个错误到自闭。。。
scanf("%lld", &a[i]);
}
sum[] = a[];
for (int i = ; i < n; i++)
{
sum[i] = sum[i - ] + a[i];
}
stack<ll>p;
for (int i = ; i < n; i++)
{
while (!p.empty() && a[p.top()] >= a[i])//按照大于等于a[i]的规则,从a[i]开始,向左边可以延伸最远的数的下标
p.pop();
if (p.empty())//说明a[i]左边的所有数都大于等于a[i]
l[i] = ;
else//找到就记录从a[i]开始向左边寻找大于等于a[i]的最大边界下标
l[i] = p.top() + ;
p.push(i);
}
while (!p.empty())
p.pop();
for (int i = n - ; i >= ; i--)//往右边找第一个比a[i]小的数
{
while (!p.empty() && a[p.top()] >= a[i])
p.pop();
if (p.empty())
r[i] = n - ;
else
r[i] = p.top() - ;
p.push(i);
}
mx = ;
for (int i = ; i < n; i++)
{
if (a[i] >= )
mx = max(mx, a[i] * (sum[r[i]] - sum[l[i]] + a[l[i]]));//这里是a[l[i]]
else
{
ll ans = , m1 = , m2 = ;
for (int j = i + ; j <= r[i] && j < n; j++)
{
ans = ans + a[j];
m1 = min(m1, ans);
}
ans = ;
for (int j = i - ; j >= l[i] && j >= ; j--)
{
ans = ans + a[j];
m2 = min(ans, m2);
}
mx = max(mx, a[i] * (m1 + m2 + a[i]));
}
} printf("%lld\n", mx);
// for(int i=1;i<=n;i++)
// cout<<l[i]<<' ';
// cout<<endl;
// for(int i=1;i<=n;i++)
// cout<<r[i]<<' ';
// cout<<endl; }
return ;
}

南昌 Max answer的更多相关文章

  1. 南昌网络赛 I. Max answer 单调栈

    Max answer 题目链接 https://nanti.jisuanke.com/t/38228 Describe Alice has a magic array. She suggests th ...

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

  3. Max answer(单调栈+ST表)

    Max answer https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value o ...

  4. 南昌邀请赛I.Max answer 单调栈+线段树

    题目链接:https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value of a in ...

  5. icpc 南昌邀请赛网络赛 Max answer

    就是求区间和与区间最小值的积的最大值 但是a[i]可能是负的 这就很坑 赛后看了好多dalao的博客 终于a了 这个问题我感觉可以分为两个步骤 第一步是对于每个元素 以它为最小值的最大区间是什么 第二 ...

  6. 南昌网络赛 I. Max answer (单调栈 + 线段树)

    https://nanti.jisuanke.com/t/38228 题意给你一个序列,对于每个连续子区间,有一个价值,等与这个区间和×区间最小值,求所有子区间的最大价值是多少. 分析:我们先用单调栈 ...

  7. 2019ICPC南昌邀请赛网络赛 I. Max answer (单调栈+线段树/笛卡尔树)

    题目链接 题意:求一个序列的最大的(区间最小值*区间和) 线段树做法:用单调栈求出每个数两边比它大的左右边界,然后用线段树求出每段区间的和sum.最小前缀lsum.最小后缀rsum,枚举每个数a[i] ...

  8. 2019南昌邀请赛预选赛 I. Max answer (前缀和+单调栈)

    题目:https://nanti.jisuanke.com/t/38228 这题题解参考网上大佬的. 程序的L[i],R[i]代表a[i]这个点的值在区间 [L[i],R[i]] 中最小的并且能拓展到 ...

  9. 2019南昌邀请赛网络预选赛 I. Max answer(单调栈+暴力??)

    传送门 题意: 给你你一序列 a,共 n 个元素,求最大的F(l,r): F(l,r) = (a[l]+a[l+1]+.....+a[r])*min(l,r); ([l,r]的区间和*区间最小值,F( ...

随机推荐

  1. CString->char*.,char*->CString,char*->LPCTSTR

    CString->char* CString strSource;//宣告CString char* charSource; //宣告char* 法1: charSource = (char*) ...

  2. handsontable-developer guide-cell function

    renderer 展示的数据不是来自于数据源,而是先把DOM和其他信息传给renderer,然后展示. //五种展示函数 TextRenderer: default NumericRenderer A ...

  3. [javascript]两段 javaScript 代码的逻辑比较

    两段 javaScript 代码的逻辑比较: #1 if(tagName.length < 3){    $(this).parent().addClass('active');    tagN ...

  4. [label][转载][paypal]paypal在线支付接口的WEB语言设置

    http://stephen830.iteye.com/blog/274072 ★★★ 本篇为原创,需要引用转载的朋友请注明:< http://stephen830.iteye.com/blog ...

  5. KVM虚拟机windows系统增加硬盘

    原文:http://www.ilanni.com/?p=6211 前一篇文章介绍了有关linux系统添加硬盘的方法,这次我们来介绍有关windows系统添加的相关步骤. 其实linux和windows ...

  6. [uwp]数据绑定再学习

    在开始上代码前,先来假设这样一种情形: 出于某些原因,创建一个自定义控件(UserControl),然后为它定义一个依赖属性,这个属性有两个作用,一是调用控件方通过数据绑定技术为它赋值,二是控件内部的 ...

  7. Keepalived_vrrp: ip address associated with VRID not present in received packet

    keepalived常见的启动报错: 5913 May 16 15:26:04 localhost Keepalived_vrrp: ip address associated with VRID n ...

  8. jQuery--基本介绍与下载

    本篇内容: 1.jquery学习内容 2.jquery下载,引用 3.使用顺序 4.版本 5.jquery对象与DOM对象转化 jQuery认识: jQuery学习内容 选择器 筛选器 样式操作 文本 ...

  9. Java50道经典习题-程序11 求不重复数字

    题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. public cla ...

  10. Syncthing源码解析 - 第三方库

    1,AudriusButkevicius/cli 网址:https://github.com/AudriusButkevicius/cli 2,bkaradzic/go-lz4 网址:https:// ...