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;
}

 

posted @ 2016-07-10 20:36  jhz033  阅读(297)  评论(0编辑  收藏  举报