uva 1619 - Feel Good || poj 2796 单调栈

本文介绍了一种计算人类生活期间情绪价值的方法。通过为每一天分配一个非负整数来表示其情绪价值,进而计算出某段时期的整体价值。文章提供了一个算法实现,使用单调栈来找出具有最大乘积(区间和与区间内最小值乘积)的时间段。

1619 - Feel Good

Time limit: 3.000 seconds

 
Bill is developing a new mathematical theory for human emotions. His recent investigations are dedi-
cated to studying how good or bad days in uent 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 day was. 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 re ects that good on average period can be greatly spoiled by one very bad day.
Now Bill is planning to investigate his own life and nd the period of his life that had the greatest
value. Help him to do so.
Input
The input will contain several test cases, each of them as described below. Consecutive test cases are
separated by a single blank line.
The rst line of the input le contains
n
| the number of days of Bill's life he is planning to
investigate (1
n
100000). The rest of the le contains
n
integer numbers
a
1
;a
2
;:::;a
n
ranging
from 0 to 10
6
| the emotional values of the days. Numbers are separated by spaces and/or line breaks.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases
will be separated by a blank line.
On the rst line of the output le print the greatest value of some period of Bill's life.
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.
SampleInput
6
3 1 6 4 5 2
SampleOutput
60
3 5
题意:得到max(一个区间的sum*min);
思路:遍历这个序列,得到以该点为最小值的区间;
   单调栈实现;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 100000007
#define esp 0.00000000001
const int N=1e5+10,M=1e6+10,inf=1e9;
ll d[N];
ll a[N];
ll l[N];
ll r[N];
ll sum[N];
void init(ll x)
{
    ll k=0;
    a[0]=-1;
    a[x+1]=-1;
    for(ll i=1;i<=x;i++)
    sum[i]=sum[i-1]+a[i];
    k=0;
    d[++k]=0;
    for(ll i=1;i<=x;i++)
    {
        while(a[d[k]]>=a[i])k--;
        l[i]=d[k];
        d[++k]=i;
    }
    k=0;
    d[++k]=x+1;
    for(ll i=x;i>=1;i--)
    {
        while(a[d[k]]>=a[i])k--;
        r[i]=d[k];
        d[++k]=i;
    }
}
int main()
{
    ll x,y,z,i,t;
    int flag=0;
    while(~scanf("%lld",&x))
    {
        if(flag)
        printf("\n");
        flag++;
        for(i=1;i<=x;i++)
        scanf("%lld",&a[i]);
        init(x);
        ll ans=0;
        ll ansl=1,ansr=1;
        for(i=1;i<=x;i++)
        {
            ll k=(sum[r[i]-1]-sum[l[i]])*a[i];
            if(k>ans)
            {
                ans=k;
                ansl=l[i]+1;
                ansr=r[i]-1;
            }
        }
        printf("%lld\n",ans);
        printf("%lld %lld\n",ansl,ansr);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jhz033/p/5658493.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值