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. C#基础系列:反射笔记

    前言:使用反射也有几年了,但是一直觉得,反这个概念很抽象,今天有时间就来总结下这个知识点. 1.为什么需要反射: 最初使用反射的时候,作为小菜总是不理解,既然可以通过new 一个对象的方式得到对象,然 ...

  2. Topcoder SRM 638 DIV 2 (大力出奇迹)

    水题,就是一个暴力.大力出奇迹. Problem Statement   There is a narrow passage. Inside the passage there are some wo ...

  3. 2018.11.22-day24 面向对象-继承

    1.归一化设计 2.抽象类 3.钻石继承 4.C3算法 5.新式类中的super

  4. gdb coredump的使用

    1 出现core dump时最好的办法是使用gdb查看coredump文件 2 使用的条件 出现问题的代码,系统,所有涉及的代码都应该一起编译,然后得到符号表,这样加载符号表,使用coredump文件 ...

  5. select version();desc mysql.user;

    D:\wamp64\wamp\bin\mysql\mysql5.6.17\bin>mysql -hgoDev -uroot -ppasswordWarning: Using a password ...

  6. Maximum likelihood from incomplete data via the EM algorithm (1977)

    Maximum likelihood from incomplete data via the EM algorithm (1977)  

  7. 那些让你代码思维和能力有较大的提升Java源码(转)

    对于学习J2EE的框架有很大的帮助,代码里使用了各种设计模式.事件机制.Java8语法.代码量也很小,web服务使用Netty作为支持,对HTTP/网络想研究的一定是你的必读品.目前在写 Blade- ...

  8. UniGui的信息弹出框MessageDlg自定义标题的方法(使用JS动态本地化文本)

    UniGui的信息弹出框MessageDlg的原型定义如下: procedure MessageDlg(const Msg: string; DlgType: TMsgDlgType; Buttons ...

  9. python自动化运维八:Ansible

    Ansible是新出现的自动化运维工具,基于Python研发.糅合了众多老牌运维工具的优点实现了批量操作系统配置.批量程序的部署.批量运行命令等功能.仅需在管理工作站上安装ansible程序配置被管控 ...

  10. 【C++基础学习】类型声明

    1.初始化 在C++中,初始化与赋值操作是完全不同的两个操作.初始化不是赋值,初始化的含义是创建变量时赋予其一个初始值,而赋值的含义是把对象的当前值擦除,而以一个新值来代替. 初始化的方式有: 1 i ...