The Preliminary Contest for ICPC China Nanchang National Invitational I题
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 \le n \le 5 \times 10 ^5n(1≤n≤5×105).
Second line contains nn integers represent the array 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 题意:给你一个长度为n的序列,让你找一段区间使得该段 区间和 和 它的区间最小值 乘积最大,并输出最大值。 思路:因为我们不知道当最小值是多少时 以它为最小值的区间 的和 与 它的乘积最大,所以我们只能枚举每个位置,以每个位置为最小值,求出以它为最小值的最大值 当我们以当前位置的值作为最小值时,我们怎么求出以它为最小值的最大区间和呢
首先,我们要知道以当前值为区间最小值时,它可能的区间范围

以上图为例 ,当我们以第三个位置的值为最小值时,它的区间范围为(2,5)
我们可以在区间(2,5)内找到一段区间和最大的区间(一定要包含第三号位置),就找到了以第三个位置为最小值的题目要找的最大值。
找这段和最大的区间时我们可以用前缀和建线段树来求出 当当前位置值为正数时,我们从当前位置的左边的区间找出最小的前缀和,从当前位置的右边区间找出最大的前缀和,那么,用右边找到的值减去左边的值就是我们满足我们要求的最大的区间和
(这样用右边找到的前缀和减去左边找到的前缀和不就是在中间我们要找的包含当前位置的区间最大和吗,负数的时候相反) 代码:
#include<cstdio>
#include<algorithm>
#include<stack>
#define ll long long
using namespace std;
struct point{
ll a;//值
ll x;//位置
}s[];
stack<point> st;
struct{
ll l,r;//记录以当前位置为最小值区可以取区间的范围
}bd[];
ll sum[];//前缀和
struct{
ll mx,mn;//记录前缀和的最大值和最小值
}tree[];
void build(ll l,ll r,ll k){
if(l==r){
tree[k].mn=tree[k].mx=sum[l];
return ;
}
ll mid=(l+r)>>;
build(l,mid,k*);
build(mid+,r,k*+);
tree[k].mx=max(tree[k*].mx,tree[k*+].mx);
tree[k].mn=min(tree[k*].mn,tree[k*+].mn);
return ;
}
ll query1(ll l,ll r,ll k,ll L,ll R){//找最大值
//printf("WW%d %d %d %d\n",l,r,L,R);
if(l>=L&&r<=R){
return tree[k].mx;
}
ll a,b;
a=-1e18;
b=-1e18;
ll mid=(l+r)>>;
if(L<=mid)
a=query1(l,mid,k*,L,R);
if(R>mid)
b=query1(mid+,r,k*+,L,R);
return max(a,b);
}
ll query2(ll l,ll r,ll k,ll L,ll R){//找最小值
if(l>=L&&r<=R){
return tree[k].mn;
} //printf("EE%d %d %d %d\n",l,r,L,R);
ll a,b;
a=1e18;
b=1e18;
ll mid=(l+r)>>;
if(L<=mid)
a=query2(l,mid,k*,L,R);
if(R>mid)
b=query2(mid+,r,k*+,L,R);
return min(a,b);
}
int main(){
ll n;
scanf("%lld",&n);
sum[]=;
for(ll i=;i<=n;i++){
scanf("%lld",&s[i].a);
s[i].x=i;
sum[i]=sum[i-]+s[i].a;
}
for(ll i=;i<=n;i++){//单调栈求范围
if(st.empty()){//如果当前栈为空,当前要放入位置的左极限位置就是自己的位置
bd[s[i].x].l=i;
st.push(s[i]);
}
else{
point tp=st.top();
if(tp.a==s[i].a){//如果当前位置的值与栈顶的值一样,那么当前要放入位置的左极限就和栈顶位置的左极限相同
bd[s[i].x].l=bd[tp.x].l;
st.push(s[i]);
}
else if(tp.a<s[i].a){//如果当前放入位置的值大于栈顶位置的值,那么当前放入位置的左极限位置就是自己的位置
bd[s[i].x].l=i;
st.push(s[i]);
}
else{//如果当前放入位置的值小于栈顶位置的值
st.pop();
ll r=tp.x;
ll l=bd[tp.x].l;
bd[tp.x].r=r;//栈顶位置的右极限就是当前栈顶位置的位置
if(!st.empty())
tp=st.top();
while(!st.empty()&&tp.a>s[i].a){//如果下一个栈顶位置的值还是大于当前位置
st.pop();
bd[tp.x].r=r;//后面的栈顶可以到的右极限都和第一次栈顶位置的右极限相同,因为栈是从小到上递增的
l=bd[tp.x].l;//更新要放入的当前位置可以到的左极限
if(!st.empty())
tp=st.top();
}
bd[s[i].x].l=l;
st.push(s[i]);
}
}
}
if(!st.empty()){//更新
point tp=st.top();
ll r=tp.x;
while(!st.empty()){
tp=st.top();
st.pop();
bd[tp.x].r=r;
}
}
// for(ll i=1;i<=n;i++){
// printf("%d %d %d\n",i,bd[i].l,bd[i].r);
// }
build(,n,);
ll ans=-1e18;
// printf("OK");
for(ll i=;i<=n;i++){
ll r1,l1;
if(s[i].a>){
r1=query1(,n,,s[i].x,bd[s[i].x].r);
l1=query2(,n,,bd[s[i].x].l-,s[i].x-);
//printf("%lld %lld\n",l1,r1);
ans=max((r1-l1)*s[i].a,ans);
}
else if(s[i].a<){
r1=query2(,n,,s[i].x,bd[s[i].x].r);
l1=query1(,n,,bd[s[i].x].l-,s[i].x-);
//printf("%lld %lld\n",l1,r1);
ans=max((r1-l1)*s[i].a,ans);
}
else
ans=max(0LL,ans);
}
printf("%lld\n",ans);
}
The Preliminary Contest for ICPC China Nanchang National Invitational I题的更多相关文章
- 2019The Preliminary Contest for ICPC China Nanchang National Invitational
The Preliminary Contest for ICPC China Nanchang National Invitational 题目一览表 考察知识点 I. Max answer 单调栈+ ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- The Preliminary Contest for ICPC China Nanchang National Invitational
目录 Contest Info Solutions A. PERFECT NUMBER PROBLEM D. Match Stick Game G. tsy's number H. Coloring ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)
题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...
- The Preliminary Contest for ICPC China Nanchang National Invitational and International Silk-Road Programming Contest
打网络赛 比赛前的准备工作要做好 确保 c++/java/python的编译器能用 打好模板,放在桌面 A. PERFECT NUMBER PROBLEM #include <cstdio> ...
- Max answer(The Preliminary Contest for ICPC China Nanchang National Invitational)
Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈
题面 题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值 题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok 我们 ...
- 2019 The Preliminary Contest for ICPC China Nanchang National Invitational(A 、H 、I 、K 、M)
A. PERFECT NUMBER PROBLEM 题目链接:https://nanti.jisuanke.com/t/38220 题意: 输出前五个完美数 分析: 签到.直接百度完美数输出即可 #i ...
- 计蒜客 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 ...
随机推荐
- mysql主从同步与读写分离
为了解决数据库服务的高可用问题以及负载均衡问题, 1正常情况下可以互为主从,均衡分担数据流量, 2防止数据库服务器在宕机的情况下可以顺利切换到正常的数据库服务器,减少公司的客户流量损失故公司需要搭建数 ...
- [原][译]我们为什么需要另一个c++测试框架?Catch||Why do we need yet another C++ test framework?
翻译问题来源:https://github.com/catchorg/Catch2/blob/master/docs/why-catch.md 其他辅助博文:从Google Test 转到 Catch ...
- 理解R语言gdistance包下的transition函数
library(raster)library(gdistance)r <- raster(nrows=3, ncols=4, xmn=0, xmx=7, ymn=0, ymx=6, crs=&q ...
- Hadoop分布式文件系统HDFS的工作原理
Hadoop分布式文件系统(HDFS)是一种被设计成适合运行在通用硬件上的分布式文件系统.HDFS是一个高度容错性的系统,适合部署在廉价的机器上.它能提供高吞吐量的数据访问,非常适合大规模数据集上的应 ...
- 文献导读 | Single-Cell Sequencing of iPSC-Dopamine Neurons Reconstructs Disease Progression and Identifies HDAC4 as a Regulator of Parkinson Cell Phenotypes
文献编号:19Mar - 11 2019年04月23日三读,会其精髓: 相信这种方法的话,那么它的精髓是什么,如何整合出这个core gene set. 首先要考虑样本的选择,样本里是否存在明显的分层 ...
- 设置RHEL-7.0的运行级别
在RHEL7中修改默认运行级别与7以前版本的修改方式不同(7以前版本可以修改/etc/inittab中的“id:5:initdefault:”参数值来实现),RHEL7在/etc/inittab文件中 ...
- 理解去中心化 稳定币 DAI
本文转载于深入浅出区块链, 原文链接 随着摩根大通推出JPM Coin 稳定币,可以预见稳定币将成为区块链落地的一大助推器. 坦白来讲,对于一个程序员的我来讲(不懂一点专业经济和金融),理解DAI的机 ...
- 在Mac os 10.11 下编译Berkeley caffe
安装各种补丁和组件,缺啥装啥. python 采用 2.7.13 最新版. 安装工具 homebrew , pip 很繁琐,但是没难度. 由于本人macbook pro不支持CUDA,所以不用安装. ...
- count性能
表有主键列,count(1)的效率会稍微高于count(*),count(主键列)效率会高于count(1).表没有主键列,count(1)效率会高于count(*) count(1).count(* ...
- 用servlet验证密码2
function createXMLHttpRequest() { var XMLHttpRequest1; if (window.XMLHttpRequest) { XMLHttpRequest_t ...