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

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

     
     
     
     
     
  • 相关阅读:
    Linux下Keepalived 安装与配置
    Nginx配置SSL证书部署HTTPS网站
    Nginx 实现MySQL的负载均衡
    iOS如何把导航默认的返回按钮设置成“返回”
    Java中JSON的简单使用与前端解析
    双机高可用、负载均衡、MySQL(读写分离、主从自动切换)架构设计
    iOS开发--使用RSA加密
    UIBezierPath画圆弧的记录
    SSL构建单双向https认证
    webpack配置上线地址
  • 原文地址:https://www.cnblogs.com/Patt/p/5067898.html
Copyright © 2011-2022 走看看