南昌网络赛 I 题,max answer(单调栈 + 线段树)

max answer

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

题意:给定一个数组,对于一个区间 [l , r] 有一个值 X ,为区间 [l , r] 的最小值 * 该区间和,求最后值 X 的最大值。

思路:假定所有 a[i] 的值都为非负数,那么解决方案就是 假设每一个值都是一个区间的最小值,通过单调栈可以求出这个区间。那么这里的问题就是 a[i] 的值是可以为负数的,负数的情况,肯定就是该区间的和最小,所以我们可通过一颗线段树维护前缀和,该点右边的最小区间和 - 该点左边的最大区间和 即可。

AC代码:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)

using namespace std;

const int maxn = 5e5 + 10;
const LL inf = 0x3f3f3f3f3f3f3f3f;
int a[maxn],n;
LL preSum[maxn];
int L[maxn],R[maxn];
stack<int> s;
LL maxx[maxn * 4], minn[maxn * 4];

void init(){
    while(!s.empty()) s.pop();
    for(int i = 1;i <= n;++ i){
        if(s.empty()) s.push(i);
        else {
            while(!s.empty() && a[s.top()] > a[i]){
                R[s.top()] = i;
                s.pop();
            }
            s.push(i);
        }
    }
    while(!s.empty()) R[s.top()] = n + 1,s.pop();
//    for(int i = 1;i <= n;++ i)
//        debug(R[i]);
    for(int i = n;i >= 1;-- i){
        if(s.empty()) s.push(i);
        else {
            while(!s.empty() && a[s.top()] > a[i]){
                L[s.top()] = i;
                s.pop();
            }
            s.push(i);
        }
    }
    while(!s.empty()) L[s.top()] = 0,s.pop();
//    for(int i = 1;i <= n;++ i)
//        debug(L[i]);
    for(int i = 0;i < maxn * 4;++ i) maxx[i] = -inf,minn[i] = inf;
}

void build(int l,int r,int i){
    if(l == r){
        maxx[i] = preSum[l];
        minn[i] = preSum[l];
        return ;
    }
    int mid = (l + r) >> 1;
    build(l,mid,i << 1);
    build(mid + 1,r,i << 1 | 1);
    maxx[i] = max(maxx[i << 1],maxx[i << 1 | 1]);
    minn[i] = min(minn[i << 1],minn[i << 1 | 1]);
}

LL query(int l,int r,int ql,int qr,int i,bool f){
    if(ql <= l && r <= qr){
        if(f) return maxx[i];
        else return minn[i];
    }
    int mid = (l + r) >> 1;
    LL ans = (f ? -inf : inf);
    if(ql <= mid){
        if(f) ans = max(ans,query(l,mid,ql,qr,i << 1,f));
        else ans = min(ans,query(l,mid,ql,qr,i << 1,f));
    }
    if(qr > mid){
        if(f) ans = max(ans,query(mid + 1,r,ql,qr,i << 1 | 1,f));
        else ans = min(ans,query(mid + 1,r,ql,qr,i << 1 | 1,f));
    }
    return ans;
}

int main() {
    scanf("%d",&n);
    rep(i,1,n + 1) scanf("%d",&a[i]);
    rep(i,1,n + 1) preSum[i] = preSum[i - 1] + (LL) a[i];
    init();
    build(1,n,1);
    LL ans = -inf;
    for(int i = 1;i <= n;++ i){
        if(a[i] >= 0)
            ans = max(ans,(preSum[R[i] - 1] - preSum[L[i]]) * a[i]);
        else {
            ans = max(ans,(query(1,n,i,R[i] - 1,1,0) - query(1,n,L[i] + 1,i,1,1) + (L[i] + 1 == R[i] - 1)) * a[i]);
        }
    }
    printf("%lld\n",ans);
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值