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

Northeastern Europe 2005

处理前缀和以及当前点向左最大数的位置和向右最大数的位置

 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 }
时间: 2024-10-05 14:30:38

POJ——T 2796 Feel Good的相关文章

【POJ】2796:Feel Good【单调栈】

Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18449   Accepted: 5125 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated

POJ 题目2796 Feel Good(动态规划)

Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11041   Accepted: 3020 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated

[poj 2796]单调栈

题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include<algorithm> #include<stack> using namespace std; const int maxn=100005; int a[maxn]; int l[maxn]; int r[maxn]; long long pre[maxn]; stack< p

POJ 2796 Feel Good(单调栈)

题目地址:POJ 2796 单调栈的第一题就是这道..把我弄的晕头转向.现在终于明白了,对单调栈又加深了理解.原来单调栈不只是可以维护数. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #incl

Poj 2796 单调栈

关于单调栈的性质,和单调队列基本相同,只不过单调栈只使用数组的尾部, 类似于栈. Accepted Code: 1 /************************************************************************* 2 > File Name: 2796.cpp 3 > Author: Stomach_ache 4 > Mail: [email protected] 5 > Created Time: 2014年07月21日 星期一

Feel Good (poj 2796)

题目大意就是在给出的串中找出一段连续数字,使得 这一段的和 乘上 这一段最小的数 的结果最大. 可以用rmq做.每次区间找当中最小的数,算出值并记录位置.然后再递推它的左右区间. 不过- -,一开始用深搜递推RE了.栈空间不够了,然后慢慢优化,最后还是ac了. 貌似这一题是用单调栈做的,还可以用查并集做...我也是醉了 具体看代码吧 1 /************************************************************************* 2 > F

51nod 1215 数组的宽度&amp;poj 2796 Feel Good(单调栈)

单调栈求每个数在哪些区间是最值的经典操作. 把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间. poj2796 弹出的时候更新答案即可 #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #include<cmath

POJ 2796 Feel Good

传送门 Time Limit: 3000MS Memory Limit: 65536K 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

POJ 2796 Feel Good(并查集)

题目链接:点击打开链接 思路: 该题转化一下, 就是枚举每一个数, 找到以这个数为最小值的最大区间(因为没有负数).  那么一个办法是预处理出每一个数左边第一个比他大的数的位置, 和右边第一个比他大的数的位置, 这个可以用构造单调栈的线性算法处理出来: 我们构造一个单调上升栈, 标记栈里每个元素在实际中的位置, 加入一个元素a[i]的时候, 如果栈顶元素大于他, 那么将栈顶元素出队列, i就是这个元素右边大于他的第一个元素.  直到栈顶元素<=a[i],这时,栈顶元素就是a[i]左边第一个>=