Largest Rectangle in a Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19452 | Accepted: 6267 |
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.
Source
用动态规划。求出每一个矩形的左边比它大的矩形的位置边界,同理求出右边的,这样就保证了每个矩形都做到此高度面积最大。然后再求每一个矩形最大面积的最大值就是整体最大值。有很多需要注意的细节,矩形高度要long long 型,不要用memset,否则超时,看来memset太慢了,以后能不用就不用。188ms。#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#define LL long long
#include<algorithm>
#define LCM(x) memset(x,-1,sizeof(x))
using namespace std;
const int N = 100000+10;
LL rec[N],Left[N],Right[N];
int main() {
int n;
while(scanf("%d",&n),n) {
// LCM(Left);
// LCM(Right); //此用法超时
for(int i=1; i<=n; i++) {
scanf("%lld",&rec[i]);
Left[i]=Right[i]=i;
}
rec[0]=rec[n+1]=-1;
for(int i=1; i<=n; i++) {
while(rec[i]<=rec[Left[i]-1]) {
Left[i]=Left[Left[i]-1];
}
}
for(int i=n; i>=1; i--) {
while(rec[i]<=rec[Right[i]+1]) {
Right[i]=Right[Right[i]+1];
}
}
LL ans,sum;
ans=-1<<30;
for(int i=1; i<=n; i++) {
sum=(Right[i]-Left[i]+1)*rec[i];
ans=max(sum,ans);
}
printf("%lld\n",ans);
}
return 0;
}
需要注意的是用数组模拟单调栈比直接用STL的栈写要快一些,这个157ms,而直接用栈跑出来是266ms。
单调栈写法:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
const int N = 100000+10;
int stack[N],top;
LL rec[N];
int main() {
int n;
while(scanf("%d",&n),n) {
for(int i=0; i<n; i++) {
scanf("%lld",&rec[i]);
}
rec[n]=-1;
LL ans,temp,cnt;
ans=-1<<30;
for(int i=0; i<=n; i++) {
if(!top||rec[stack[top-1]]<rec[i]) {
stack[top++]=i;
}
else if(rec[i]<rec[stack[top-1]]){
while(top&&rec[stack[top-1]]>=rec[i]) {
temp=(i-stack[top-1])*rec[stack[top-1]];
ans=max(temp,ans);
cnt=stack[top-1];
top--;
}
stack[top++]=cnt;
rec[cnt]=rec[i];
}
}
printf("%lld\n",ans);
}
return 0;
}
题目地址:http://poj.org/problem?id=2559