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 ...
随机推荐
- js原生捕鱼达人(二)
昨天写到构造炮弹,有点小bug不知道大家发现没有,今天继续昨天的步骤 7>构造炮弹 思路和前面都是一样一样的 注意构造函数中需要考虑的属性 和 构造函数的原型上面的方法 <sc ...
- 常用excel技巧
1.excel 设置行列分色显示 =MOD(ROW(),2)=0 2.多表匹配数据 通过身份证在另外一个表查找这个人的基本信息 第一张表 第二张表: =VLOOKUP(F12,'2014总表'!D: ...
- SQL语句统计每天、每月、每年的数据
1.每年select year(ordertime) 年,sum(Total) 销售合计from 订单表group by year(ordertime) 2.每月select year(orderti ...
- 获取元素在浏览器中的绝对位置(从jquery1.8中抠出来)
<style> html,body{margin:0;padding:0;} .d1{margin-left:40px;background:red;width:2000px;height ...
- Gerrit日常操作命令收集
Gerrit代码审核工具是个好东西,尤其是在和Gitlab和Jenkins对接后,在代码控制方面有着无与伦比的优势. 在公司线上部署了一套Gerrit系统,在日常运维中,使用了很多gerrit命令,在 ...
- swift 判断输入的字符串是否为数字
// 判断输入的字符串是否为数字,不含其它字符 func isPurnInt(string: String) -> Bool { let scan: Scanner = Scanner(stri ...
- Linux 信号概念
程序在执行的时候,几乎任何时刻都会反生事件. 信号通常用来向一个进程通知事件. 信号是不可提前预知的,所以信号是异步的. 信号随时都可能发生,接收信号的进程也可以没有控制权. 每个信号名都以SIG开头 ...
- 每日一SQL-善用DATEADD和DATEDIFF
转自:http://www.dotblogs.com.tw/lastsecret/archive/2010/10/04/18097.aspx 上個星期去Tech-Day聽了幾場有趣的課,其中一堂是楊志 ...
- 实践:VIM深入研究(20135301 && 20135337)
目录 一.基本知识 1.vim模式介绍 2.三种常用模式的切换 二.Vim文档编辑 1.vim重复命令 2.游标的快速跳转 3.复制粘贴和剪切 4.删除文本 5.字符的替换及撤销(Undo操作) 6. ...
- ModernUI教程:第一个ModernUI应用(采用项目模板)
在我们开始之前,请确保你已经为你的Visual2012或者2013安装了ModernUI for WPF的模板扩展: >>从Visual Studio 库 下载并安装VSIX扩展 > ...