Largest Rectangle in a Histogram--(单调队列orDP)

来源:互联网 发布:读书笔记软件下载 编辑:程序博客网 时间:2024/06/09 15:47

Largest Rectangle in a Histogram

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 25   Accepted Submission(s) : 10
Problem 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 <i>n</i>, denoting the number of rectangles it is composed of. You may assume that <i>1<=n<=100000</i>. Then follow <i>n</i> integers <i>h<sub>1</sub>,...,h<sub>n</sub></i>, where <i>0<=h<sub>i</sub><=1000000000</i>. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is <i>1</i>. 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 34 1000 1000 1000 10000
 

Sample Output
84000

给出一系列高度,宽为1,要求求一个最大矩形面积

可以用队列求出h[i]左边和右边连续比它高的个数,设计一个升序的队列,当循环到h[i]时比他高的出队,求出它与队尾元素下标只差既是所求个数,

DP方法时,求出h[i]左边(右边)连续比他高的元素的最左(右)下标,

注意要用long long才行

代码(单调队列):

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>#include<map>using namespace std;#define M 100005int n;long long int q[M],h[M],l[M],r[M];void get_left(){    int i,head=1,tail=1;    q[1]=0;  //队列中初始化一个元素,方便运算    for(i=1;i<=n;i++)    {        while(head<=tail&&h[q[tail]]>=h[i]) tail--;  //队中高度大于等于h[i]元素出队        l[i]=i-q[tail]-1;        q[++tail]=i;    }}void get_right(){    int i,head=1,tail=1;    q[1]=n+1;    for(i=n;i>=1;i--)    {        while(head<=tail&&h[q[tail]]>=h[i]) tail--;        r[i]=q[tail]-i-1;        q[++tail]=i;    }}void get_max(){    int i;    long long int cnt=0,sum=0;    for(i=1;i<=n;i++)    {        sum=(l[i]+r[i]+1)*h[i];        if(sum>cnt) cnt=sum;    }    cout<<cnt<<endl;}int main(){    while(scanf("%d",&n)&&n)    {        for(int i=1;i<=n;i++)            scanf("%I64d",&h[i]);        h[0]=h[n+1]=-1;        get_left();        get_right();        get_max();    }    return 0;}

代码(dp):

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>#include<map>using namespace std;#define M 100005int n;int l[M],r[M];long long int h[M];void get_left(){    int i,tt;    l[1]=1;    for(i=2;i<=n;i++)    {        tt=i;        while(tt>1&&h[i]<=h[tt-1]) //如果要入队的元素比当前找到的最左元素的左边一个要矮,最左元素更新            {tt=l[tt-1];}        l[i]=tt;      }}void get_right(){    int i,tt;    r[n]=n;    for(i=n-1;i>=1;i--)    {        tt=i;        while(tt<n&&h[i]<=h[tt+1])             {tt=r[tt+1];}        r[i]=tt;    }}void get_max(){    int i;    long long int cnt=0,sum=0;    for(i=1;i<=n;i++)    {        sum=(r[i]-l[i]+1)*h[i];        if(sum>cnt) cnt=sum;    }    cout<<cnt<<endl;}int main(){    while(scanf("%d",&n)&&n)    {        for(int i=1;i<=n;i++)            scanf("%I64d",&h[i]);        get_left();        get_right();        get_max();    }    return 0;}



阅读全文
0 0
原创粉丝点击