Largest Rectangle in a Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21468 | Accepted: 6912 |
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.
![](https://i-blog.csdnimg.cn/blog_migrate/45660aba95464e61adfbbcc7cea2c44c.jpeg)
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.
题意:给你n个矩形的高度,问你,可以获得的最大的矩形的面积是多少(高度是对齐的)。
首先不难想到枚举每个矩形,然后从左右两端找到第一个比当前矩形高度小的,之前的所有小矩形的和就是大矩形的宽度,当前矩形的高度,便是大矩形的高度。
更新最大值就可以了。
但重点是怎么维护这个第一个比当前矩形高度小的。二分显然不行,这里就可以采用单调栈了。
首先考虑,从当前矩形往两边走的时候,递增的序列显然是无用的,也就是做了无用的枚举。所以
我们维护一个从栈顶到栈底单调递减的一个序列,在栈中找到第一个比当前值小的,然后加上宽度即可。
注意:
单调栈内要维护两个信息,一个是高度,另外一个是位置,因为我们需要通过位置来计算宽度。
问题就解决了~
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
const int MAXN=1e5+7;
int num[MAXN],left[MAXN],right[MAXN];
int n,m,k;
pair<int,int>temp[MAXN];
int main()
{
while(~scanf("%d",&n)&&n)
{
for(int i=0;i<n;++i)scanf("%d",&num[i]);
int top=0;
for(int i=0;i<n;++i)
{
while(top&&temp[top-1].first>=num[i])top--;
if(!top)left[i]=i+1;
else left[i]=i-temp[top-1].second;
temp[top++]=make_pair(num[i],i);
}
top=0;
for(int i=n-1;i>=0;--i)
{
while(top&&temp[top-1].first>=num[i])top--;
if(!top)right[i]=n-i;
else right[i]=temp[top-1].second-i;
temp[top++]=make_pair(num[i],i);
}
long long MAX=0;
for(int i=0;i<n;++i)
{
MAX=max(MAX,num[i]*(left[i]+right[i]-1ll));
}
printf("%lld\n",MAX);
}
return 0;
}