Feel Good
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 18449   Accepted: 5125
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

Source


Solution

题意:找一个区间,使这个区间最小值乘上这个区间的和最大。

用单调栈维护递增,找出以每个$i$为最小值的最远的左右端点即可。

每次弹栈就更新被弹元素的右端点,入栈时更新入栈元素的左端点即可。

(为什么不把题说清楚有多组数据还有spj太垃圾了吧必须要区间最小!!!!)

(真的受不了了为什么poj那么卡aaaaa!!!浪费了我好多好多时间!!!!!!!!【粉转黑!!)

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std; LL a[], pre[];
int stk[], R[], L[], n;
int main() {
while(~scanf("%d", &n)) {
int top = ;
memset(pre, , sizeof(pre));
memset(L, , sizeof(L));
memset(R, , sizeof(R));
for(int i = ; i <= n; i ++) {
scanf("%I64d", &a[i]);
pre[i] = pre[i - ] + a[i];
while(a[i] < a[stk[top]] && top) {
R[stk[top --]] = i - ;
}
L[i] = stk[top] + ;
stk[++ top] = i;
}
while(top) {
R[stk[top --]] = n;
}
LL ans = ; int l, r;
for(int i = ; i <= n; i ++) {
LL tmp = (pre[R[i]] - pre[L[i] - ]) * a[i];
if(tmp > ans) {
ans = tmp;
l = L[i], r = R[i];
}
if(tmp == ans) {
if(R[i] - L[i] + < r - l + ) {
l = L[i], r = R[i];
}
}
}
printf("%I64d\n%d %d\n", ans, l, r);
} 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. POJ 3658 Artificial Lake (单调栈)

    题意: 析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度. 代码如下: #pragma comment(linker, "/S ...

  4. poj 2559 Largest Rectangle(单调栈)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26549 ...

  5. POJ - 2796 Feel Good 单调递增栈+前缀和

    Feel Good Bill is developing a new mathematical theory for human emotions. His recent investigations ...

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

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

  7. poj 2796 Feel Good 单调队列

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

  8. [poj 2796]单调栈

    题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...

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

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

  10. POJ 2796 Feel Good 【单调栈】

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

随机推荐

  1. Indepence Mode 备份 weblogic

    一般不在administation server 停止这个模式 管理服务器挂了,不会影响其他服务器的运行 备份一个domain copy  一个  /bin  把启动的脚本做一个修改  里面的doma ...

  2. Windows执行命令与下载文件总结

    1.前言 在渗透或是病毒分析总是会遇到很多千奇百怪的下载文件和执行命令的方法. 2.实现方式 2.1.Powershell win2003.winXP不支持 $client = new-object ...

  3. 80.YCrCb - YUV - RGB之间的介绍

    一,引言 YUV(亦称YCrCb)是被欧洲电视系统所采用的一种颜色编码方法(属于PAL).YUV主要用于优化彩色视频信号的传输,使其向后兼容老式黑白电视.与RGB视频信号传输相比,它最大的优点在于只需 ...

  4. 音频自动增益 与 静音检测 算法 附完整C代码【转】

    转自:https://www.cnblogs.com/cpuimage/p/8908551.html 前面分享过一个算法<音频增益响度分析 ReplayGain 附完整C代码示例> 主要用 ...

  5. REX系统了解1

    REX是高通开发出来的一个操作系统,起初它是为了在Inter 80186处理器上应用而开发的,到后来才转变成应用在ARM这种微处理器上.他历经了很多版本,代码也越来越多,功能也越来越完善.REX只用不 ...

  6. Codeforces Round #505

    Codeforces Round #505 A. Doggo Recoloring 题目描述:给定一个字符串,每次选择一个在字符串里面出现至少两次的字符,然后将这种字符变成那一种指定的字符,问最终这个 ...

  7. 说一下怎么搭建外网来访问SVN服务器

    一.搭建SVN服务器 1.所需软件 TortoiseSVN,下载地址http://tortoisesvn.net/downloads.html TortoiseSVN中文语言包,下载地址http:// ...

  8. php中的单引号与双引号详解

    一.引号定义字符串 在Php中,通常一个字符串被定义在一对引号中,如: 'I am a string in single quotes'"I am a string in double qu ...

  9. KnockoutJs学习笔记(四)

    由于Writable computed observables和How dependency tracking works的要求相对较高,我先跳过这两篇,学习Pure computed observa ...

  10. 《Android源码设计模式》----面向对象六大原则

    1.单一职责原则 Single Respoonsibility Principle(SRP) --封装 2.开闭原则 Open Close Principle(OCP)--对扩展开放,对修改封闭 3. ...