POJ——T 2796 Feel Good

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ——T 2796 Feel Good相关的知识,希望对你有一定的参考价值。

http://poj.org/problem?id=2796

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 15375   Accepted: 4252
Case Time Limit: 1000MS   Special Judge

Description

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 106 - 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

Source

 
处理前缀和以及当前点向左最大数的位置和向右最大数的位置
 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 #define LL long long
 8 const int N(100000+5);
 9 int ans_l,ans_r,l[N],r[N];
10 LL n,ans_val=-1,sum[N],val[N];
11 
12 inline void read(LL &x)
13 {
14     x=0; LL ch=getchar();
15     for(;ch>9||ch<0;) ch=getchar();
16     for(;ch>=0&&ch<=9;ch=getchar()) x=ch-0+x*10;
17 }
18 
19 int main()
20 {
21     read(n);
22     for(int i=1;i<=n;i++)
23     {
24         read(val[i]),l[i]=r[i]=i;
25         sum[i]=sum[i-1]+val[i];
26     }
27     for(int i=2;i<=n;i++)
28       for(;l[i]>1&&val[l[i]-1]>=val[i];)
29         l[i]=l[l[i]-1];
30     for(int i=n-1;i>=1;i--)
31       for(;r[i]<n&&val[r[i]+1]>=val[i];)
32         r[i]=r[r[i]+1];
33     for(int i=1;i<=n;i++)
34     {
35         LL tmp=val[i]*(sum[r[i]]-sum[l[i]-1]);
36         if(tmp>ans_val)
37         {
38             ans_l=l[i];
39             ans_r=r[i];
40             ans_val=tmp;
41         }
42     }
43     printf("%I64d\n%d %d",ans_val,ans_l,ans_r);
44     return 0;
45 }

 

以上是关于POJ——T 2796 Feel Good的主要内容,如果未能解决你的问题,请参考以下文章

POJ.2796feel good(单调栈)

POJ.2796feel good(单调栈)

POJ2796 Feel Good -- 单调队列

POJ2796Feel Good[单调栈]

POJ2796:Feel Good单调栈

poj2796 feel good 单调栈