Feel Good

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 12987   Accepted: 3639
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 单调栈的精髓还得好好领会。
 //2016.8.23
#include<iostream>
#include<cstdio>
#define ll __int64 using namespace std; const int N = ;
ll sum[N], ans, tmp;
int Stack[N], a[N], l[N], L, R; int main()
{
int n, top;
while(scanf("%d", &n)!=EOF)
{
sum[] = ans = top = ;
for(int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
sum[i] = sum[i-]+a[i];
}
a[++n] = -;
ans = -;
for(int i = ; i <= n; i++)
{
if(top == || a[i]>a[Stack[top-]])
{
Stack[top++] = i;
l[i] = i;
continue;
}
if(a[i] == a[Stack[top-]])continue;
while(top> && a[i]<a[Stack[top-]])
{
top--;
tmp = 1ll*a[Stack[top]]*(sum[i-]-sum[l[Stack[top]]-]);
if(tmp > ans)
{
ans = tmp;
L = l[Stack[top]];
R = i-;
}
} l[i] = l[Stack[top]];
Stack[top++] = i;
} printf("%I64d\n%d %d\n", ans, L, R);
} return ;
}

POJ2796(单调栈)的更多相关文章

  1. The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈

    题面 题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值 题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok 我们 ...

  2. POJ2796 Feel Good 单调栈

    题意:给定一个序列,需要找出某个子序列S使得Min(a[i])*Σa[i] (i属于S序列)最大 正解:单调栈 这题的暴力还是很好想的,只需3分钟的事就可以码完,以每个点拓展即可,但这样的复杂度是O( ...

  3. upc组队赛1 小C的数学问题【单调栈】(POJ2796)

    小C的数学问题 题目描述 小C是个云南中医学院的大一新生,在某个星期二,他的高数老师扔给了他一个问题. 让他在1天的时间内给出答案. 但是小C不会这问题,现在他来请教你. 请你帮他解决这个问题. 有n ...

  4. poj2796 维护区间栈//单调栈

    http://poj.org/problem?id=2796 题意:给你一段区间,需要你求出(在这段区间之类的最小值*这段区间所有元素之和)的最大值...... 例如: 6 3 1 6 4 5 2 以 ...

  5. 【POJ2796】Feel Good 单调栈

    题目大意:给定一个长度为 N 的序列,求任意区间 [ l , r ] 中最小的\(min\{v[i],i\in[l,r] \}*\Sigma_{i=l}^rv[i]\). 题解:这是一道具有标准单调栈 ...

  6. 单调栈poj2796

    题意:给你一段区间,需要你求出(在这段区间之类的最小值*这段区间所有元素之和)的最大值...... 例如: 6 3 1 6 4 5 2 以4为最小值,向左右延伸,6 4 5  值为60....... ...

  7. POJ2796 Feel Good(单调栈)

    题意:给一个非负整数序列,求哪一段区间的权值最大,区间的权值=区间所有数的和×区间最小的数. 用单调非递减栈在O(n)计算出序列每个数作为最小值能向左和向右延伸到的位置,然后O(n)枚举每个数利用前缀 ...

  8. UVA 1619/POJ2796 滑窗算法/维护一个单调栈

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

  9. POJ2796【单调栈】

    题意: 题意:n个数,求某段区间的最小值*该段区间所有元素之和的最大值 思路: 主要参考:http://www.cnblogs.com/ziyi–caolu/archive/2013/06/23/31 ...

随机推荐

  1. 关于python 函数参数

    函数参数:必选参数.默认参数.可选参数.关键字参数 1.默认参数 默认参数可以简化函数的调用.设置默认参数时,有几点要注意: 一是必选参数在前,默认参数在后,否则Python的解释器会报错: 二是如何 ...

  2. iOS常用宏定义

    转发:https://www.douban.com/note/486674206/ #ifndef MacroDefinition_h#define MacroDefinition_h //----- ...

  3. (中等) HDU 5046 Airport ,DLX+可重复覆盖+二分。

    Description The country of jiuye composed by N cites. Each city can be viewed as a point in a two- d ...

  4. log4CXX第二篇---配置文件(properties文件)详解

    一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...

  5. easyui 异步json tree跨域访问问题解决

    最近在用easyui中的异步tree时发现了跨域访问问题,我们都知道jquery ajax提供get请求的跨域访问.所以解决easyui tree跨域访问的问题便是将数据通过jquery ajax将数 ...

  6. DialogFragment学习笔记

    创建DialogFragment 跟通常的创建Fragment差不多,XML,继承DialogFragment,复写onCreateView() public View onCreateView(La ...

  7. poj2528 Mayor's posters(线段树区间覆盖)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 50888   Accepted: 14737 ...

  8. cocoapod升级版本

    原文 http://blog.csdn.net/sing_sing/article/details/49762359 该方法好用 sudo gem install -n /usr/local/bin ...

  9. imagebutton、imageview的属性

    [转]http://blog.csdn.net/victoryckl/article/details/14162131 http://blog.sina.com.cn/s/blog_68b3fdc30 ...

  10. wildfly 如何设置外网访问

    wildfly的默认配置是不支持外网访问的, 要想实现外网访问需要修改standalone.xml配置文件. 配置文件所在路径:wildfly/standalone/configuration/sta ...