传送门:POJ 2559 Largest Rectangle in a Histogram
描述:
Largest Rectangle in a Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19580 | Accepted: 6320 |
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
题意:
给出一系列的1*h的矩形,求矩形的最大面积。
思路:
求每一个顶点i,按照这个点的高度,向左和向右最大能拓展到的位置l[i]和h[i],然后答案就是max(a[i]*(r[i]-l[i]))。
换句话说求每个点,它左边第一个比这个点矮的点的坐标,和它右边第一个比这个点矮的坐标。
那么就用到一个叫做单调栈的神器- -。
栈底是一个极小值对应的坐标(0),即a[0]=-1,然后枚举每一个值i,如果对于栈顶的元素x,a[x]>=a[i]的话,就弹出,弹到a[x]<a[i]位置,记录l[i]=x,然后把i压栈。
在某一个状态i下,栈里保存的就是1~i-1中比a[i]小的坐标。由于每一个元素都进出栈一次,时间复杂度就是o(n)的。
同理求出r数组,注意a[n+1]=-1,还有清空栈。
代码:
#include <iostream>
#include <cstdio>
#include<stack>
#define ll __int64
using namespace std;
const int maxn=1e5+10;
int n,h[maxn];
int l[maxn],r[maxn];
stack<int>s;
int main(){
while(~scanf("%d",&n)&&n){
h[0]=h[n+1]=-1;
for(int i=1; i<=n; i++)scanf("%d",&h[i]);
int x;
while(!s.empty())s.pop();s.push(0);
for(int i=1; i<=n; i++){
for(x=s.top(); h[x]>=h[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--){
for(x=s.top(); h[x]>=h[i]; x=s.top())s.pop();
r[i]=x-1;
s.push(i);
}
ll ans=0;
for(int i=1; i<=n; i++){
ans=max(ans, (ll)h[i]*(r[i]-l[i]+1));
}
printf("%I64d\n",ans);
}
return 0;
}