HDU1506(单调栈或者DP) 分类: 数据结构 2015-07-07 23:23 2人阅读 评论(0) 收藏

时间:2020-12-20 17:01:33

Largest Rectangle in a Histogram

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

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:
HDU1506(单调栈或者DP)                                                    分类:            数据结构             2015-07-07 23:23    2人阅读    评论(0)    收藏
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
 
Source

Difficulty:单调栈:栈内的元素,按照某种方式排序下(单调递增或者单调递减) 如果新入栈的元素破坏了单调性,就弹出栈内元素,直到满足单调性
 单调栈模板:

 int top=,s[maxn];
for(int i=;i<=n;i++)
{
while(top&&h[s[top]]>=h[i])//根据改变大于号小于号来改变单调性。
{
--top;//出栈,一般在while这重循环里添加东西去满足题意。
}
s[++top]=i; //入栈
}

单调栈解法:

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
long long h[];
int s[];
long long area[];
int L[];
int R[];
int n;
long long ans;
bool cmp( int a, int c)
{
return a>c; }
int main()
{
while(~scanf("%d",&n)&&n)
{
int top=;
for(int i=;i<=n;i++)
{
scanf("%lld",&h[i]);
while(top&&h[s[top]]>=h[i])
--top;
L[i]=(top==?:s[top]+);
s[++top]=i;
}
top=;
for(int i=n;i>=;i--)
{
while(top&&h[s[top]]>=h[i])
--top;
R[i]=(top==?n:s[top]-);
s[++top]=i;
}
ans=-; for(int i=;i<=n;i++)
{
area[i]=h[i]*(R[i]-L[i]+);
if(ans<area[i])
ans=area[i]; }
printf("%lld\n",ans);
}
return ;
}

DP解法:

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 251000+5
long long h[maxn];
int L[maxn];
int R[maxn];
int n,m;
int main()
{
while(~scanf("%d",&n))
{
if(n==)
break;
for(int i=;i<=n;i++)
{
scanf("%lld",&h[i]);
R[i]=L[i]=i;
}
h[]=-;
for(int i=;i<=n;i++)
{
while(h[i]<=h[L[i]-])
L[i]=L[L[i]-];
}
h[n+]=-;
for(int i=n;i>=;i--)
{
while(h[i]<=h[R[i]+])
R[i]=R[R[i]+];
}
long long max1=;
for(int i=;i<=n;i++)
{
max1=max(max1,(R[i]-L[i]+)*h[i]);
}
printf("%lld\n",max1);
} return ;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。