POJ - 2559 单调栈

算是回顾吧

扫描计算时的高度是单调递减的,

最相对较高的能延伸的矩阵[全部]计算完以后都合并成一个待计算的相对较矮的单一矩阵

规定合并后的矩阵留到下一次计算

一直扫描到栈空(哨兵h[n+1]==0处)即可

STL炒鸡好用干嘛要手写啊

/*H E A D*/
int main(){
    int n,h[maxn],w[maxn];
    stack<int> stk;
    while(~iin(n)){
        if(n==0)break;
        rep(i,1,n) h[i]=read();
        while(!stk.empty()) stk.pop();
        h[n+1]=0;
        ll ans=0;
        rep(i,1,n+1){
            if(stk.empty()||stk.top()<h[i]){
                stk.push(h[i]);
                w[stk.size()]=1;
            }else{
                int width=0;
                while(!stk.empty()&&stk.top()>h[i]){
                    int t=stk.top();
                    width+=w[stk.size()];
                    stk.pop();
                    ans=max(ans,1ll*t*width);
                }
                stk.push(h[i]);
                w[stk.size()]=width+1;
            }
        }
        println(ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/caturra/p/8408278.html

时间: 2024-08-30 03:32:49

POJ - 2559 单调栈的相关文章

poj 2059 单调栈

题意:求柱状图中最大矩形面积. 单调栈:顾名思义就是栈内元素单调递增的栈. 每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素.直到满足当前须要插入的元素大于栈顶元素为止.能够easy求出某个数左边或右边,第一个大于或小于它的数,且复杂度是O(n). 思路:easy先想到一个好的枚举方式:以当前柱状为扩展点,往左边和右边扩展.当遇到一个比当前柱状小的柱状时停止扩展.以当前柱状的高度为矩形的高.向左右扩展的距离之差为矩形的长度,这样对n个柱状进行扫描之后可得最大

[poj 2796]单调栈

题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include<algorithm> #include<stack> using namespace std; const int maxn=100005; int a[maxn]; int l[maxn]; int r[maxn]; long long pre[maxn]; stack< p

POJ 2796 Feel Good(单调栈)

题目地址:POJ 2796 单调栈的第一题就是这道..把我弄的晕头转向.现在终于明白了,对单调栈又加深了理解.原来单调栈不只是可以维护数. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #incl

POJ 2559 Largest Rectangle in a Histogram RMQ || 单调栈

题目链接:点击打开链接 题意就是求最大面积 枚举每个柱子作为起点 然后二分两边长度. 求个区间最值. #include<stdio.h> #include<iostream> #include<math.h> using namespace std; #define ll long long #define N 100100 inline bool rd(int &n){ int x = 0, tmp = 1; char c = getchar(); while

POJ 2559 Largest Rectangle in a Histogram(单调栈) &amp;&amp; 单调栈

嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递减. 2.模样: 这是一个单调递增的栈,如果我们插入的元素大于栈顶元素,则直接入栈: 如果我们插入的元素小于栈顶,则需要把栈内所有大于它的元素暂时出栈,将这个元素入栈后,再将暂时出栈的元素入栈,维护单调性. 二.模板: 这道题是单调栈的一道模板题: 先思考一个问题,如果题目中的矩形的高度都是单调递增

POJ 2559 Largest Rectangle in a Histogram(单调栈)

Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectang

POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831   Accepted: 5121 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi

POJ 3415 Common Substrings(后缀数组+单调栈)

[题目链接] http://poj.org/problem?id=3415 [题目大意] 求出两个字符串长度大于k的公共子串的数目. [题解] 首先,很容易想到O(n2)的算法,将A串和B串加拼接符相连, 做一遍后缀数组,把分别属于A和B的所有后缀匹配,LCP-k+1就是对答案的贡献, 但是在这个基础上该如何优化呢. 我们可以发现按照sa的顺序下来,每个后缀和前面的串的LCP就是区间LCP的最小值, 那么我们维护一个单调栈,将所有单调递减的LCP值合并, 保存数量和长度,对每个属于B串的后缀更新

POJ 3250 Bad Hair Day(单调栈)

题目地址:POJ 3250 初学单调栈.多校和网络赛已经碰到两次了. 单调栈的原理简单的不能再简单了..就是让栈里的元素从栈顶到栈底呈单调性. 比如说递增单调栈. 每次放进一个数的时候,如果栈顶的数小于要放的数,就把栈顶的数pop出来使得栈里保持单调性. 对于这道题来说,就从右往左开始遍历,建一个递增单调栈.那么每次pop出来的就是当前的牛可以看到的牛数.然后累加即可. 代码如下: #include <iostream> #include <cstdio> #include <