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\n", 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;
}
复制代码

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

 
 
 
 
 
posted @   Pat  阅读(308)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
点击右上角即可分享
微信分享提示