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 ...
随机推荐
- Android SQLite (二) 基本用法
在Android开发中SQLite起着很重要的作用,网上SQLite的教程有很多很多,不过那些教程大多数都讲得不是很全面.本人总结了一些SQLite的常用的方法,借着论坛的大赛,跟大家分享分享的. 一 ...
- css3爆炸效果更换图片轮播图
思路:给一个div设置一个背景图片1.jpg,然后在这个div上面用两个for循环动态的创建一个列数为C行数为R数量的span,并给这些span设置宽高.定位并设置背景图片0.jpg,然后设置每个sp ...
- 分享一个刷网页PV的python小脚本
下面分享一个小脚本,用来刷网页PV. [root@huanqiu ~]# cat www.py #!/usr/bin/python# coding: UTF-8import webbrowser as ...
- Html之初体验
概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言.相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器根据标记 ...
- onresize方法
resize()方法可以写在当前页面包含的所有js里
- Node.js开发环境搭建
1.安装express npm install express -g 2.express33.6以后把express-generator分离出来了,所以还需安装express-generator,否则 ...
- 怎么写针对IE9的CSS
(自己亲自试过有用)针对IE9的CSS只需在相应CSS代码加入只有IE9识别的 \9\0.具体代码如下: .div{ background-color:#0f0\9\0;/* ie9 */ } 其他浏 ...
- 突然想起android与mfc差异
两者都可以算作是客户端程序,都是做上位机用的.而且都是被动执行. 相同点: 1.MFC中,它是由 project的名字 里面的某个成员函数来初始化,窗体,以及窗体里面的变量. 后面都是监听消息循环.数 ...
- 高性能网站性能优化与系统架构(ZT)
转载请保留出处:俊麟 Michael’s blog (http://space.itpub.net/7311285/viewspace-97) 我在CERNET做过拨号接入平台的搭建,而后在Yahoo ...
- tkinter 改变按钮状态
import tkinter as tk def btn1_change_btn1(event): '''方式一:通过事件控制自己''' if event.widget['state'] == 'no ...