2019南昌icpc网络赛 I. Max answer (单调栈)

本文探讨了一个有趣的算法挑战,即在给定的整数序列中找到一个子序列,使其最小值与所有元素和的乘积达到最大。文章分析了问题的解决策略,包括使用单调栈预处理数组,以及正数和负数的独立考虑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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×10
5
).

Second line contains nn integers represent the array a (-10^5 \le a_i \le 10^5)a(−10
5
≤a
i

≤10
5
).

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

样例输入 复制
5
1 2 3 4 5
样例输出 复制
36

题意:给以一段整数序列,寻找某一子序列,使得(子序列中的最小值乘以子序列所有元素和)的值最大,这个题和poj 2796很像,但是要注意这个题序列值有可能为负数
分析:对于每一个数,假设他是最小值,求出它的最优区间(用单调栈预先将每一个数左右第一个比他小的数的位置算出),正数负数分别考虑。
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define d(x) cout << (x) << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int mod = 1e9 + 7;
const int N = 5e5 + 10;

int n, x, y;
ll maxn = -INF;
int a[N];	
int b[N];	//用单调栈时会改变数组的值,用b数组存一下
int tmp[N];
ll sum[N];
P pre[N];	//保存每个数左右第一个比他小的数的下标
stack<int> s;

void init()     //先算出每个值向左向右第一个比他小的数出现的位置
{
    a[n + 1] = -INF;
    for (int i = 1; i <= n+1; i++){
        if(s.empty() || a[i] >= a[s.top()]){
            s.push(i);
            tmp[i] = i;
        }else{
            int top;
            ll ans;
            while(!s.empty() && a[i] < a[s.top()]){
                top = s.top();
                s.pop();
                pre[tmp[top]] = make_pair(top-1, i - 1);
            }
            s.push(top);
            tmp[top] = i;
            a[top] = a[i];
        }
    }
    // for(int i = 1; i <= n; i++){
    //     printf("%d %d\n", pre[i].first, pre[i].second);
    // }
}

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &a[i]);
        b[i] = a[i];
        sum[i] = sum[i - 1] + a[i];     //前缀和数组
    }
    init();
    for(int i = 1; i <= n; i++){
        if(b[i] > 0){
            ll ans = (sum[pre[i].second] - sum[pre[i].first]) * b[i];
            maxn = max(maxn, ans);
            // d(maxn);
        }else if(b[i] < 0){
            ll ansl = 0;
            ll ansr = 0;
            ll ansll = 0;
            ll ansrr = 0;
            for (int j = i - 1; j >= 1; j--){
                if(j < 0)
                    break;
                if(b[j] < b[i]){
                    break;
                }else{
                    ansl += b[j];
                    ansll = min(ansll, ansl);
                }
            }
            for (int j = i + 1; j <= n; j++){
                if(j == n)
                    break;
                if(b[j] < b[i]){
                    break;
                }else{
                    ansr += b[j];
                    ansrr = min(ansrr, ansr);
                }
            }
            maxn = max(maxn, (ansll + ansrr + b[i]) * b[i]);
            // d(maxn);
        }else{
            maxn = max(maxn, ll(0));
        }
    }
    printf("%lld\n", maxn);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值