2018南昌邀请赛网络赛 单调栈 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

 

给出一个数列 请你选则任意一个区间,使区间中的最小值*区间的和最大

首先对于一个5e5的数列 我们不可能把数列一一枚举

所以我们采用单调栈 首先预处理出每个数字多管控的范围

然后对于每个数字分正负讨论即可

正数:所有数累加在一起

负数:查一个区间使区间和最小即可(使用线段树)

code:

#include<bits/stdc++.h>
using namespace std;
int a[500005];
int l[500005];
int r[500005];
stack<int> st;
long long s[500005];
long long MIN[500005*4],MAX[500005*4];
void build(int root,int l,int r)
{
    if(l==r)
    {
        MIN[root]=s[l];
        MAX[root]=s[r];
        return ;
    }
    int mid=(l+r)/2;
    build(root*2,l,mid);
    build(root*2+1,mid+1,r);
    MIN[root]=min(MIN[root*2],MIN[root*2+1]);
    MAX[root]=max(MAX[root*2],MAX[root*2+1]);
    return;
}
long long q(int root,int l,int r,int ll,int rr,int tp)
{
    if(ll<=l&&r<=rr)
    {
        if(tp==0) return MIN[root];
        if(tp==1) return MAX[root];
    }
    int mid=(l+r)/2;
    if(rr<=mid) return q(root*2,l,mid,ll,rr,tp);
    else if(ll>mid) return q(root*2+1,mid+1,r,ll,rr,tp);
    else
    {
        if(tp==0) return min(q(root*2,l,mid,ll,rr,tp),q(root*2+1,mid+1,r,ll,rr,tp));
        if(tp==1) return max(q(root*2,l,mid,ll,rr,tp),q(root*2+1,mid+1,r,ll,rr,tp));
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=n; i>=1; i--)
    {
        while(st.size()&&a[st.top()]>=a[i])
        {
            st.pop();
        }
        if(st.size()==0)
        {
            r[i]=n;
        }
        else
        {
            r[i]=st.top()-1;
        }
        st.push(i);
    }
    while(st.size())
    {
        st.pop();
    };
    for(int i=1; i<=n; i++)
    {
        while(st.size()&&a[st.top()]>=a[i])
        {
            st.pop();
        }
        if(st.size()==0)
        {
            l[i]=1;
        }
        else
        {
            l[i]=st.top()+1;
        }
        st.push(i);
    }
    s[0]=0;
    for(int i=1; i<=n; i++)
    {
        s[i]=s[i-1]+a[i];
    }
    build(1,0,n);
    long long ans=-1e18;
    for(int i=1;i<=n;i++)
    {
        if(a[i]>=0)
        {
            ans=max(ans,a[i]*(s[r[i]]-s[l[i]-1]));
//            printf("%d %d\n",1,ans);
        }
        else
        {
            long long tMIN=q(1,0,n,i,r[i],0);
            long long tMAX=q(1,0,n,l[i]-1,i,1);
//            cout<<tMIN<<" "<<tMAX<<endl;
            ans=max(ans,a[i]*(tMIN-tMAX));
        }
    }
    printf("%lld\n",ans);
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值