http://poj.org/problem?id=2559
Largest Rectangle in a Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21489 | Accepted: 6923 |
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.
题目大意:同样是先看图,有这样的一些矩阵,宽度都是1,高度都不同,求一个面积最大的矩形。
题解:当以第i个矩形为中心高度的时候,那么往左往右的矩形高度都要比第i高才行。那就看看以第i为中心高度的时候的左边界和右边界在哪就行了。问题是怎么求左边界(右边界同样的)。这里用到单调栈的知识,既然是单点肯定是递增(或递减)的,在这个题中用的是递增的。自己模拟一下它的过程的话就是当加入一个之后,如果这个值比前面的大就直接入栈,否则将栈顶出站知道一个比自己小的或者占空。当然了,栈中存的是矩形高度的序号,毕竟要求的是左右边界。
ps:在这里顺便说一下单调队列的作用:求后i个数的最大值或最小值
///单调栈的应用-比自身大的左右边界
///单调队列的应用-求后n个数中的最大值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
typedef long long LL;
LL a[100100];
int l[100100],r[100100];
int main()
{
int n;
while(scanf("%d",&n),n)
{
stack<int>s;
while(!s.empty())s.pop();
a[0]=a[n+1]=-1;
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
}
s.push(0);
for(int i=1; i<=n; i++)///利用单调栈求左边界
{
int x;
for(x=s.top(); a[x]>=a[i];x=s.top())
{
s.pop();
}
l[i]=x+1;
s.push(i);
}
while(!s.empty())s.pop();
s.push(n+1);
for(int i=n;i>=1;i--)///利用单调栈求右边界
{
int x;
for(x=s.top();a[x]>=a[i];x=s.top())
{
s.pop();
}
r[i]=x-1;
s.push(i);
}
LL ans=-1;
for(int i=1;i<=n;i++)
{
ans=max(ans,(r[i]-l[i]+1)*a[i]);
}
printf("%lld\n",ans);
}
return 0;
}