• 题意:求柱状图中最大矩形面积。

  • 单调栈:顾名思义就是栈内元素单调递增的栈。

    每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素。直到满足当前须要插入的元素大于栈顶元素为止。能够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 单调栈的更多相关文章

  1. Poj 3250 单调栈

    1.Poj 3250  Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...

  2. [poj 2796]单调栈

    题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...

  3. uva 1619 - Feel Good || poj 2796 单调栈

    1619 - Feel Good Time limit: 3.000 seconds   Bill is developing a new mathematical theory for human ...

  4. POJ 3044单调栈

    题意: 思路: 单调栈 // by SiriusRen #include <stack> #include <cstdio> using namespace std; stac ...

  5. poj 2082 单调栈 ***

    和poj2082差不多,加了一个宽度的条件 #include<iostream> #include<string> #include<cmath> #include ...

  6. poj 2559 单调栈 ***

    给出一系列的1*h的矩形,求矩形的最大面积. 如图: 题解链接:点我 #include <iostream> #include <cstdio> using namespace ...

  7. poj 2599 单调栈 ***

    和poj2082差不多,加了一个宽度的条件 #include<cstdio> #include<cmath> #include<algorithm> #includ ...

  8. poj 3415 Common Substrings(后缀数组+单调栈)

    http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Sub ...

  9. poj 3250 Bad Hair Day (单调栈)

    http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

随机推荐

  1. [2014亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number

    1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 这个题目也是当时笔试第一次见到,花了一个小时才做出了.慢慢总结还是挺简单的. 2.分析 分析如下: (1)一位数N(9 ...

  2. SharePoint 2013 基于表单 Membership 的身份验证

    其实关于SharePoint 2013 表单身份验证网上已经有很多了,比如SharePoint 2013自定义Providers在基于表单的身份验证(Forms-Based-Authenticatio ...

  3. Git-忽略规则.gitignore生效

    Git中如果忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法,如下这个文件每一行保存了一个匹配的规则例如,忽略单个文件或者整个目录的文件: *.css ...

  4. 领扣-754 到达终点数字 Reach a Number MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. 【Javascript设计模式1】-单例模式

    <parctical common lisp>的作者曾说,如果你需要一种模式,那一定是哪里出了问题.他所说的问题是指因为语言的天生缺陷,不得不去寻求和总结一种通用的解决方案. 不管是弱类型 ...

  6. js获取当前域名、Url、相对路径和参数

    用 Javascript 可以单独获取当前域名.Url.相对路径和参数,所谓单独攻取,即域名不包括网页文件的路径和参数.参数不包括域名和网页文件路径,下面分别介绍. 一.js获取当前域名有2种方法 1 ...

  7. MFC路径层的使用(BeginPath和EndPath函数)

    响应WM_PAINT消息的代码如下: void CTextView::OnDraw(CDC* pDC) { CTextDoc* pDoc = GetDocument(); ASSERT_VALID(p ...

  8. 【Scala】Scala-Option-Null的蹊跷

    Scala-Option-Null的蹊跷 scala Some(null)_百度搜索 scala - Why Some(null) isn't considered None? - Stack Ove ...

  9. [置顶] Hadoop2.2.0中HDFS的高可用性实现原理

    在Hadoop2.0.0之前,NameNode(NN)在HDFS集群中存在单点故障(single point of failure),每一个集群中存在一个NameNode,如果NN所在的机器出现了故障 ...

  10. jquery操作select(取值,设置选中)(转)

    http://www.cnblogs.com/liaojie970/p/5210541.html 比如<select class="selector"></sel ...