Poj 2796 单调栈
关于单调栈的性质,和单调队列基本相同,只不过单调栈只使用数组的尾部, 类似于栈。
Accepted Code:
/*************************************************************************
> File Name: 2796.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年07月21日 星期一 22时45分56秒
> Propose:
************************************************************************/
#include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
const int maxn = ;
int n;
// b[i] holds the smallest index where a[b[i]]...a[i] are not larger than a[i], and e[i] is similar, just extends to the right of a[i]
int a[maxn], b[maxn], e[maxn], st[maxn];
LL sum[maxn]; //prefix sum int
main(void) {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
while (~scanf("%d", &n)) {
for (int i = ; i <= n; i++) scanf("%d", a+i);
for (int i = ; i <= n; i++) b[i] = e[i] = i;
sum[] = ;
for (int i = ; i <= n; i++) {
sum[i] = sum[i-] + a[i];
}
// the top of no decreasing stack
int top = ;
for (int i = ; i <= n; i++) {
if (!top || a[i] > a[st[top-]]) {
// push an element to stack
st[top++] = i;
} else {
while (top > && a[st[top-]] > a[i]) {
// update e[st[top-1]]
e[st[top-]] = i - ;
// update b[i]
b[i] = b[st[top-]];
// pop the toppest element of stack
top--;
}
if (!top || a[st[top-]] != a[i]) {
// push
st[top++] = i;
} else if(a[st[top-] == a[i]]) {
// update b[i]
b[i] = b[st[top-]];
}
}
}
// update e[i] of those elements that still in the stack
for (int i = ; i < top; i++) e[st[i]] = n;
// find the answer
LL ans = -, bb, ee;
for (int i = ; i <= n; i++) {
if (ans < a[i] * (sum[e[i]]-sum[b[i]-])) {
ans = a[i] * (sum[e[i]]-sum[b[i]-]);
bb = b[i];
ee = e[i];
}
}
printf("%lld\n%lld %lld\n", ans, bb, ee);
} return ;
}
Poj 2796 单调栈的更多相关文章
- [poj 2796]单调栈
题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...
- uva 1619 - Feel Good || poj 2796 单调栈
1619 - Feel Good Time limit: 3.000 seconds Bill is developing a new mathematical theory for human ...
- Poj 3250 单调栈
1.Poj 3250 Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...
- poj 2059 单调栈
题意:求柱状图中最大矩形面积. 单调栈:顾名思义就是栈内元素单调递增的栈. 每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素.直到满足当前须要插入的元素大于栈顶元 ...
- POJ 3044单调栈
题意: 思路: 单调栈 // by SiriusRen #include <stack> #include <cstdio> using namespace std; stac ...
- poj 2082 单调栈 ***
和poj2082差不多,加了一个宽度的条件 #include<iostream> #include<string> #include<cmath> #include ...
- poj 2559 单调栈 ***
给出一系列的1*h的矩形,求矩形的最大面积. 如图: 题解链接:点我 #include <iostream> #include <cstdio> using namespace ...
- poj 2599 单调栈 ***
和poj2082差不多,加了一个宽度的条件 #include<cstdio> #include<cmath> #include<algorithm> #includ ...
- 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 ...
随机推荐
- linux最基础最常用的命令快速手记 — 让手指跟上思考的速度(三)
这一篇作为姐妹篇的第三篇,废话不多说,我觉得这个比mysql的还要重要,为什么,一旦你摊上linux 敲键盘输入命令简直是要飞的速度,不断的卡壳查命令,效率太低了,而且非常严重的影响思绪,思绪! 某些 ...
- Jqgrid 序号列宽度调整
// 遍历jqgrid 使其序号列宽度为45 function setwidth() { $("table[role='grid']").each(function () {//j ...
- python configparser模块详解
此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...
- 快速I/O 51node 1406
#include <bits/stdc++.h> using namespace std; #define LL long long typedef pair<int,int> ...
- [JLOI2015]战争调度【暴力+树形Dp】
Online Judge:Bzoj4007,Luogu P3262 Label:暴力,树形Dp 题解 参考了这篇blog https://www.cnblogs.com/GXZlegend/p/830 ...
- JZOJ5857 【NOIP提高组模拟A组2018.9.8】没有上司的舞会
题目 Description "那么真的有果尔德施坦因这样一个人?"他问道. "是啊,有这样一个人,他还活着.至于在哪里,我就不知道了." "那么那个 ...
- Lucene 评分机制一
1. 评分公式 1.1 公式介绍 这个公式是Lucene实际计算时使用的公式,是由原型公式推导而来 tf(t in d) 表示某个term的出现频率,定义了term t出现在当前document d的 ...
- 深入浅出 Java Concurrency (23): 并发容器 part 8 可阻塞的BlockingQueue (3)[转]
在Set中有一个排序的集合SortedSet,用来保存按照自然顺序排列的对象.Queue中同样引入了一个支持排序的FIFO模型. 并发队列与Queue简介 中介绍了,PriorityQueue和Pri ...
- etcd 研究研究
先记录参考信息:etcd 场景https://blog.csdn.net/bbwangj/article/details/82584988 etcd 集群部署https://www.jianshu.c ...
- xshell 连接 kali
1 修改配置文件 vi /etc/ssh/sshd_config #PasswordAuthentication no 去掉#,并且将no修改为YES //kali中默认是yes PermitRo ...