HDOJ 1506 Largest Rectangle in a Histogram(单调栈)

LargestRectangle in a Histogram

Time Limit: 2000/1000 MS(Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17953    Accepted Submission(s): 5368

Problem Description

A histogram is a polygoncomposed of a sequence of rectangles aligned at a common base line. Therectangles have equal widths but may have different heights. For example, thefigure on the left shows the histogram that consists of rectangles with theheights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of therectangles:

Usually, histograms are used to represent discrete distributions, e.g., thefrequencies of characters in texts. Note that the order of the rectangles,i.e., their heights, is important. Calculate the area of the largest rectanglein a histogram that is aligned at the common base line, too. The figure on theright shows the largest aligned rectangle for the depicted histogram.

 

 

Input

The input contains severaltest 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 thehistogram in left-to-right order. The width of each rectangle is 1. A zerofollows the input for the last test case.

 

 

Output

For each test case output on asingle 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


此题的思路便是对于每一列找到使它面积最大的左边界与右边界,进一步分析可知:

左边界L是满足h[i-1]<h[now] 的最大的 i,即从当前列开始向左遍历找到的第一个比当前列高度小的列的标号+1 (h[i]为每列的高度)

同理,左边界R是满足h[i+1]<h[now] 的最小的 i,即从当前列开始向右遍历找到的第一个比当前列高度小的列的标号-1

(开始分析时,觉得按以上方法遍历找到的L,R构成的面积并不是最优解,觉得可以继续遍历使水平长度变大,高度减小点也没关系,但后来一想这种情况已经包含在其他列的情况中了。。。)


给出两种解法,一种是单调栈(推荐),还有就是DP


单调栈

#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
const int maxn = 100005;
const int mod = 9973;
#define ll long long
#define rush() int T;scanf("%d",&T);while(T--)
ll a[maxn],L[maxn],R[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        f(i,0,n)
        {
            scanf("%I64d",&a[i]);
        }
        stack<int> s;
        f(i,0,n)
        {
            while(s.size()&&a[s.top()]>=a[i])
                s.pop();
            if(s.empty())
                L[i]=0;
            else L[i]=s.top()+1;
            s.push(i);
        }
        while(s.size())
            s.pop();
        for(int i=n-1;i>=0;i--)
        {
            while(s.size()&&a[s.top()]>=a[i])
                s.pop();
            if(s.empty())
                R[i]=n;
            else R[i]=s.top();
            s.push(i);
        }
        ll ans=0;
        f(i,0,n)
        {
            ll cnt=a[i]*(R[i]-L[i]);
            ans=max(ans,cnt);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

DP


#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
const int maxn = 100005;
const int mod = 9973;
#define ll long long
#define rush() int T;scanf("%d",&T);while(T--)
ll a[maxn],L[maxn],R[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        f(i,0,n)
        {
            scanf("%I64d",&a[i]);
        }
        L[0]=0;
        R[n-1]=n-1;
        f(i,1,n)
        {
            int t=i;
            while(t>0&&a[i]<=a[t-1])
                t=L[t-1];
            L[i]=t;
        }
        for(int i=n-2;i>=0;i--)
        {
            int t=i;
            while(t<n-1&&a[i]<=a[t+1])
                t=R[t+1];
            R[i]=t;
        }
        ll ans=0;
        f(i,0,n)
        {
            ll cnt=a[i]*(R[i]-L[i]+1);
            ans=max(ans,cnt);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值