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

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

    每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素。直到满足当前须要插入的元素大于栈顶元素为止。能够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. Shell变量while循环内改变无法传递到循环外

    转自: https://blog.csdn.net/shawhe/article/details/65631543 今天刷Leecode(192 Word frequency)时,遇到一个shell语 ...

  2. HDU1208:Pascal's Travels(DP)

    Problem Description An n x n game board is populated with integers, one nonnegative integer per squa ...

  3. Service 简介 启动方式 生命周期 MD

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

  4. Linux上安装Hadoop集群(CentOS7+hadoop-2.8.0)

    1下载hadoop 2安装3个虚拟机并实现ssh免密码登录 2.1安装3个机器 2.2检查机器名称 2.3修改/etc/hosts文件 2.4 给3个机器生成秘钥文件 2.5 在hserver1上创建 ...

  5. 快速教你成为C#高手教程

    C#是微软公司发布的一种面向对象的.运行于.NET Framework之上的高级程序设计语言. C#看起来与Java有着惊人的相似:它包括了诸如单一继承.接口.与Java几乎同样的语法 和编译成中间代 ...

  6. 求职之C++小知识点整理

    1.顺序容器 1.顺序容器:vector,deque,list,forward_list,array,string.其中除list和forward_list外,其它都支持快速随机访问. deque a ...

  7. Windows10 安装Jupyter

    官方文档:https://jupyter-notebook.readthedocs.io/en/stable/ https://github.com/jupyter/jupyter/wiki/A-ga ...

  8. Python 通过打码平台实现验证码

    在爬虫时,经常遇到登录需要验证码的情况,简单的验证码可以自己解决,复制的验证码需要借助机器学习,有一定的难度.还有一个简单的方案就是采用付费的打码平台. 比如R若快(http://www.ruokua ...

  9. Laravel学习笔记之Session源码解析(上)

    说明:本文主要通过学习Laravel的session源码学习Laravel是如何设计session的,将自己的学习心得分享出来,希望对别人有所帮助.Laravel在web middleware中定义了 ...

  10. 神经网络激活函数sigmoid relu tanh 为什么sigmoid 容易梯度消失

    https://blog.csdn.net/danyhgc/article/details/73850546 什么是激活函数 为什么要用 都有什么 sigmoid ,ReLU, softmax 的比较 ...