Feel Good

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 10 6 - 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

  1. 6
  2. 3 1 6 4 5 2

Sample Output

  1. 60
  2. 3 5
  3.  
  4. 单调递增栈,遍历最小值,找左右两端点,前缀和求出区间和×最小值,更新最大解。
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stack>
  4. using namespace std;
  5. int main()
  6. {
  7. int n,i;
  8. long long max;
  9. long long a[],b[],l[],r[];
  10. stack<int> s;
  11. scanf("%d",&n);
  12. memset(a,,sizeof(a));
  13. memset(b,,sizeof(b));
  14. memset(l,,sizeof(l));
  15. memset(r,,sizeof(r));
  16. for(i=;i<=n;i++){
  17. scanf("%lld",&a[i]);
  18. b[i]+=b[i-]+a[i];
  19. }
  20. for(i=;i<=n;i++){
  21. while(s.size()&&a[s.top()]>=a[i]) s.pop();
  22. l[i]=s.size()==?:(s.top()+);
  23. s.push(i);
  24. }
  25. while(s.size()) s.pop();
  26. for(i=n;i>=;i--){
  27. while(s.size()&&a[s.top()]>=a[i]) s.pop();
  28. r[i]=s.size()==?n:(s.top()-);
  29. s.push(i);
  30. }
  31. max=;
  32. int ll=,rr=;
  33. for(i=;i<=n;i++){
  34. if((b[r[i]]-b[l[i]-])*a[i]>max){
  35. max=(b[r[i]]-b[l[i]-])*a[i];
  36. ll=l[i];
  37. rr=r[i];
  38. }
  39. }
  40. printf("%lld\n%d %d\n",max,ll,rr);
  41. return ;
  42. }

POJ - 2796 Feel Good 单调递增栈+前缀和的更多相关文章

  1. poj 2796 Feel Good单调栈

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20408   Accepted: 5632 Case T ...

  2. poj 2796 Feel Good 单调栈区间问题

    Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...

  3. QDUOJ LC的课后辅导 单调递增栈

    LC的课后辅导 发布时间: 2015年9月19日 21:42   时间限制: 1000ms   内存限制: 256M 描述 有一天,LC给我们出了一道题,如图: 这个图形从左到右由若干个 宽为1 高不 ...

  4. poj 2796 Feel Good 单调队列

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8753   Accepted: 2367 Case Ti ...

  5. POJ 2796 Feel Good 【单调栈】

    传送门:http://poj.org/problem?id=2796 题意:给你一串数字,需要你求出(某个子区间乘以这段区间中的最小值)所得到的最大值 例子: 6 3 1 6 4 5 2 当L=3,R ...

  6. POJ 2796:Feel Good(单调栈)

    http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...

  7. POJ 2796:Feel Good 单调栈

    题目,给定一个数列,n <= 1e5 .要求找出一个区间,使得其内区间最小值 * 区间总和的值最大,要求输出区间. 首先先维护一个单调递增的栈,同时记录一个lef值表示:lef[i]表示当前栈内 ...

  8. 线段树+单调栈+前缀和--2019icpc南昌网络赛I

    线段树+单调栈+前缀和--2019icpc南昌网络赛I Alice has a magic array. She suggests that the value of a interval is eq ...

  9. POJ 3415 后缀数组+单调栈

    题目大意: 给定A,B两种字符串,问他们当中的长度大于k的公共子串的个数有多少个 这道题目本身理解不难,将两个字符串合并后求出它的后缀数组 然后利用后缀数组求解答案 这里一开始看题解说要用栈的思想,觉 ...

随机推荐

  1. css3背景及字体渐变

    1.背景渐变: .linear { width: 100%; FILTER: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,sta ...

  2. Linux命令之ln软链接

    用途:链接文件 默认情况下,ln命令产生硬链接. 最常用的参数是-s(建立符号连接Symbolic Link,也叫软连接),具体用法是: ln-s 源文件 目标文件 当我们需要在不同的目录用到相同的文 ...

  3. Memcached服务端自动启动(转载)

    Memcached服务端自动启动 原文链接:http://www.cnblogs.com/technet/archive/2011/09/11/2173485.html  经测试,要使得Memcach ...

  4. springboot实战--笔记

    由于这本书看过一遍,所以这里是二次复习,记录的东西比较少,就不分章节了. 共12章,524页,预计时间是18h 第一章 spring基础: 第二章 spring常用配置: bean的Scope:sin ...

  5. ios上视频与音乐合成后出现播放兼容问题的解决方法

    近期EasyDarwin开源流媒体团队EasyVideoRecorder小组同学Carl在支持一款短视频应用上线时,遇到一个问题:我们在IOS上合成"图片+音乐"成为视频之后,在P ...

  6. Hive与impala的对比测试实验

    前面几篇随笔记录了我安装环境的一些笔记,环境ok以后,自然要看看impala到底性能如何,拿他来hive做做对比: 前面hive章节中,已经建立了一张名叫chengyeliang的table,该表的结 ...

  7. 给第三方apk进行系统签名的几种方式【转】

    本文转载自:http://blog.csdn.net/luzhenrong45/article/details/47733053 版权声明:本文为博主原创文章,未经博主允许不得转载. -------- ...

  8. 【CQ18高一暑假前挑战赛2】标程

    [昨晚打校赛,5个小时打完很累了,所以搞忘出题了...对不起学弟们,不过出的题都亲自写过一遍,可以保证题目和代码长度都不长,题目难度不大] [A:bush博弈] #include<bits/st ...

  9. bzoj 1798 Seq 维护序列seq —— 线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1798 这题还4A... 注意:cnt 从1开始:各种模 p:乘法标记初始值是 1:可能乘 0 ...

  10. HDU2444(二分图判定+最大匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...