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

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

    每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素。直到满足当前须要插入的元素大于栈顶元素为止。能够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. 析构函数 (C++)

    最近发现自己对析构函数的认知有一定的问题,因为之前有在使用placement new时主动调用对象的析构函数,所以觉得析构函数只是个普通的成员函数,调用的时候只会执行自己方法体内的代码内容,而回收内存 ...

  2. iOS截屏方法

    //获取屏幕截屏方法 - (UIImage *)capture { // 创建一个context UIGraphicsBeginImageContextWithOptions(self.view.bo ...

  3. 写出优质Java代码的4个技巧(转)

    http://geek.csdn.net/news/detail/238243 原文:4 More Techniques for Writing Better Java 作者:Justin Alban ...

  4. 4444: [Scoi2015]国旗计划|贪心|倍增

    由于没有区间被其它区间包括这个条件,也就是假设li<lj那么一定满足ri<rj,就能够贪心搞一搞了. 假如区间[l,r]都已经被覆盖,那么能够继续找一个li在[l,r]范围内的最大的一个, ...

  5. Struts2之server端验证

    声明:在我的教程中有些东西,没有提及到.不是我不知道,而是在我个人来看对你们不是太重要的知识点.你们在看课本时有了解到即可.我不会面面俱到的都给你们提及.我写博文的目的是把我这一年的开发经验通过学习s ...

  6. dubbo 提示No such extension Filter for filter/com.alibaba.dubbo.rpc.Filter

    配置时 <dubbo:provider filter="DubboExceptionFilter"></dubbo:provider> DubboExcep ...

  7. “Uncaught TypeError: Cannot call method 'createChild' of undefined" 问题的解决

    Uncaught TypeError: Cannot call method 'createChild' of undefined 我在使用Ext 4.1.1做grid.Panel,然后chrome爆 ...

  8. iOS中文API之UIResponder介绍

    该UIResponder类定义了响应和处理事件的对象接口.它是UIView和UIApplication的超类(UIWindow).这些类的实例有时被称为响应者对象,简单地说就是响应者. 通常有2种事件 ...

  9. Android自己定义View之仪表盘

    新建项目,新建DashBoardView继承自View实现OnGlobalLayoutListener接口,并重写OnDraw方法. 使用OnGlobalLayoutListener接口须要重写onG ...

  10. android中使用toolbar

    系统默认使用的是ActionBar,就是界面中的标题栏,但是由于ActionBar设计的原因,被限定只能位于活动的顶部,从而不能实现Material Design效果,所以官方建议使用Toolbar替 ...