Largest Rectangle in a Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18332 | Accepted: 5891 |
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.
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.
Source
题解:单调栈
单调栈的模板题。单调栈与单调队列有所不同,单调队列可以求一段区间的最值,单调栈可以求以某个数为最值的最长区间。在这道题里要求以某个数为最小值的最长区间,于是维护一个单调递增的栈,依次把所有元素进栈,对于每个元素维护它作为最小值的区间的左端点l和右端点r,注意到单调栈的性质,所有元素的序号也是依次递增的,而因为在把一个元素进栈之前需要把所有大于等于它的都弹出,于是我们可以发现对于将要进栈的元素i,从栈顶弹出一个元素相当于发现了它左边一个比它大的元素,于是就可以把i元素的左端点l[i]移动到栈顶元素的左端点l[s[top]]。这里可能会注意到在s[top]和i中间的那些元素,因为假如它们中间有空缺,那一定是有某些元素出栈了,而出栈的这些元素一定是大于a[s[top]]的,所以可以直接继承。而当栈顶元素出栈以后,新的栈顶元素就发现了一个在它右边并且比它大的值,就可以用r[s[top]]来更新r[s[top-1]]。最后当不能再出栈以后,对于现在的栈顶元素s[top],i也是一个在它右边并且比它大的元素,同样更新r。最后全部元素扫描完以后栈里面可能还有一些元素,而这些元素的r是没有被完全更新的,所以要依次出栈并且更新r,就相当于让它们发现那些在右边并且比它们大的元素。
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
ll m,n,a[200003],top,st[200003];
struct data
{
ll l,r;
};data f[200003];
void insert(ll x)
{
while(a[x]<=a[st[top]]&&top)
{
f[x].l=f[st[top]].l;
f[st[top-1]].r=f[st[top]].r;
top--;
}
f[st[top]].r=f[x].r;
st[++top]=x;
}
int main()
{
while (scanf("%lld",&n))
{
if (n==0) break;
for (ll i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
f[i].l=f[i].r=i;
}
for (ll i=1;i<=n;i++)
insert(i);
while(top)
f[st[top-1]].r=f[st[top]].r,top--;
ll ans=0;
for (ll i=1;i<=n;i++)
ans=max(ans,(f[i].r-f[i].l+1)*a[i]);
printf("%lld\n",ans);
}
return 0;
}