POJ 2796 Feel Good
传送门
Description
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
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
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的更多相关文章
- [poj 2796]单调栈
题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...
- POJ 2796:Feel Good(单调栈)
http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...
- POJ 2796 Feel Good 【单调栈】
传送门:http://poj.org/problem?id=2796 题意:给你一串数字,需要你求出(某个子区间乘以这段区间中的最小值)所得到的最大值 例子: 6 3 1 6 4 5 2 当L=3,R ...
- POJ 2796[UVA 1619] Feel Good
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16786 Accepted: 4627 Case T ...
- poj 2796 Feel Good单调栈
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20408 Accepted: 5632 Case T ...
- POJ 2796 / UVA 1619 Feel Good 扫描法
Feel Good Description Bill is developing a new mathematical theory for human emotions. His recent ...
- Poj 2796 单调栈
关于单调栈的性质,和单调队列基本相同,只不过单调栈只使用数组的尾部, 类似于栈. Accepted Code: /******************************************* ...
- POJ 2796 Feel Good(单调栈)
传送门 Description Bill is developing a new mathematical theory for human emotions. His recent investig ...
- poj 2796 Feel Good 单调栈区间问题
Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...
随机推荐
- iOS中NSLog的输出格式
%@ 对象%d %i 整数%u 无符整形%f 浮点/双字%x, %X 二进制整数%o 八进制整数%zu size_t%p 指针 ; NSLog(@"%p",&tem); & ...
- 【MFC】WM_GETMINMAXINFO 设置无边框窗口最大花不遮挡任务栏
LRESULT OnGetMinMaxInfo( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/ ) { ...
- [转]iptables详解
FROM : http://blog.chinaunix.net/uid-26495963-id-3279216.html 一:前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的 ...
- Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- JS 之性能优化(2)
继续上一篇的JS性能优化之后,下面接着讲关于前端性能优化的内容.如果有不对的地方欢迎纠正. 1.避免过多的重排与重绘操作. 尽量将DOM中的多个读操作放一起,中间不要插入写的操作,因为写操作会导致浏览 ...
- CSS 清除浮动的四种方法
在实际项目中,我们经常会用到float属性来对页面进行布局.当使用float时,意味着该元素已经脱离了文档流,相当于浮于文档之上,不占据空间.但是针对兄弟元素为文字内容时,会占据一定空间,从而产生文字 ...
- 职业卖家淘宝美工教程,掌握技能无师自通 学到的不只是PS(共81节)附素材【无水印版】
职业卖家淘宝美工教程,掌握技能无师自通 学到的不只是PS(共81节)附素材[无水印版]设计传说出品的专业课程是我们资深培训讲师精心录制的,只有视频教程和常用必备的插件,其他绝不掺和,如果你是职业卖家, ...
- caffe windows学习:第一个测试程序
caffe windows编译成功后,就可以开始进行测试了.如果还没有编译成功的,请参考:caffe windows 学习第一步:编译和安装(vs2012+win 64) 一般第一个测试都是建议对手写 ...
- Linux第二次学习笔记
#Linux第二次实验(第三周) 学习目标 熟悉Linux系统下的开发环境 熟悉vi的基本操作 熟悉gcc编译器的基本原理 熟练使用gcc编译器的常用选项 熟练使用gdb调试技术 熟悉makefile ...
- unity3d 赛车游戏——复位点检测
一直没有时间写博客 昨天我的CarWaypoints插件也告一段落了 今年没回家,过年就我一个人 挺无聊的,那就休息一天写几篇博客吧 我的代码可能很少,但是思路很重要 希望不懂的朋友别只copy代码 ...