Feel Good POJ - 2796 (前缀和+单调栈)(详解)

本文介绍了一种算法,用于解决寻找给定数组中具有最大平衡值的区间问题。平衡值定义为区间内数值和乘以该区间内的最小值,旨在模拟人类生活期间情感价值的变化。通过构造辅助数组L和R,并使用单调栈,算法能在O(N)时间内找到最优区间。

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

Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life. 

A new idea Bill has recently developed assigns a non-negative integer value to each day of human life. 

Bill calls this value the emotional value of the day. The greater the emotional value is, the better the daywas. Bill suggests that the value of some period of human life is proportional to the sum of the emotional values of the days in the given period, multiplied by the smallest emotional value of the day in it. This schema reflects that good on average period can be greatly spoiled by one very bad day. 

Now Bill is planning to investigate his own life and find the period of his life that had the greatest value. Help him to do so.

Input

The first line of the input contains n - the number of days of Bill's life he is planning to investigate(1 <= n <= 100 000). The rest of the file contains n integer numbers a1, a2, ... an ranging from 0 to 10 6 - the emotional values of the days. Numbers are separated by spaces and/or line breaks.

Output

Print the greatest value of some period of Bill's life in the first line. And on the second line print two numbers l and r such that the period from l-th to r-th day of Bill's life(inclusive) has the greatest possible value. If there are multiple periods with the greatest possible value,then print any one of them.

Sample Input

6
3 1 6 4 5 2

Sample Output

60
3 5

中文题意:
给定一个含有N个正整数的数组,我们定义一个区间[L,R]的平衡值为这个区间的数值和*这个区间的最下值。
让求出这个数组的平衡值最大的区间,并输出区间的边界。

思路:
定义两个数组L和R,L[i]表示从a[i]向左遍历,第一个比a[i]小的右边那个数的下标。
R[i] 表示从a[i]向右遍历,第一个比a[i]小的左边的那个数的下标。
以上的L和R数组,都可以利用单调栈进行O(N)求出,。然后因为要用到区间和,所以预处理一下前缀和数组sum即可。
然后我们扫一遍数组,以a[i]为最小值的区间的平衡值就为 : a[i]*( sum[r[i]]-sum[l[i]-1] )
然后对应求出最大值和区间情况。

代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int l[maxn];
int r[maxn];
ll sum[maxn];
int  a[maxn];
int n;
int main()
{
    gg(n);
    repd(i,1,n)
    {
        gg(a[i]);
        sum[i]=sum[i-1]+1ll*a[i];
    }
    stack<int> s;
    // 1 2 3 4 5
    // 4
    while(s.size())
        s.pop();
    repd(i,1,n)
    {
        while(s.size()&&a[s.top()]>=a[i])
        {
            s.pop();
        }
        if(s.size())
        {
            l[i]=s.top()+1;
        }else
        {
            l[i]=1;
        }
        s.push(i);
    }
    while(s.size())
        s.pop();
    //  3 1 6 4 5 2
    //  4 -> 5  2
    int x;
    for(int i=n;i>=1;i--)
    {
        while(s.size()&&a[s.top()]>=a[i])
        {
            s.pop();
        }
        if(s.size())
        {
            r[i]=s.top()-1;
        }else
        {
            r[i]=n;
        }
        s.push(i);
    }
    ll ans=-1;
    int lf,ri;
    repd(i,1,n)
    {
        ll cnt=1ll*a[i]*(sum[r[i]]-sum[l[i]-1]);
        if(cnt>ans)
        {
            ans=cnt;
            lf=l[i];
            ri=r[i];
        }
    }
    printf("%lld\n",ans);
    printf("%d %d",lf,ri);
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

 


转载于:https://www.cnblogs.com/qieqiemin/p/10330637.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值