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. Java并发艺术-CAS

    前言 CAS(Compare and Swap),即比较并替换,实现并发算法时常用到的一种技术,Doug lea大神在java同步器中大量使用了CAS技术,鬼斧神工的实现了多线程执行的安全性. CAS ...

  2. Windows sql语句正则匹配导出数据到本地 The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

    尝试使用 into outfile导出数据的时候出现错误: The MySQL server is running with the --secure-file-priv option so it c ...

  3. [label][javascript-Unit Test][JSLint]A Guide To JSLint Messages

    原文链接: http://www.jameswiseman.com/blog/2011/03/26/coding-convention-an-style-guide/ http://www.james ...

  4. Android studio 报错 installation failed with message failed to finalize session:INSTALL_FAILED_INVALID_APK 解决方法

    解决方案: File->Setting->Build->Instant Run

  5. java多线程 基础demo

    join()   让主进程等待子进程全部执行完 例子如下:   package mocker; public class TestThread5 extends Thread {      priva ...

  6. spark-streming 中调用spark-sql时过程遇到的问题

    在spark-streming 中调用spark-sql时过程遇到的问题 使用版本:spark-2.1.0 JDK1.8 1. spark-sql中对limit 的查询结果使用sum() 聚合操作不生 ...

  7. ssl协议,openssl,创建私有CA

    SSL是Security Socket Layer:安全的套接字层 他介于HTTP和TCP协议层之间 SSL是Netscape公司开发的,属于个人 TLS是标准委员会制定的 OpenSSL是SSL的开 ...

  8. SQL Server数据类型总结

    1.char char [(n)]存储固定长度的非Unicode字符串数据.n定义字符串长度,并且必须是1到8,000之间的值.存储大小为n个字节. 2.varchar varchar [(n | m ...

  9. 自己从0开始学习Unity的笔记 II (C#条件语句基础练习)

    首先,自己用了下三目表达式,来做了一个闰年的判断,也就是能被400整除,或者是能够被4整除,但是不能被100整除的年份. int year; //输入的年 bool fourHunderd; //能被 ...

  10. 经典的兔子生兔子问题(C#递归解法)

    古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 思路:先求出每个月新增的兔子,再用循环求和即可算出这个月 ...