zoukankan      html  css  js  c++  java
  • POJ 2559 Largest Rectangle in a Histogram

    传送门

    Time Limit: 1000MS  Memory Limit: 65536K

     

    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:

    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.

    Source


    Solution
    假设已经确定矩形的底边边为[l, r),则最大矩形的高为 min{h[i], l <= i < r}。
    现在反过来假设已经确定矩形的高由第i个小矩形提供,那么矩形的底边长的最大值是多少?
    分别用L[i]和R[i]表示由第i个小矩形提供高的最大矩形的左右边界,将此矩形的底边边界表示成[L[i], R[i])
    则L[i]=max{ j: j<=i, h[j]>=h[i]},R[i] = min{ j: j>i, h[j]<h[i]}
    利用栈这一数据结构在O(n)时间内求出L[ ]和R[ ]。

    Implementation
    单调栈
    #include <cstdio>
    #include <stack>
    using namespace std;
    typedef long long LL;
    
    const int N(1e5+5);
    
    int h[N], L[N], R[N];
    
    stack<int> st;
    //[L[i], R[i])
    void solve(int n){
        for(; st.size(); st.pop());
        for(int i=0; i<n; i++){
            for(; !st.empty()&&h[st.top()]>=h[i]; st.pop());
            if(st.empty()) L[i]=0;
            else L[i]=st.top()+1;
            st.push(i);
        }
        for(; st.size(); st.pop());
        for(int i=n-1; ~i; i--){
            for(; !st.empty() && h[st.top()]>=h[i]; st.pop());
            if(st.empty()) R[i]=n;
            else R[i]=st.top();
            st.push(i);
        }
        LL res=0;
        for(int i=0; i<n; i++)
            res=max(res, (LL)h[i]*(R[i]-L[i]));
        printf("%lld
    ", res);
    }
    
    int main(){
        for(int n; scanf("%d", &n), n; ){
            for(int i=0; i<n; i++) scanf("%d", h+i);
            solve(n);
        }
        return 0;
    }

    注:用数组实现栈写起来更方便一些。

     
     
     
     
     
  • 相关阅读:
    整合ssm
    卸载,安装JDK
    基本的Dos命令
    基本的Dos命令
    Typora软件操作教程(简单的编写文档,方便,美观,有效的 HTML 文档)
    Vuex 第6节 module模块组
    Vuex 第5节 actions异步修改状态
    Vuex 第4节 getters计算过滤操作
    Vuex 第3节 Mutations修改状态
    Vuex 第2节 state访问状态对象
  • 原文地址:https://www.cnblogs.com/Patt/p/5067898.html
Copyright © 2011-2022 走看看