1. 程式人生 > >POJ2796 Feel Good -- 單調隊列

POJ2796 Feel Good -- 單調隊列

inf possible 最小 -s tip sum ive size esc

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

題意:
  給出一個數列,求其一個區間中 最小值 與 區間元素和 的成績的最大值;
分析:
  1.將每個元素看做所在區間的最小值 向左右兩區間進行查找,找以其為最小值的最大區間;
  2.單調隊列的應用,以查找以當前元素為最小值的最大區間的左端點為例:
   ①構造嚴格遞增的單調隊列,即進隊元素需比隊尾元素大,否則隊尾元素出隊;
   ②從第一個元素開始進行進隊,將當前值與隊尾進行比較,若隊尾大於當前元素,則隊尾出隊,否則隊尾元素的下標便是以當前元素為最小值
    的最大區間的左端點;
    查找以當前元素為最小值的最大區間的右端點方法相同。
代碼分析:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <vector>
 7 #include <map>
 8 #include<string.h>
 9 #include<stack>
10 #include<set>
11 #include <queue>
12 using namespace std;
13 
14 long long a[100005];
15 //數組模擬隊列
16 long long q[100005];     
17 //以每個元素為最小值的最大區間左端點
18 long long l[100005];
19 //以每個元素為最小值的最大區間右端點
20 long long r[100005];
21 //存隊列中每個元素的下標
22 long long p[100005];
23 //區間的值
24 long long sum[100005];
25 int main()
26 {
27     int n,i,j;
28     while(~scanf("%d",&n))
29     {
30         sum[0] = 0;
31         for(i = 1; i<=n; i++)
32         {
33             scanf("%lld",a+i);
34             sum[i] = sum[i-1]+a[i];
35         }
36         //初始化隊頭
37         q[0] = -1;
38         p[0] = 0;
39         p[n+1] = n+1;
40         q[n+1] = -1;
41         int head = 1;
42         int tail = 0;
43         //查找以當前元素為最小值的最大區間的左端點
44         for(i = 1 ; i<=n; i++)
45         {
46             
47             while(head<=tail&&q[tail]>=a[i]) tail--;//隊尾元素大於等於當前元素
48             //以當前元素為最小值的最大區間的左端點
49             l[i] = p[tail];
50             //當前元素進隊
51             q[++tail] = a[i];
52             //記錄下標
53             p[tail] =  i;
54         }
55         //查找以當前元素為最小值的最大區間的右端點
56         q[0] = -1;
57         p[0] = 0;
58         p[n+1] = n+1;
59         q[n+1] = -1;
60         head = n;
61         tail = n+1;
62         for(i = n ; i>=1; i--)
63         {
64             while(head>=tail&&q[tail]>=a[i]) tail++;
65             r[i] = p[tail];
66             q[--tail] = a[i];
67             p[tail] =  i;
68         }
69         long long max1 = -1;
70         int k = 0;  //標記最大值的區間
71         for(i = 1; i<=n; i++)
72         {
73 
74          if(max1<a[i]*(sum[r[i]-1]-sum[l[i]]))
75          {
76              max1= a[i]*(sum[r[i]-1]-sum[l[i]]);
77              k = i;
78          }
79         }
80         printf("%lld\n",max1);
81         printf("%lld %lld\n",l[k]+1,r[k]-1);
82 
83     }
84     return 0;
85 }

個人隨筆,望大佬勿噴,若能提供幫助不勝榮幸。

POJ2796 Feel Good -- 單調隊列