【单调栈】POJ_2559_Largest Rectangle in a Histogram

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;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 先说说我本人,80后,即将三十岁,毕业于一所二本的文科专业。我在创业前因为兴趣稍微学了点编程知识,但从来没自己弄过...
    冲锋阅读 460评论 0 1
  • 没有人能脱离家庭教育,但是教育在撒谎! (一)在教育市场上有个非常诡异的现象: 很多教育大咖们声称,自己有一套神奇...
    南匀木阅读 659评论 0 7
  • 文芾 雨落在飞鸟 黑色的背上 落在婴儿车 绿色的伞顶 雨看透了 整个秋天 看透了我 雨代替阳光 还原着万物 雨是深度
    徐文显阅读 168评论 0 3