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

6
3 1 6 4 5 2

Sample Output

60
3 5 单调递增栈,遍历最小值,找左右两端点,前缀和求出区间和×最小值,更新最大解。
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
int n,i;
long long max;
long long a[],b[],l[],r[];
stack<int> s;
scanf("%d",&n);
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(l,,sizeof(l));
memset(r,,sizeof(r));
for(i=;i<=n;i++){
scanf("%lld",&a[i]);
b[i]+=b[i-]+a[i];
}
for(i=;i<=n;i++){
while(s.size()&&a[s.top()]>=a[i]) s.pop();
l[i]=s.size()==?:(s.top()+);
s.push(i);
}
while(s.size()) s.pop();
for(i=n;i>=;i--){
while(s.size()&&a[s.top()]>=a[i]) s.pop();
r[i]=s.size()==?n:(s.top()-);
s.push(i);
}
max=;
int ll=,rr=;
for(i=;i<=n;i++){
if((b[r[i]]-b[l[i]-])*a[i]>max){
max=(b[r[i]]-b[l[i]-])*a[i];
ll=l[i];
rr=r[i];
}
}
printf("%lld\n%d %d\n",max,ll,rr);
return ;
}

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. bootstrap table api

    http://blog.csdn.net/rickiyeat/article/details/56483577

  2. sublime 实用快捷键

    Command+Enter 在下一行插入新行.举个栗子:即使光标不在行尾,也能快速向下插入一行. Command+Shift+Enter 在上一行插入新行.举个栗子:即使光标不在行首,也能快速向上插入 ...

  3. JSP 随记

    jstl <c:forEach> 遍历,多个<option>时显示"全部".单个 option时,默认选中! 引入:<%@ taglib prefix ...

  4. Oracle比较时间大小

    1,比较当前时间与指定时间相差分钟数:    select sysdate,  sysdate - to_date('2007-04-03 13:45:39','yyyy-mm-dd hh24:mi: ...

  5. Greenplum使用简明手册

    GP服务启停 su - gpadmin gpstart #正常启动 gpstop #正常关闭 gpstop -M fast #快速关闭 gpstop –r #重启 gpstop –u #重新加载配置文 ...

  6. linux命令详解:file命令

    前言 file命令可以获取多种文件类型,包括文本文件.脚本文件.源码文件.多媒体文件(音频视频)等.file是通过查看文件的头部内容,来获取文件的类型,而不像Window那样是通过扩展名来确定文件类型 ...

  7. EasyRTMP实现将RTSP流转换成RTMP流实现RTSP直播转RTMP直播的功能

    本文转自EasyDarwin开源团队Kim的博客:http://blog.csdn.net/jinlong0603/article/details/52951311 EasyRTMP EasyRTMP ...

  8. Ubuntu上Eclipse安装PyDev方法和配置

    Ubuntu11.10中Eclipse安装PyDev插件方法 PyDev是Eclipse中用来开发python的一个插件,个人比较喜欢,下面介绍在Ubuntu下安装这个插件的方法.(在Windows下 ...

  9. Extjs-树 Ext.tree.TreePanel 动态加载数据

    先上效果图 1.说明Ext.tree.Panel 控件是树形控件,大家知道树形结构在软件开发过程中的应用是很广泛的,树形控件的数据有本地数据.服务器端返回的数据两种.对于本地数据的加载,在extjs的 ...

  10. linux杂谈(十三):代理server

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/linux_player_c/article/details/24707457 1.代理server的 ...