POJ - 2559 Largest Rectangle in a Histogram (单调栈与区间问题)

37 篇文章 0 订阅
17 篇文章 4 订阅
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.

题意:n个矩形他们的宽均为1,给出它们的高,求出由n个矩形构成的矩形的最大面积.

思路:算出每个矩形最左边小于高的位置和最右边小于高的位置。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#define N 100005
using namespace std;
typedef long long ll;
ll h[N],l[N],r[N];
int main()
{
    int n;
    while(scanf("%d",&n)==1&&n!=0){
        for(int i=1;i<=n;i++){
            scanf("%d",&h[i]);
            l[i]=r[i]=i;
        }
        h[0]=h[n+1]=-1;
        for(int i=1;i<=n;i++){
            int j=i-1;
            while(h[j]>=h[i]) //找到最左边高度小于当前高度
                j=l[j]-1;     //l[j]肯定满足条件,则减一
            l[i]=j+1;         //更新j位置小于h[i],则j+1位置满足条件
        }
  //利用了更新,例如样例  2 1 4 5 1 3 3  在第2个1向前寻找的时候先是和5比较然后j=l[4]-1=4-1=3
  //然后再和位置3的数字比较,j=l[3]-1=2,再和位置是2的数字比较,j=l[2]-1=0,所以l[5]=1
        for(int i=n;i>=1;i--){
            int j=i+1;
            while(h[j]>=h[i])
                j=r[j]+1;
            r[i]=j-1;
        }
        ll ans=0;
        for(i=1;i<=n;i++)
            ans=max(ans,(r[i]-l[i]+1)*h[i]);
        printf("%lld\n",ans);
    }
    return 0;
}
建立一个单调递增栈,所有元素各进栈和出栈一次即可。每个元素出栈的时候更新最大的矩形面积。
设栈内的元素为一个二元组(x, y),x表示矩形的高度,y表示矩形的宽度。
若原始矩形高度分别为2,1,4,5,1,3,3
高度为2的元素进栈,当前栈为(2,1)
高度为1的元素准备进栈,但必须从栈顶开始删除高度大于或等于1的矩形,因为2已经不可能延续到当前矩形。删除(2,1)这个元素之后,更新最大矩形面积为2*1=2,然后把它的宽度1累加到当前高度为1的准备进栈的矩形,然后进栈,当前栈为(1,2)
高度为4的元素进栈,当前栈为(1,2) (4,1)
高度为5的元素进栈,当前栈为(1,2) (4,1) (5,1)
高度为1的元素准备进栈,删除(5,1)这个元素,更新最大矩形面积为5*1=5,把1累加到下一个元素,得到(4,2),删除(4,2),更新最大矩形面积为4*2=8,把2累加到下一个元素,得到(1,4),1*4=4<8,不必更新,删除(1,4),把4累加到当前准备进栈的元素然后进栈,当前栈为(1,5)
高度为3的元素进栈,当前栈为(1,5) (3,1)
高度为3的元素准备进栈,删除(3,1),不必更新,把1累加到当前准备进栈的元素然后进栈,当前栈为(1,5) (3,2)
把余下的元素逐个出栈,(3,2)出栈,不必更新,把2累加到下一个元素,当前栈为(1,7),(1,7)出栈,不必更新。栈空,结束。

最后的答案就是8。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
#define N 100005
using namespace std;
typedef long long ll;
typedef pair <ll,ll> pll; //h,w
int main()
{
    int n,h;
    while(scanf("%d",&n)==1&&n!=0){
        stack <pll>q;
        ll ans=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&h);
            ll w=0;
            while(!q.empty()&&q.top().first>=h){ //当栈顶元素大于当前元素
                ll tmph=q.top().first; 
                ll tmpw=q.top().second; 
                q.pop();
                w=w+tmpw; 
                ans = max(ans,tmph*w);    //如果大于当前的最大面积就更新
            }                             //因为左边的比他小,所以只需要向右延伸就可以了
            q.push(make_pair(h,w+1));     //所以每次出栈的时候,就是出栈后的栈顶元素宽度+1
        }
         int tmp = 0;
        while(!q.empty()){
            ans = max(ans,q.top().first*(tmp+q.top().second));//原理和上面一样
            tmp+=q.top().second;
            q.pop();
        }
        printf("%lld\n",ans);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值