POJ2559 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21267 Accepted: 6851

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.

          当时刚做完bc,学了单调栈和单调队列,找了点题目做做,这是个单调栈的题目
       题目要求求出这个条形图的最大阴影面积且要求是矩形,暴力解决过,没成功。用单调栈做,这里是单调递增栈。单调递增栈的概念就是只有当一个数比栈顶的元素大的时候才入栈,若比栈顶元素小的时候则对栈顶元素进行操作运算,并出栈直到栈顶元素小于当前数时再将当前的数入栈。单调递减栈则相反。
       这个题目先从前向后遍历数组入栈计算出每个矩形(包括其本身)以自身为高的右半部分的最大面积,然后再从后向前遍历数组入栈计算出每个 矩形(包括其本身)以自身为高的左半部分的最大面积,那么第i个矩形左右两端的面积之和再减去其本身的面积(计算了两次),最后比较取最大值即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
typedef long long int ll;
ll a[100005];
ll l[100005];
ll r[100005];
struct node{
   ll y;
   int x;
};
int main()
{
    int n;
    while(~scanf("%d",&n)&&n){
        for(int i = 0;i < n;scanf("%I64d",&a[i++]));
        ll ans = 0;
        stack<node>s;
        memset(l,0,sizeof(l));
        memset(r,0,sizeof(r));
        node temp;
        temp.y = a[0],temp.x = 0;
        s.push(temp);
        int width;
        for(int i = 1;i < n;i++){
            width = s.top().x;
            while(!s.empty()&&s.top().y>a[i]){
                if(s.top().y*(width-s.top().x+1)>ans)
                    r[s.top().x] = s.top().y*(width-s.top().x+1);
                s.pop();
            }
            temp.y = a[i],temp.x = i;
            s.push(temp);
        }
        width = s.top().x;
        while(!s.empty()){
            if(s.top().y*(width-s.top().x+1)>ans)
                    r[s.top().x] = s.top().y*(width-s.top().x+1);
            s.pop();
        }
        temp.x = n-1,temp.y = a[n-1];
        while(!s.empty())s.pop();
        s.push(temp);
        for(int i = n-2;i >= 0;i--){
            width = s.top().x;
            while(!s.empty()&&s.top().y>a[i]){
                if(s.top().y*(s.top().x-width+1)>ans)
                    l[s.top().x] = s.top().y*(s.top().x-width+1);
                s.pop();
            }
            temp.y = a[i],temp.x = i;
            s.push(temp);
        }
        width = s.top().x;
        while(!s.empty()){
            if(s.top().y*(s.top().x-width+1)>ans)
                    l[s.top().x] = s.top().y*(s.top().x-width+1);
            s.pop();
        }
        for(int i = 0;i < n;i++)
            ans = max(ans,r[i]+l[i]-a[i]);
        printf("%I64d\n",ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值