POJ - 2559 Largest Rectangle in a Histogram 单调栈

Largest Rectangle in a Histogram

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26415 Accepted: 8536

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

Ulm Local 2003


题目

给出一个柱形统计图(histogram), 它的每个项目的宽度是1,高度输入。 现在编程求出在这个柱形图中的最大面积的长方形。

样例参考题目


题解

朴素想法

考虑每一个柱型块i为中心,即最后得到的长方体的高度为他的高度height[i]。

如果以他为中心能得到多大面积的长方体,显然他可以向两边扩展。

而扩展的条件是,高度必须不小于他,即height[j] >= hight[i]。

因为如果height[j]<height[i]则就不能以i的高度作为最后得到长方体的高度。

那么对于每一个柱型块i,需要找出左右两边第一个小于i的位置分别为L[i]和R[i]。

于是高度为height[i]的长方体的宽度为(R[i]-L[i]-1)

朴素算法O(n^2) 

考虑用单调栈优化

我们维护一个单调非减序列,考虑一次进栈位置i

假设qu[t](队列尾) > height[i] 说明qu[t].id(其实际存放位置) 可以作为 i 的L[i] 这里L[i] 代表左边最后一个比i大的位置。

于是在柱型i进栈的过程中,其最后停放的位置就是其L[i]的值。

同时考虑对尾元素出栈时候,其出栈是因为qu[t] > height[i] 则 位置 i 就是 qu[t] 的 右边第一个小于其的位置,即R[qu[t]]

那么我们在入栈时得到其L[i],出栈时得到其R[i]

就可以在出栈时更新答案

时间复杂度O(n)


代码 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
int n,arr[maxn];
pair<int,int> qu[maxn];
int main()
{
    while(~scanf("%d",&n),n) {
        ll res = 0;
        int h=1,t=0;
        for(int i=0;i<n;i++) scanf("%d",&arr[i]);
        for(int i=0;i<n;i++) {
            pair<int,int> tmp = make_pair(i,arr[i]);
            while(h <= t && qu[t].second > arr[i]) {
                res = max(res,1LL*(i-qu[t].first)*qu[t].second);
                tmp.first = qu[t].first;
                t--;
            }
            qu[++t] = tmp;
        }
        while(h <= t) {
            res = max(res,1LL*(n-qu[t].first)*qu[t].second);
            t--;
        }
        printf("%lld\n",res);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值