POJ2796(单调栈)
Feel Good
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 12987 | Accepted: 3639 | |
Case Time Limit: 1000MS | Special Judge |
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
Output
Sample Input
6
3 1 6 4 5 2
Sample Output
60
3 5 单调栈的精髓还得好好领会。
//2016.8.23
#include<iostream>
#include<cstdio>
#define ll __int64 using namespace std; const int N = ;
ll sum[N], ans, tmp;
int Stack[N], a[N], l[N], L, R; int main()
{
int n, top;
while(scanf("%d", &n)!=EOF)
{
sum[] = ans = top = ;
for(int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
sum[i] = sum[i-]+a[i];
}
a[++n] = -;
ans = -;
for(int i = ; i <= n; i++)
{
if(top == || a[i]>a[Stack[top-]])
{
Stack[top++] = i;
l[i] = i;
continue;
}
if(a[i] == a[Stack[top-]])continue;
while(top> && a[i]<a[Stack[top-]])
{
top--;
tmp = 1ll*a[Stack[top]]*(sum[i-]-sum[l[Stack[top]]-]);
if(tmp > ans)
{
ans = tmp;
L = l[Stack[top]];
R = i-;
}
} l[i] = l[Stack[top]];
Stack[top++] = i;
} printf("%I64d\n%d %d\n", ans, L, R);
} return ;
}
POJ2796(单调栈)的更多相关文章
- The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈
题面 题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值 题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok 我们 ...
- POJ2796 Feel Good 单调栈
题意:给定一个序列,需要找出某个子序列S使得Min(a[i])*Σa[i] (i属于S序列)最大 正解:单调栈 这题的暴力还是很好想的,只需3分钟的事就可以码完,以每个点拓展即可,但这样的复杂度是O( ...
- upc组队赛1 小C的数学问题【单调栈】(POJ2796)
小C的数学问题 题目描述 小C是个云南中医学院的大一新生,在某个星期二,他的高数老师扔给了他一个问题. 让他在1天的时间内给出答案. 但是小C不会这问题,现在他来请教你. 请你帮他解决这个问题. 有n ...
- poj2796 维护区间栈//单调栈
http://poj.org/problem?id=2796 题意:给你一段区间,需要你求出(在这段区间之类的最小值*这段区间所有元素之和)的最大值...... 例如: 6 3 1 6 4 5 2 以 ...
- 【POJ2796】Feel Good 单调栈
题目大意:给定一个长度为 N 的序列,求任意区间 [ l , r ] 中最小的\(min\{v[i],i\in[l,r] \}*\Sigma_{i=l}^rv[i]\). 题解:这是一道具有标准单调栈 ...
- 单调栈poj2796
题意:给你一段区间,需要你求出(在这段区间之类的最小值*这段区间所有元素之和)的最大值...... 例如: 6 3 1 6 4 5 2 以4为最小值,向左右延伸,6 4 5 值为60....... ...
- POJ2796 Feel Good(单调栈)
题意:给一个非负整数序列,求哪一段区间的权值最大,区间的权值=区间所有数的和×区间最小的数. 用单调非递减栈在O(n)计算出序列每个数作为最小值能向左和向右延伸到的位置,然后O(n)枚举每个数利用前缀 ...
- UVA 1619/POJ2796 滑窗算法/维护一个单调栈
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 12409 Accepted: 3484 Case T ...
- POJ2796【单调栈】
题意: 题意:n个数,求某段区间的最小值*该段区间所有元素之和的最大值 思路: 主要参考:http://www.cnblogs.com/ziyi–caolu/archive/2013/06/23/31 ...
随机推荐
- 64位系统访问注册表SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
public int ChecNonkWoW64() { try { ; string subKey = @"SOFTWARE\Microsoft\Windows\CurrentVersio ...
- jQuery中的attr()和prop()使用
总结:除了checked.seleted这样的属性推荐用prop()外,其他的随意都可以. 原因如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML ...
- log4cxx入门第一篇--一个小例子
先看官网:http://logging.apache.org/log4cxx/index.html 转载自:http://wenku.baidu.com/view/d88ab5a9d1f34693da ...
- tcpdump的表达元
(nt: True 在以下的描述中含义为: 相应条件表达式中只含有以下所列的一个特定表达元, 此时表达式为真, 即条件得到满足) dst host host如果IPv4/v6 数据包的目的域是host ...
- jvm 配置,看看
http://my.oschina.net/qiangzigege/blog/661757
- (简单) UVA 11624 Fire! ,BFS。
Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...
- OPENCV图像变换-2
一.经典霍夫变换 霍夫变换是图像处理中的一种特征提取技术,该方法通过在一个参数空间中通过计算累计结果的局部最大值来得到一个符合该特定形状的集合,作为结果. 运用两个坐标空间之间的变换,将一个空间中具有 ...
- nginx实战
原文:http://www.cnblogs.com/yucongblog/p/6289628.html nginx实战 (一) nginx环境的搭建安装流程: 1 通过ftp将nginx-1.11 ...
- Codeforces#360Div2
A题 题意:给定d个操作,每个操作当中只包含1和0,若存在0,则表示操作者获胜,求最大的连续获胜个数 分析:直接统计之后用一个数组纪录下来即可 #include <iostream> #i ...
- (中等) POJ 1436 Horizontally Visible Segments , 线段树+区间更新。
Description There is a number of disjoint vertical line segments in the plane. We say that two segme ...