poj 2796 Feel Good 单调队列

Feel Good
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8753   Accepted: 2367
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

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

 

 

 

 

  1 /*
  2 题意:10^5个数字,求max {  区间[  ] *  该区间最小值 };
  3 解决思路:
  4 找区间最小值,如果能够求出其长度,那么就可以扫一遍解决。
  5 关键在于找区间最小值。
  6 而且存在一种情况,同一个值,会是许多区间的最小值。
  7 所以,转化一下,求每一个i为最小值的时候,能到到达的最远位置。
  8             
  9 */
 10 
 11 #include<iostream>
 12 #include<stdio.h>
 13 #include<cstring>
 14 #include<cstdlib>
 15 using namespace std;
 16 
 17 int a[100002],b[100002];
 18 __int64 s[100002];
 19 int L[100002],R[100002];
 20 typedef struct
 21 {
 22     int num;
 23     int rp;
 24 }Queue;
 25 Queue q[100002],tmp;
 26 
 27 int main()
 28 {
 29     int n,i;
 30     int head,tail,Num,l,r,lx,rx;
 31     __int64 hxl,tom;
 32     while(scanf("%d",&n)>0)
 33     {
 34         for(i=1,s[0]=0;i<=n;i++)
 35         {
 36             scanf("%d",&a[i]);
 37             b[n-i+1]=a[i];
 38             s[i]=s[i-1]+a[i];
 39         }
 40         memset(L,-1,sizeof(L));
 41         memset(R,-1,sizeof(R));
 42         head=0;tail=-1;
 43         for(i=1;i<=n;i++)
 44         {
 45             tmp.num=i;
 46             tmp.rp=a[i];
 47             while( head<=tail && q[tail].rp>tmp.rp )
 48             {
 49                 if( R[q[tail].num]==-1)
 50                 {
 51                     R[q[tail].num]= i - q[tail].num-1;
 52                 }
 53                 tail--;
 54             }
 55             q[++tail]=tmp;
 56         }
 57        while( head<=tail )
 58         {
 59             Num=q[tail].num-q[head].num;
 60             if( R[q[head].num]==-1)
 61             {
 62                 R[q[head].num]=Num;
 63             }
 64             head++;
 65         }
 66         head=0;tail=-1;
 67         for(i=1;i<=n;i++)
 68         {
 69             tmp.num=i;
 70             tmp.rp=b[i];
 71             while( head<=tail && q[tail].rp>tmp.rp )
 72             {
 73                 Num=n-q[tail].num+1;
 74                 if( L[Num]==-1)
 75                     L[Num]=  i - q[tail].num-1;
 76                 tail--;
 77             }
 78             q[++tail]=tmp;
 79         }
 80         while( head<=tail )
 81         {
 82             Num=n-q[head].num+1;
 83             if( L[Num]==-1)
 84             {
 85                 L[Num]=q[tail].num-q[head].num;
 86             }
 87             head++;
 88         }
 89         for(tom=-1,i=1;i<=n;i++)
 90         {
 91             l=i-L[i];
 92             r=i+R[i];
 93             hxl=(s[r]-s[l-1])*a[i];
 94             if( hxl>tom)
 95             {
 96                 tom=hxl;
 97                 lx=l;
 98                 rx=r;
 99             }
100         }
101         printf("%I64d\n%d %d\n",tom,lx,rx);
102     }
103     return 0;
104 }

 

posted @ 2014-02-22 16:52  芷水  阅读(211)  评论(0编辑  收藏  举报