南昌 Max answer
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的更多相关文章
- 南昌网络赛 I. Max answer 单调栈
Max answer 题目链接 https://nanti.jisuanke.com/t/38228 Describe Alice has a magic array. She suggests th ...
- 计蒜客 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 ...
- Max answer(单调栈+ST表)
Max answer https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value o ...
- 南昌邀请赛I.Max answer 单调栈+线段树
题目链接:https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value of a in ...
- icpc 南昌邀请赛网络赛 Max answer
就是求区间和与区间最小值的积的最大值 但是a[i]可能是负的 这就很坑 赛后看了好多dalao的博客 终于a了 这个问题我感觉可以分为两个步骤 第一步是对于每个元素 以它为最小值的最大区间是什么 第二 ...
- 南昌网络赛 I. Max answer (单调栈 + 线段树)
https://nanti.jisuanke.com/t/38228 题意给你一个序列,对于每个连续子区间,有一个价值,等与这个区间和×区间最小值,求所有子区间的最大价值是多少. 分析:我们先用单调栈 ...
- 2019ICPC南昌邀请赛网络赛 I. Max answer (单调栈+线段树/笛卡尔树)
题目链接 题意:求一个序列的最大的(区间最小值*区间和) 线段树做法:用单调栈求出每个数两边比它大的左右边界,然后用线段树求出每段区间的和sum.最小前缀lsum.最小后缀rsum,枚举每个数a[i] ...
- 2019南昌邀请赛预选赛 I. Max answer (前缀和+单调栈)
题目:https://nanti.jisuanke.com/t/38228 这题题解参考网上大佬的. 程序的L[i],R[i]代表a[i]这个点的值在区间 [L[i],R[i]] 中最小的并且能拓展到 ...
- 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( ...
随机推荐
- velocity在html中换行符转换
<td colspan="3" class="tdContent2">$!obj.getDealInfo().replaceAll("\r ...
- app开发中如何利用sessionId来实现服务端与客户端保持回话
app开发中如何利用sessionId来实现服务端与客户端保持回话 这个问题太过于常见,也过于简单,以至于大部分开发者根本没有关注过这个问题,我根据和我沟通的开发者中,总结出来常用的方法有以下几种: ...
- ADO.net开放式并发
https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/optimistic-concurrency 在多用户环境中,有两种用于更新 ...
- POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- OpenGL中的像素包装理解
OpenGL中的像素包装理解 像素包装 位图和像素图很少会被紧密包装到内存中.在许多硬件平台上,考虑到性能的原因位图和像素图的每一行的数据会从特殊的字节对齐地址开始.绝大多数编译 器会自动把变量和缓冲 ...
- Solr相似度算法二:Okapi BM25
地址:https://en.wikipedia.org/wiki/Okapi_BM25 In information retrieval, Okapi BM25 (BM stands for Be ...
- (zxing.net)一维码Code 128的简介、实现与解码
一.简介 一维码Code 128:1981年推出,是一种长度可变.连续性的字母数字条码.与其他一维条码比较起来,相对较为复杂,支持的字元也相对较多,又有不同的编码方式可供交互运用,因此其应用弹性也较大 ...
- PostSharp 结合 log4net 自动记录日志
环境: VS 2012 PostSharp-4.1.28 (下载地址)https://visualstudiogallery.msdn.microsoft.com/a058d5d3-e654-43f ...
- sql 常用基础查询
*查询第一条记录:select top 1 * from tablename *随机查询N条记录:select top N * from table order by newid( ...
- Windows7 64位中出现的KERNELBASE.dll错误的解决方法
最近在服程序时遇到个问题,电脑是win764位,编译完的exe测试,偶尔总报错,报错是偶尔的,有时候报错很频繁,但是有一次测试,测试了半天都没有报错,我以为好,发布输出没一会儿又报错了,真是崩溃了,所 ...