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 ...
随机推荐
- 10张图带你深入理解Docker容器和镜像-转
转载:http://dockone.io/article/783 这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(container)和镜像(image)之间的区别,并深入探讨容器和运行 ...
- linux 显示ip地址小工具-nali
1.下载软件包 wget http://qqwry.googlecode.com/files/nali-0.1.tar.gz 2.安装 tar -zxvf nali-0.2.tar.gz cd nal ...
- 用JSON将一个字典写入到文件,通过loads()将JSON字符串在转换为本来的类型
通过dumps将字典转换为JSON的字符串,存到磁盘里面
- Office2010 破解(Microsoft Toolkit 2.4.3.exe)
这两天破解刚安装好的office2010,总是报错 刚才重新下载了Microsoft Toolkit 2.4.3.exe工具后,破解成功,操作如下: 选择office按钮后,如下操作,
- 44个 Javascript 变态题解析 (上)
原题来自: javascript-puzzlers(http://javascript-puzzlers.herokuapp.com/) 读者可以先去做一下感受感受. 当初笔者的成绩是 21/44… ...
- springcloud 与分布式系统(转载)
原地址:http://blog.csdn.net/neosmith/article/details/51919038 本文不是讲解如何使用spring Cloud的教程,而是探讨Spring Clou ...
- TCP/TP:DNS区域(Zone)
之前阅读资料不是特别明白,看到一个博主的解释,豁然开朗,特此记录. https://blog.csdn.net/huangzx3/article/details/79347556 DNS区域(ZONE ...
- [Array]414. Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- [Array]217.Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 【html、CSS、javascript-2】CSS基础
CSS CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一 css的四种引入方式 1.行内式 ...