当前位置: 代码迷 >> 综合 >> POJ 2796 Feel Good
  详细解决方案

POJ 2796 Feel Good

热度:87   发布时间:2024-02-10 07:21:45.0

POJ 2796 Feel Good

POJ 2796

题目

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.


输入

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.


输出

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.


样例

input
6
3 1 6 4 5 2

output
60
3 5


题意

区间的最小值乘区间所有数的和是区间的权值
求最大的区间权值并输出区间的范围


解题思路

将当前定义为区间的最小值
做两遍单调栈
一次从1~ n n 找左边比i大的最后一个数
存进l数组
一次从 n n ~1找右边比i大的最后一个数
存进r数组
区间权值= a a [ i i ]*( s s [ r r [ i i ]]- s s [ l l [ i i ]-1])
a n s ans 求最大


代码

#include<iostream>
#include<cstdio>
using namespace std;
long long n,x,y,t,ans=-1,a[100010],s[100010],q[100010],l[100010],r[100010];
int main()
{scanf("%lld",&n);for (int i=1;i<=n;i++){scanf("%lld",&a[i]);s[i]=s[i-1]+a[i];  //前缀和l[i]=i;r[i]=i;}for (int i=1;i<=n;i++){while (t>0&&a[q[t]]>=a[i])  //左边的比i大的出队{ l[i]=l[q[t]];  //保存左边最后一个比i大的数t--;  }q[++t]=i;}t=0;for (int i=n;i>0;i--){while (t>0&&a[q[t]]>=a[i])  //右边的比i大的出队{ r[i]=r[q[t]];  //保存右边最后一个比i大的数t--;}q[++t]=i;}for (int i=1;i<=n;i++)if (ans<a[i]*(s[r[i]]-s[l[i]-1])){ans=a[i]*(s[r[i]]-s[l[i]-1]);  //求权值,更新答案x=l[i];y=r[i];  //保存区间的范围}cout<<ans<<endl;cout<<x<<" "<<y;return 0;
}
  相关解决方案