Max answer

题目链接

https://nanti.jisuanke.com/t/38228

Describe

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*10^5)

Second line contains n integers represent the array a(−10^5≤ai≤10^5)

Output

One line contains an integer represent the answer of the array.

样例输入

5

1 2 3 4 5

样例输出

36

题意
给你一个序列,对于每个连续子区间,有一个价值,等与这个区间和×区间最小值,
求所有子区间的最大价值是多少。

题解

a[i]表示第i位的权值,L[i]表示第i个数左边第一个比他小的数,R[i]表示右边第一个比它小的数。

我们可以用单调站求出来,不会可以先做洛谷的完美序列, https://www.luogu.org/problemnew/show/P2659

然后对于每一位i,如果a[i]为正数,我们可以求包含i的区间和最大的子区间,然后再乘上这个值,当然不能越过L[i]和R[i]。

我们怎么求呢?

我们先定义sum(i,j)为i到j的区间和,再定义lmax[i]为以i 为右端点的使得区间和和最大的左端点位置,(即求一个l<=i,使得sum(l,i)最大)。

那么,lmax[i]可以通过lmax[i-1]更新,这个就是一个贪心吧,分两种情况:

1.sum(lmax[i-1],i-1)<0, 那么lmax[i]=i.

2.sum(lmax[i-1],i-1)>0,lmax[i+1]=lmax[i].

相似的我们可以求出lmax[i],lmin[i],rmax[i]。对于i ,若a[i]>0,其最大贡献就是sum(max(L[i]+1,lmax[i]),min(R[i]-1,rmax[i])*a[i]。负数类似。

这样题目就做完啦,时间复杂度O(n)

代码:

 #include<bits/stdc++.h>
using namespace std;
#define N 3000050
#define INF 0x7f7f7f7f
#define ll long long
template<typename T>void read(T&x)
ll n,a[N],s[N],sk[N],L[N],R[N];
ll Rmin[N],Lmin[N],Rmax[N],Lmax[N],ans,top;
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
ios::sync_with_stdio(false);
cin>>n;
for(ll i=;i<=n;i++)cin>>a[i],s[i]=a[i]+s[i-];
ans=a[]*a[];
sk[top=]=;
a[]=-INF-;
a[n+]=-INF;
for(ll i=;i<=n+;i++)
{
while(a[i]<a[sk[top]])
R[sk[top--]]=i;
L[i]=sk[top];
sk[++top]=i;
}
Lmin[]=;
Lmax[]=;
Rmin[n]=n;
Rmax[n]=n;
for(ll i=;i<=n;i++)
{
if (s[i-]-s[Lmin[i-]-]>)Lmin[i]=i;
else Lmin[i]=Lmin[i-];
if (s[i-]-s[Lmax[i-]-]<)Lmax[i]=i;
else Lmax[i]=Lmax[i-];
}
for(ll i=n-;i>=;i--)
{
if (s[Rmin[i+]]-s[i]>)Rmin[i]=i;
else Rmin[i]=Rmin[i+];
if (s[Rmax[i+]]-s[i]<)Rmax[i]=i;
else Rmax[i]=Rmax[i+];
}
ll l,r;
for(ll i=;i<=n;i++)
{
if (a[i]<)
{
l=max(L[i]+,Lmin[i]);
r=min(R[i]-,Rmin[i]);
}
else
{
l=max(L[i]+,Lmax[i]);
r=min(R[i]-,Rmax[i]);
}
ans=max((s[r]-s[l-])*a[i],ans);
}
cout<<ans;
}

南昌网络赛 I. Max answer 单调栈的更多相关文章

  1. 2019ICPC南昌邀请赛网络赛 I. Max answer (单调栈+线段树/笛卡尔树)

    题目链接 题意:求一个序列的最大的(区间最小值*区间和) 线段树做法:用单调栈求出每个数两边比它大的左右边界,然后用线段树求出每段区间的和sum.最小前缀lsum.最小后缀rsum,枚举每个数a[i] ...

  2. 南昌网络赛 I. Max answer (单调栈 + 线段树)

    https://nanti.jisuanke.com/t/38228 题意给你一个序列,对于每个连续子区间,有一个价值,等与这个区间和×区间最小值,求所有子区间的最大价值是多少. 分析:我们先用单调栈 ...

  3. 网络赛 I题 Max answer 单调栈+线段树

    题目链接:https://nanti.jisuanke.com/t/38228 题意:在给出的序列里面找一个区间,使区间最小值乘以区间和得到的值最大,输出这个最大值. 思路:我们枚举每一个数字,假设是 ...

  4. 南昌邀请赛I.Max answer 单调栈+线段树

    题目链接:https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value of a in ...

  5. The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)

    题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...

  6. HDU 5033 Building(北京网络赛B题) 单调栈 找规律

    做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time L ...

  7. The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈

    题面 题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值 题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok 我们 ...

  8. 线段树+单调栈+前缀和--2019icpc南昌网络赛I

    线段树+单调栈+前缀和--2019icpc南昌网络赛I Alice has a magic array. She suggests that the value of a interval is eq ...

  9. dp--2019南昌网络赛B-Match Stick Game

    dp--2019南昌网络赛B-Match Stick Game Xiao Ming recently indulges in match stick game and he thinks he is ...

随机推荐

  1. select sum也会返回null值

    SELECT  SUM(detail.VAL)  FROM   AI_SDP_ORDER_MONTH_DETAIL_201706    detail 如果所有的VAL都是null的话,或者根本就不存在 ...

  2. Delphi三层开发小技巧:TClientDataSet的Delta妙用

    Delphi三层开发小技巧:TClientDataSet的Delta妙用 转载 2014年10月13日 09:41:14 标签: 三层 / ClientDataSet 318 from :http:/ ...

  3. 封装basedao及动态创建新类型的java数组

    package com.huawei.base; import java.io.Serializable;import java.lang.reflect.Array;import java.lang ...

  4. solr配置相关:约束文件及引入ik分词器

    schema.xml: solr约束文件 Solr中会提前对文档中的字段进行定义,并且在schema.xml中对这些字段的属性进行约束,例如:字段数据类型.字段是否索引.是否存储.是否分词等等 < ...

  5. 06.Lucen入门程序-Field

    需求: 实现一个歌词搜索系统,通过关键字搜索,凡是文件名或文件内容包括关键字的文件都要找出来. 注意:该入门程序只对文本文件(.txt)搜索. Lucene中包含两个重要的类: IndexWriter ...

  6. Nginx源码完全注释(5)core/ngx_cpuinfo.c

    /* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #include <ngx_config.h> #include ...

  7. 75. Sort Colors (Array)

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. react.js 各种小测试笔记

    首先看一个 基础html  至于其中的 js 问价大家去官网下载就好了. <html> <head> <script src="../build/react.j ...

  9. PythonScripter2.7报错ascii codec can't encode characters in position 0-1:ordinal not in range(128)

    1. 这是Python 2 mimetypes的bug2. 需要将Python2.7\lib\mimetypes.py文件中如下片段注释或删除:try: ctype = ctype.encode(de ...

  10. arping

    强制交换机刷新MAC arping -I em2(网卡名称) 58.215.88.8(Vip)