Largest Rectangle in a Histogram

来源:互联网 发布:淘宝店推广引流的技巧 编辑:程序博客网 时间:2024/05/22 03:13

题目:Largest Rectangle in a Histogram
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506
题目描述:
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.

解题思路:
这道题用DP可以实现,也可以用栈来实现,DP的思想较为简单,代码也比较容易懂,但栈的方法效率会更高,写出来的代码也会更加简洁,下面我来讲一讲DP的思路,网上也能找到用栈解决的好代码,待会我会推荐一个博客你们可以看一下。
DP的想法:
如果确定了长方形的左端点L和右端点R,那么最大可能的高度就是min{hi|L <= i < R}。
找左边比它大的,右边比它大的,枚举一下就可以,暴力找肯定超时,用dpl[i]计一下左边不小于的,如果左边第一个不小于它,则左边dpl[i-1]个都不小于它,则这dpl[i-1]个就课以跳过,再往后找,直到找到比它小的。同理右边。
首先定义两个数组l[N]跟r[N];
l[i]这个数组中存放的数字为比a[i]大的连续最左边的位置
r[i]这个数组中存放的数字为比a[i]大的连续最右边的位置
接着再定义一个数组d[i]=(r[i]-l[i]+1)*a[i];对d[i]进行sort排序,输出最大的即可
下面是我的AC代码:

#include<iostream>#include<cstdlib>#include<stdio.h>#include <algorithm>using namespace std;const int N=100010;long long a[N],d[N],l[N],r[N],maxn,s;int main(){    int n;    while(scanf("%d",&n)==1)    {        if(n == 0)            break;        else        {            for(int i=1; i<=n; i++)                scanf("%I64d",&a[i]);            l[1]=1;            r[n]=n;            for(int i=2; i<=n; i++)            {                int sum=i;                while(sum>1&&a[i]<=a[sum-1])//dp的想法                    sum=l[sum-1];                l[i]=sum;//l[i]这个数组中存放的数字为比a[i]大的连续最左边的位置            }            for(int i=n-1; i>=1; i--)            {                int add=i;                while(add<n&&a[i]<=a[add+1])                    add=r[add+1];                r[i]=add;//r[i]这个数组中存放的数字为比a[i]大的连续最右边的位置            }            maxn=0;            for(int i=1; i<=n; i++)            {                d[i]=(r[i]-l[i]+1)*a[i];            }            sort(d,d+n+1);            printf("%I64d\n",d[n]);        }    }    return 0;}

下面是栈的做法
借用一下这位博主的博文,如有冒犯请联系删除,谢谢!
http://www.cnblogs.com/lichen782/p/leetcode_Largest_Rectangle_in_Histogram.html

原创粉丝点击