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. BNU OJ 51003 BQG's Confusing Sequence

    二进制++高精度取模 #include<cstdio> #include<cstring> #include<algorithm> using namespace ...

  2. 关于 CentOS 7 里面 普通用户 Ulimit max user processes 值的问题

    最近在对tomcat 的一个 项目进行 压测, 普通用户 启动 tomcat 的时候 压力上去以后就会报 java.lang.OutOfMemoryError 的错误, 这种错误 按道理来说都是 系统 ...

  3. w3c学习总结1

    1.根据 HTML5 规范,在没有其他合适标签更合适时,才应该把 <b> 标签作为最后的选项.HTML5 规范声明:应该使用 <h1> - <h6> 来表示标题,使 ...

  4. eclipse创建android项目,无法正常预览布局文件

    http://jingyan.baidu.com/article/d621e8da0e0e052865913fae.html

  5. (五)Jquery Mobile列表

    Jquery Mobile列表 一.JM列表 1.普通列表            效果:            带序号的列表 将ul换成ol      效果:       2.data-inset=& ...

  6. 检测网站挂马程序(Python)

    系统管理员通常从svn/git中检索代码,部署站点后通常首先会生成该站点所有文件的MD5值,如果上线后网站页面内容被篡改(如挂马)等,可以比对之前生成MD5值快速查找去那些文件被更改,为了使系统管理员 ...

  7. iOS开发——获取本设备IP

    不说废话,直接上代码. #import <ifaddrs.h> #import <arpa/inet.h> - (NSString *)getIPAddress { NSStr ...

  8. IOS_FMDB有关字典、数组存储及获取问题

    http://blog.csdn.net/betterbb/article/details/25984455 FMDB存储字典或数组时会变成字符串存入sqlite里,但如果不将其转换成json格式存储 ...

  9. iOS第三方常用类库

    1.AFNetworking AFNetworking 采用 NSURLConnection + NSOperation, 主要方便与服务端 API 进行数据交换, 操作简单, 功能强大, 现在许多人 ...

  10. angularjs ajax传参

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...