传送门

Time Limit: 3000MS  Memory Limit: 65536K
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
单调栈

Implementation
#include <cstdio>
#include <stack>
#include <iostream>
using namespace std;
typedef long long LL; const int N(1e5+); int h[N], L[N], R[N], st[N];
LL s[N]; int main(){
int n;
scanf("%d", &n);
for(int i=; i<=n; i++) scanf("%d", h+i);
for(int i=; i<=n; i++) s[i]=s[i-]+h[i];
// (L[i], R[i]]
int t=-; //top of stack
for(int i=; i<=n; i++){
for(; ~t && h[st[t]]>=h[i]; t--);
L[i]=~t?st[t]:; //error-prone
st[++t]=i;
}
t=-;
for(int i=n; i; i--){
for(; ~t && h[st[t]]>=h[i]; t--);
R[i]=~t?st[t]-:n;
st[++t]=i;
}
LL res=-; //error-prone
int l, r;
for(int i=; i<=n; i++){
LL t=h[i]*(s[R[i]]-s[L[i]]);
if(t>res){
res=t;
l=L[i]+, r=R[i];
}
}
printf("%lld\n%d %d\n", res, l, r);
return ;
}

代码里标error-prone的地方都是坑,请特别注意。

尤其 res应初始化成-1,因为"A new idea Bill has recently developed assigns a non-negative integer value to each day of human life."

POJ 2796 Feel Good的更多相关文章

  1. [poj 2796]单调栈

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

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

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

  3. POJ 2796 Feel Good 【单调栈】

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

  4. POJ 2796[UVA 1619] Feel Good

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

  5. poj 2796 Feel Good单调栈

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

  6. POJ 2796 / UVA 1619 Feel Good 扫描法

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

  7. Poj 2796 单调栈

    关于单调栈的性质,和单调队列基本相同,只不过单调栈只使用数组的尾部, 类似于栈. Accepted Code: /******************************************* ...

  8. POJ 2796 Feel Good(单调栈)

    传送门 Description Bill is developing a new mathematical theory for human emotions. His recent investig ...

  9. poj 2796 Feel Good 单调栈区间问题

    Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...

随机推荐

  1. Python 栅栏凯撒

    def fence_Crypto(msg,priority="row"): ''' usage: fence_Crypto(msg[, priority])->msg to ...

  2. 使用EditText+ListView并结合TextWatcher实现输入关键字筛选数据

    想必大家应该遇到过这样的情况,当点击Spinner控件后弹出的列表内容超多,一个一个滑动着去寻找所要的项很麻烦,尤其是当自己知道想要选择的内容,这时候如果我们只需要输入某些关键字,就可以讲上百条数据筛 ...

  3. Android service ( 二) 远程服务

    通常每个应用程序都在它自己的进程内运行,但有时需要在进程间传递对象,你可以通过应用程序UI的方式写个运行在一个不同的进程中的service.在android平台中,一个进程通常不能访问其他进程中的内存 ...

  4. C# 使用正则表达式去掉字符串中的数字,或者去掉字符串中的非数字

            /// 去掉字符串中的数字           public static string RemoveNumber(string key)           {            ...

  5. 基于 URL 的权限控制

    先不用框架,自己实现一下 数据库 /* SQLyog v10.2 MySQL - 5.1.72-community : Database - shiro *********************** ...

  6. ffmpeg常用基本命令(转)

    [FFmpeg]FFmpeg常用基本命令 1.分离视频音频流 ffmpeg -i input_file -vcodec copy -an output_file_video //分离视频流 ffmpe ...

  7. [iOS翻译]《iOS7 by Tutorials》系列:在Xcode 5里使用单元测试(上)

    简介: 单元测试是软件开发的一个重要方面.毕竟,单元测试可以帮你找到bug和崩溃原因,而程序崩溃是Apple在审查时拒绝app上架的首要原因. 单元测试不是万能的,但Apple把它作为开发工具包的一部 ...

  8. 用python简单处理图片(3):添加水印

    python版本:3.4 Pillow版本:3.0 一.添加文字水印 from PIL import Image, ImageDraw,ImageFont im = Image.open(" ...

  9. 交流异步电机的Modelica模型

    Modelica标准库里的异步电机模型过于复杂,为了便于学习,我用最基本的异步电机方程写了一个Modelica模型,公式参照陈伯时的<电力拖动自动控制系统--运动控制系统>第3版的190页 ...

  10. 编写高质量代码--改善python程序的建议(八)

    原文发表在我的博客主页,转载请注明出处! 建议四十一:一般情况下使用ElementTree解析XML python中解析XML文件最广为人知的两个模块是xml.dom.minidom和xml.sax, ...