poj 2059 单调栈
- 题意:求柱状图中最大矩形面积。
- 单调栈:顾名思义就是栈内元素单调递增的栈。
每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素。直到满足当前须要插入的元素大于栈顶元素为止。能够easy求出某个数左边或右边,第一个大于或小于它的数,且复杂度是O(n)。
- 思路:easy先想到一个好的枚举方式:以当前柱状为扩展点,往左边和右边扩展。当遇到一个比当前柱状小的柱状时停止扩展。以当前柱状的高度为矩形的高。向左右扩展的距离之差为矩形的长度,这样对n个柱状进行扫描之后可得最大矩形,复杂度是O(n2),显然超时。
本题用单调栈来做,维护一个条形图高度的单调栈。
- 心得:一定要想好了思路,并用伪代码在草稿纸上写一遍再动手写,这道题单调栈实现部分花了大量时间debug,源于思路不清晰。
简洁的实现:
1.读入当前柱状的高度h。若h大于栈顶元素,则向栈中push(h, i) (i为当前柱状的编号)。若h小于栈顶元素,则弹出栈顶元素,并得到(i−topi)∗toph为栈顶柱状所能扩展得到的最大矩形面积,以此面积来更新全局的最大面积。
反复上操作,指导栈顶元素小于当前元素。
2.最后得到一个单调递增的栈,再来用(n−topi)∗toph来表示栈顶元素所能组成的最大面积。处理并更新完整个栈就可以。
注:我的代码实现,没实用事实上点做标记,而是一直条形图合并的方法,代码实现复杂了一些
我的代码:
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long int LL;
const int M = 100009,INF = 0x3fffffff;
int main(void) {
//problem: poj2559, address:http://poj.org/problem?
id=2559
LL n, ans = -1, temp;
while (scanf("%lld",&n), n) {
ans = -1;
pair<LL, LL> key(-99999999 , 1);
stack<pair<LL, LL> > s;
s.push(key);
for (int i = 1; i <= n; i++) {
key.second = 1;
scanf("%lld", &key.first);
int k = 0 ;
while (s.top().first >= key.first) {
temp = LL(s.top().second + k)*LL( s.top().first);
if (temp > ans) ans = temp;
key.second += s.top().second;
k += s.top().second;
s.pop();
}
s.push(key);
}
for (int i = 0; !s.empty();) {
temp = LL(s.top().first) * LL(i + s.top().second);
if(temp > ans) ans = temp;
i += s.top().second;
s.pop();
}
printf("%lld\n", ans);
}
return 0;
}
poj 2059 单调栈的更多相关文章
- Poj 3250 单调栈
1.Poj 3250 Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...
- [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 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 3415 Common Substrings(后缀数组+单调栈)
http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Sub ...
- poj 3250 Bad Hair Day (单调栈)
http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
随机推荐
- go语言之进阶篇主协程先退出导致子协程没来得及调用
1.主协程先退出导致子协程没来得及调用 示例: package main import ( "fmt" "time" ) //主协程退出了,其它子协程也要跟着退 ...
- Create an Android library
官方文档 创建 Android 库 [Create an Android library] Android 库在结构上与 Android app module 相同.它可以提供构建应用所需的一切内容, ...
- CSS的50个代码片段
1.css全局 html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a ...
- JQuery-Dialog(弹出窗口,遮蔽窗口)
在Ajax中经常用到的弹出窗口和遮蔽窗口.自己写肯定是一个最佳方案,但时间和成本上,还是决定了寻找现成的吧.大概罗列一下.需要我满足我几个条件 一定要简洁方便 拥有遮蔽功能,Model Dialog ...
- BuildTask & BuildType
Build Tasks 在build文件中使用了Android或者Java插件之后就会自动创建一系列可以运行的任务. Gradle中有如下一下默认约定的任务: assemble 该任务包含了项目中的所 ...
- Linq-Contains查询
customers.Where(c => c.Name.Contains("john"));
- 'Lock wait timeout exceeded; try restarting transaction'问题
OperationalError: (1205, 'Lock wait timeout exceeded; try restarting transaction') 原因很简单,太多错误,意外处理没有 ...
- Log4j 2.0在开发中的高级使用具体解释—介绍篇(一)
Log4j最终迎来了首个apache版本号.Log4j 2 是 Log4j 的升级版本号,该版本号比起其前任来说有着显著的改进,包括非常多在 Logback 中的改进以及Logback 架构中存在的问 ...
- VS2017安装或卸载错误1303
为添加一个Python开发环境,运行Visual Studio Installer安装报错,下面是日志: 问题日志 安装出现问题. 可通过以下方式排查包故障问题: 1. 使用以下搜索 URL 来搜索针 ...
- iOS编程(双语版)-视图-Frame/Bounds/Center
1. Frame 每个视图都有一个frame属性,它是CGRect结构,它描述了视图所在的矩形在其父视图中的位置. (屏幕坐标系默认的原点在左上角,x轴向右伸展,y轴向下伸展) 设置frame通常通过 ...