南昌 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( ...
随机推荐
- [转]分布式中使用Redis实现Session共享(二)
本文转自:http://www.cnblogs.com/yanweidie/p/4678095.html 上一篇介绍了一些redis的安装及使用步骤,本篇开始将介绍redis的实际应用场景,先从最常见 ...
- 有關更新Java 至UPDATE 45 後出現沒法進入ORACLE EBS
近日部份使用者在更新Java 至UPDATE 45 後出現沒法進入ORACLE. 解決方法如下. 在開始 => 程式集 => JAVA => Configure Java中 (Ja ...
- Android-UIUtils-工具类
UIUtils工具类,主要是处理和Activity有关,和界面显示层有关的公共方法: package common.library.utils; import android.app.Activity ...
- Android-startService后台运行
什么时候使用startService? 答:APP在后台长时间运行并执行业务的时候,可以用服务,主要是看服务的特点(在后台长时间运行): Service相关代码: package liudeli.se ...
- [LeetCode 题解]: Symmetric Tree
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a ...
- struts2+ckeditor配置图片上传
又是一个漫漫长夜. 公司的编辑器坏了,用的是百度编辑器,上传图片的网址被框架给拦截了,我们本地怎么测试都没问题,放到服务器就这样了.和老李找了半天,疯了,没原因的. 笔者以前用过jsp+ckedito ...
- Buffer Pool--锁定内存页
锁定内存页在数据库中的优点和缺点: SQL Server 使用VirtualAlloc来分配内存,无内存压力时,SQL Server会尽可能地申请内存来缓存数据,当内存出现压力时,会出现缓存数据频繁地 ...
- 朋友,请待你的朋友——BUG好一点!
程序猿嘛,难免会被BUG缠身,我相信,没有一个程序猿在被BUG缠身时是感觉轻松的,消灭BUG一定是你最大的愿望.本周,我们团队的项目进入调试阶段,各种BUG层出不穷,眼看下个周就要进行项目答辩会,所以 ...
- C#——Winform 无边框随意拖动【转载】
本篇技术内容转载自:http://www.cnblogs.com/ap0606122/archive/2012/10/23/2734964.html using System; using Syste ...
- HtmlHelper扩展之mvchtmlstring
后台: using System;using System.Web;using System.Web.Mvc; namespace EwayFramework.Utils.Token{ public ...