POJ2796 Feel Good 单调栈

来源:互联网 发布:java 字符串相等 编辑:程序博客网 时间:2024/05/16 14:34
Feel Good
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 15889 Accepted: 4394Case 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



【题意】

qwq乍一看想在讲故事喔,模拟一下样例就知道,题目要求求一个区间,首先这个区间里的数都要是正数(当然),其次就是使这个区间的和乘这个区间的最小值的值最大,当然这个区间越长越好。

【题解】
说好的spj,可我感觉怎么不大对呢疑问...第一思路当然是枚举这个区间的最小值,然后往左往右扩展,直到找到比它小的值为止。
这样做显然有点暴力,那就可以想想单调栈啊~
对于每个数入栈,将栈顶所有比这个数大的数弹掉,并且更新入栈的数的左端点,出栈后栈顶的元素的右端点也是要更新的(每次)qwq。
最后再压入栈。
当然最后要把这个栈弹光a.并且更新栈顶的右端点w。
剩下只要枚举最小值用前缀和维护一下就可以啦~
咳咳问一下为什么把(sum*a[i]>ans)改成(sum*a[i]>=ans)就对了捏#


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace std;int n,top,ansl,ansr,a[100100],sta[100100];long long s[100100],ans;struct leo{int l,r;}f[100100];inline void push(int x){if(x==1){sta[++top]=x;return;}while(a[sta[top]]>=a[x] && top){f[x].l=f[sta[top]].l;f[sta[top-1]].r=f[sta[top]].r;top--;}f[sta[top]].r=f[x].r;sta[++top]=x;}inline void pop(){f[sta[top-1]].r=f[sta[top]].r;top--;}int main(){scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]),s[i]=s[i-1]+a[i],f[i].l=f[i].r=i;for(int i=1;i<=n;i++) push(i);while(top) pop();for(int i=1;i<=n;i++){long long sum=s[f[i].r]-s[f[i].l-1];if(sum*a[i]>=ans){ans=sum*a[i];ansl=f[i].l;ansr=f[i].r;}}printf("%lld\n%d %d\n",ans,ansl,ansr);return 0;}


原创粉丝点击