美文网首页
【单调栈】POJ_2559_Largest Rectangle

【单调栈】POJ_2559_Largest Rectangle

作者: 今天也继续开心涅普涅普 | 来源:发表于2016-10-18 01:04 被阅读0次

Largest Rectangle in a Histogram

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19689 Accepted: 6370

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 rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:

Paste_Image.png

Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.

Input
The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1<=n<=100000. Then follow n integers h1,...,hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.

Output
For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.

Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0

Sample Output
8
4000

Hint
Huge input, scanf is recommended.

题意:
有一堆宽度恒为1、高度不定的长方形排成一列,找出最大面积的长方形。

思路:
单调栈。维护一个高度单调递增的栈(没有相等项),对于每一次进栈,判断其元素高度是否大于栈顶元素:若大于,入栈;若小于,则弹出栈顶元素,每次弹出计算面积,覆盖最大值。输入完毕后得到单调栈,从栈顶开始逐个出栈,每次出栈计算面积,覆盖最大值。最终输出最大值。

//使用stack

#include<cstdio>
#include<stack>
using namespace std;

struct Node {
    int height;
    int width;
    Node(int h = 0, int w = 0):height(h), width(w) {};
};
stack<Node> st;
long long ans, cur;
long long width;
int height;
int n;

int main() {
    while (scanf("%d", &n)!=EOF && n) {
        ans = 0;
        for (int i = 0; i < n; ++i) {
            scanf("%d", &height);
            width = 0;
            while (!st.empty() && st.top().height >= height) {
                cur = st.top().height * (st.top().width + width);
                if (cur>ans)
                    ans = cur;
                width += st.top().width;
                st.pop();
            }
            st.push(Node(height, width + 1));
        }
        width = 0;
        while (!st.empty()) {
            cur = st.top().height * (st.top().width + width);
            if (cur > ans)
                ans = cur;
            width += st.top().width;
            st.pop();
        }
        printf("%lld\n", ans);
    }
}

//使用数组

#include<cstdio>
using namespace std;

const int maxn = 100000;

struct Node {
    int height;
    int width; 
    Node(int h = 0, int w = 0) :height(h), width(w) { };
}st[maxn + 10];
int top;

int main() {
    long long ans, cur;
    long long width;
    int height;
    int n;
    while (scanf("%d", &n) != EOF && n) {
        top = 0;
        ans = 0;
        for (int i = 0; i < n; ++i) {
            scanf("%d", &height);
            width = 0;
            // 出栈
            while (top>0 && st[top - 1].height >= height) {
                cur = st[top - 1].height * (st[top - 1].width + width);
                if (cur > ans)
                    ans = cur;
                width += st[top - 1].width;
                --top;
            }
            // 进栈
            st[top].height = height;
            st[top].width = width + 1;
            ++top;
        }
        // 在栈中找最大
        width = 0;
        while (top > 0) {
            cur = st[top - 1].height * (st[top - 1].width + width);
            if (cur > ans)
                ans = cur;
            width += st[top - 1].width;
            --top;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

相关文章

  • 【单调栈】POJ_2559_Largest Rectangle

    Largest Rectangle in a Histogram Time Limit: 1000MS ...

  • 单调 栈&&队列

    题目链接:Largest Rectangle in a Histogram 单调栈: 题目链接:Imbalance...

  • leetcode 题解 84. Largest Rectangl

    leetcode 题解 84. Largest Rectangle in Histogram (单调栈的应用们) ...

  • 2019-07-02 单调栈

    单调栈在LC中运用到很多,非常典型的题就是84题,Largest Rectangle in Histogram,题...

  • 单调栈和应用实践

    什么是单调栈 单调栈的定义:单调栈即满足单调性的栈结构。与单调队列相比,其只在一端进行进出。 如何使用单调栈 单调...

  • 1.单调栈

    一、单调栈定义 单调栈(monotone-stack)是指栈内元素(栈底到栈顶)都是(严格)单调递增或者单调递减的...

  • 单调栈 2020-06-12(未经允许,禁止转载)

    1.单调栈 指栈内元素保持单调性的栈结构,分为单调增栈(栈底到栈顶元素递增)和单调减栈(栈底到栈顶元素递减) 2....

  • LeetCode刷题指北----单调栈

    1.什么是单调栈?有什么好处? 定义: 单调栈就是栈内元素递增或者单调递减的栈,并且只能在栈顶操作。单调栈的维护是...

  • C语言之单调栈

    单调栈 What 单调栈也是栈的一种,满足先进后出的原则,另外,单调栈中的元素是有序的,分为两种 单调递增栈:单调...

  • 腾讯笔试--逛街

    主要的知识点是:单调栈,该题牢牢记得:栈中记录当前楼能看到的元素 单调栈是单调递增栈,栈顶是最小值单调栈存的是能看...

网友评论

      本文标题:【单调栈】POJ_2559_Largest Rectangle

      本文链接:https://www.haomeiwen.com/subject/svbzyttx.html