POJ2796 Feel Good -- 单调队列

来源:互联网 发布:大学生推荐书目 知乎 编辑:程序博客网 时间:2024/05/17 03:40


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

63 1 6 4 5 2

Sample Output

603 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 }
复制代码

 

 

个人随笔,望大佬勿喷,若能提供帮助不胜荣幸。

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

63 1 6 4 5 2

Sample Output

603 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 }
复制代码

 

 

个人随笔,望大佬勿喷,若能提供帮助不胜荣幸。

原创粉丝点击