POJ 2796: feel Good (单调栈)

来源:互联网 发布:淘宝有没有隐形降权 编辑:程序博客网 时间:2024/06/05 03:03
Feel Good
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 14820 Accepted: 4104Case 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

Source

Northeastern Europe 2005


题意理解]:  题目大致是说给定一个数n;然后输入n个数,求一个连续区间,使得这个区间的元素加起来再乘以这个区间里最小的数

所得到的结果是所有可能中最大的。 开始我没有想到单调栈,一直在想用队列取解决,想了一个下午都没想出来,伤心到吐血。
       
最后突然想到用单调栈来解决这个问题。


[大致思路]:  我们需要记录每个元素,所能到达的左边界和右边界;先说说左边界,一个元素往左扩展遇到比他的的元素就更新一下

左边界的值,遇到比他小的元素就停止搜索(仔细思考一下题意便能明白),右边界同理,可是这样的时间复杂度是n²,肯定会超时

。那么就要用到单调栈了。

       主要思想就是,最开始初始化每个元素的左右边界都是他们自己,当要压入一个元素的时候,若栈为空,则直接压入,若不

是,就一直比较栈首元素和要压入元素的大小,如果要压入的元素比栈首元素小,就将队首元素弹出,并更新要压入元素左边界的

值,直到遇到比该要压入元素小的元素或者栈为空时,最后压入该元素,同理右边界值就是反向扫一遍也就可以更新出来。

       最开始,我以为我这样做一定是不会超时了,可是我一提交Time Limit Exceeded,我顿时傻眼,到底是哪里出了问题呢,后来我

终于发现原来,我出问题在最后的算每个区间的值的时候,超的时,我们需要最开始就用一个sum的数组,记录一下,sum[i]就表示

前i个元素的和;到时候知道左右区间,一减便是这个区间里的元素和,这个区间的最小元素也就是显而易见了,也不用一个个去

找,因为就是你从这个元素往两边走所得到的区间,所以最小数就是这个元素。
       [代码]:
#include<stdio.h>#include<stack>#include<math.h>#include<algorithm>using namespace std;struct s{int x;int l;//记录左端点 int r;//记录右端点 }p[100010];long long sum[100010];int main(){int n;scanf("%d",&n);stack<s>st;for(int i=1;i<=n;i++)  //更新左断点 {scanf("%d",&p[i].x);p[i].l=i;sum[i]+=sum[i-1]+p[i].x;if(st.empty()){    st.push(p[i]);}else{if(st.top().x>=p[i].x)//如果要压入的元素小于等于队首元素 {while(st.top().x>=p[i].x)//直到找到栈中比要压入的元素小的元素,或者栈为空 {p[i].l=st.top().l;//更新左端点 st.pop();if(st.empty()){break;}}st.push(p[i]);}else{st.push(p[i]);}}}while(!st.empty())//清空栈 {st.pop();}for(int i=n;i>0;i--) //更新右断点 {p[i].r=i;if(st.empty()){    st.push(p[i]);}else{if(st.top().x>=p[i].x) //如果要压入的元素小于等于队首元素 {while(st.top().x>=p[i].x) //直到找到栈中比要压入的元素小的元素,或者栈为空 {p[i].r=st.top().r;  //更新右端点 st.pop();if(st.empty()){break;}}st.push(p[i]);}else{st.push(p[i]);}}}long long max=0;int ans_r,ans_l;for(int i=1;i<=n;i++) //寻找能找到最大值的区间 {long long temp=0;temp=sum[p[i].r]-sum[p[i].l-1];long long q=p[i].x;    temp=temp*q;if(temp>=max)   //更新结果 {max=temp;ans_r=p[i].r;    ans_l=p[i].l;}}printf("%lld\n%d %d\n",max,ans_l,ans_r);return 0;}