POJ 2796 Feel Good(单调栈)
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
思路
定义一串数字,其子区间的值为子区间元素和乘以区间最小值,因此利用单调栈的思想,以每个当前值为区间最小值,向左向右拓展区间。如果当前元素大于栈顶元素,那么就不能往前拓展,
如果当前元素小于栈顶元素,这个时候就要把栈中的元素一个一个弹出来,直到当前元素大于栈顶元素,对于弹出来的元素,它扩展到当前元素就不能向后伸展下去了,因此对于弹出来的元素这个时候就可以计算左右端点形成区间和与最小值的乘积了,维护一个最大值即可。
#include<stdio.h>
#include<string.h>
typedef __int64 LL;
const int maxn = 1000005;
LL a[maxn],stack[maxn] = {0},left[maxn] = {0},sum[maxn] = {0};
int main()
{
LL N,L = 0,R = 0,res = -1,tmp = 0;
scanf("%I64d",&N);
for (int i = 1;i <= N;i++) scanf("%I64d",&a[i]),sum[i] = a[i] + sum[i-1];
a[++N] = -1; //确保栈中元素能被全部弹出
int top = 0;
for (int i = 1;i <= N;i++)
{
if (!top || a[i] > a[stack[top-1]])
{
stack[top++] = i;
left[i] = i;
continue;
}
if (a[i] == a[stack[top-1]]) continue;
while (top > 0 && a[i] < a[stack[top-1]])
{
top--;
tmp = a[stack[top]]*(sum[i-1] - sum[left[stack[top]] - 1]);
if (tmp > res) res = tmp,L = left[stack[top]],R = i-1;
}
tmp = stack[top];
stack[top++] = i;
left[i] = left[tmp];
}
printf("%I64d\n%I64d %I64d\n",res,L,R);
return 0;
}
POJ 2796 Feel Good(单调栈)的更多相关文章
- poj 2796 Feel Good单调栈
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20408 Accepted: 5632 Case T ...
- poj 2796 Feel Good 单调栈区间问题
Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...
- POJ 3658 Artificial Lake (单调栈)
题意: 析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度. 代码如下: #pragma comment(linker, "/S ...
- poj 2559 Largest Rectangle(单调栈)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26549 ...
- POJ - 2796 Feel Good 单调递增栈+前缀和
Feel Good Bill is developing a new mathematical theory for human emotions. His recent investigations ...
- POJ 3415 后缀数组+单调栈
题目大意: 给定A,B两种字符串,问他们当中的长度大于k的公共子串的个数有多少个 这道题目本身理解不难,将两个字符串合并后求出它的后缀数组 然后利用后缀数组求解答案 这里一开始看题解说要用栈的思想,觉 ...
- poj 2796 Feel Good 单调队列
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8753 Accepted: 2367 Case Ti ...
- [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 ...
随机推荐
- Oracle PL/SQL 入门
PL/SQL 全称:Procedure Language/SQL.产生背景自己去百度. 模板: Declare ---变量定义 num ; name ) := 'damon'; idesc cnt_i ...
- 使用pngquant来压缩png资源缩小apk
最近发现了一个叫做pngquant的工具,可以有效的压缩资源文件中的png文件,从而减小发布的apk的大小.我发现这个工具有两个特点: 1. 真无损,压缩后重新运行了我的app发现是没有任何区别的 2 ...
- 为什么带网格(mesh)的模型添加了刚体Rigidbody和MeshCollider,还是会从地板穿过去?
两个Gameobject 放置在空中, 一个是Cube,一个是茄子模型 Cube的Collider 是Box Collider , 茄汁的Collider 是mesh collider, 他们都添加了 ...
- 爬虫获取邮箱,存入数据库,发送邮件java Mail
在网页上获取邮箱: package com.my.test; import java.io.BufferedReader; import java.io.InputStreamReader; impo ...
- Linux终端更改提示符
打开~/.bashrc可以看到命令提示的内容为:\u@\h\w\$ \u表示用户名,\h表示主机名,\w表示当前目录,\$表示命令提示符(普通用户$,超级用户#) 这个命令提示符有点长,很碍事,\u@ ...
- MVC认知路【点点滴滴支离破碎】【二】----Razor服务器标记语言
Razor 代码块包含在 @{....}中 内嵌表达式(变量和函数)已 @ 开头 代码语句用分号结束 变量使用 var 关键字声明 字符创用引号括起来 C#代码区分大小写 C#文件的扩展是 .csht ...
- android wifi热点 socket通信
1.首先建立wifi热点服务器 wifi客户端连接 2.开启一个子线程循环监听某个端口,进行数据流输入输出 /* 服务器 接收数据 */ class Receiver extends Thread ...
- python 批量更改文件名
工作中遇到一种情况,就是市场部那边经常发过来一些apk的包 但是要求更改名字,文件太多了,没办法,只有想办法了,还好命名都是有规则的 比如说 YZLoan-gdtyyb-V2.23.apk------ ...
- [转]响应式WEB设计学习(3)—如何改善移动设备网页的性能
原文地址:http://www.jb51.net/web/70362.html 前言 移动设备由于受到带宽.处理器运算速度的限制,因而对网页的性能有更高的要求.究竟是网页中的何种元素拉低了网页在移动设 ...
- 让nodeJS支持ES6的词法----babel的安装和使用
要使用Babel, 我们需要nodeJS的环境和npm, 主要安装了nodeJS, npm就默认安装了 , 现在安装nodeJS很简单了, 直接下载安装就好了: 安装es-checker 在使用Bab ...