POJ2796---Feel Good(前缀和+单调栈维护)

来源:互联网 发布:mysql 删除表恢复 编辑:程序博客网 时间:2024/06/10 13:12

POJ2796---FeelGood(前缀和+单调栈维护)

Description 
Bill is developing a new mathematical theory for human emotions. His recentinvestigations are dedicated to studying how good or bad days influent people
’smemories about some period of life.

A new idea Bill hasrecently developed assigns a non-negative integer value to each day of humanlife.

Bill calls thisvalue the emotional value of the day. The greater the emotional value is, thebetter the daywas. Bill suggests that the value of some period of human life isproportional to the sum of the emotional values of the days in the givenperiod, multiplied by the smallest emotional value of the day in it. This schemareflects that good on average period can be greatly spoiled by one very badday.

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

Input 
The first line of the input contains n - the number of days of Bill
’slife he is planning to investigate(1 <= n <= 100 000). The rest of thefile contains n integer numbers a1, a2,… an rangingfrom 0 to 106 - the emotional values of the days. Numbers are separated byspaces 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-thto r-th day of Bill’s life(inclusive) has the greatestpossible value. If there are multiple periods with the greatest possiblevalue,then print any one of them.

Sample Input


3 1 6 4 5 2

Sample Output

60 
3 5

 

#include<bits/stdc++.h>using namespace std;const int N=100001;typedef long long ll;int a[N],L[N],R[N],s[N],top;ll sum[N],ans=-1,n;ll l=1,r=1;int main(){        freopen("feelgood.in","r",stdin);    freopen("feelgood.out","w",stdout);    ios::sync_with_stdio(false);    cin>>n;    memset(sum,0,sizeof(sum));    for(int i=1;i<=n;i++)    {        cin>>a[i];        sum[i]=sum[i-1]+a[i];    }    top=0;    for(int i=1;i<=n;i++)    {        while(top&&a[s[top]]>=a[i])            --top;        L[i]=(top==0?1:s[top]+1);        s[++top]=i;    }    top=0;    for(int i=n;i>=1;i--)    {        while(top&&a[s[top]]>=a[i])            --top;        R[i]=(top==0?n:s[top]-1);        s[++top]=i;    }    for(int i=1;i<=n;i++)    {        ll temp=(sum[R[i]]-sum[L[i]-1])*a[i];        if(temp>ans) {ans=temp;r=R[i],l=L[i];}    }     cout<<ans<<endl<<l<<" "<<r<<endl;}


0 0
原创粉丝点击