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. Maven项目下java.lang.ClassNotFoundException常规解决办法

    网上很多要修改.class .project的比较麻烦有时候还不一定管用.下面的方法适合于项目用已经引用了jar,但是运行时却ClassNotFound,请用如下方法试试: 严重: Error con ...

  2. xcconfig

    [xcconfig] 1.When you can use a .xcconfig file? Use .xcconfig files if you find yourself changing th ...

  3. CentOS7通过 yum安装路径查询方法

    CentOS7通过 yum安装路径查询方法 rpm -qa 然后执行 rpm -ql 软件名称 就可以显示软件的安装路径. 原文博客的链接地址:https://cnblogs.com/qzf/

  4. java类的泛型DAO

    @Transactional public abstract class DAOSupport<T> implements DAO<T> { protected Class&l ...

  5. Uploadify多文件上传插件.NET使用案例+PHP使用案例

    ploadify是一个非常好用的多文件上传插件 插件下载:http://www.uploadify.com 下载后需要用到的文件: 接下来就是直接添加代码: Default.aspx代码 <%@ ...

  6. ZOJ3714:Java Beans

    There are N little kids sitting in a circle, each of them are carrying some java beans in their hand ...

  7. [Yii2]yiisoft/yii2 2.0.2 requires bower-asset/jquery 2.1.*@stable | 1.11.*@stable -> no matching package found

    composer require "dektrium/yii2-user:0.9.*@dev" 一直安装失败,提示:Your requirements could not be r ...

  8. 关于createTextRange和createRange的一些用法【转】

    一.返回createTextRange的text和htmlText <mce:script language="javascript"><!--function ...

  9. Nutch 问题杂记

    1. 如何绕过目标站点的robots.txt限制 多数站点都是只允许百度.google等搜索引擎抓取的,所以会在robots.txt里限制其他爬虫. nutch自然是会遵循robots协议的,但是我们 ...

  10. 如何在TortoiseGit中使用ssh-keygen生成的key

    再windows 用TortoiseGit 时,git clone 项目时 提示 "Couldn't load this key (OpenSSH SSH-2 private key),如下 ...