HDU 1506 Largest Rectangle in a Histogram (单调栈or笛卡尔树)

Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22361    Accepted Submission(s): 6925


 

Problem 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.

 

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

单调栈:

/*
题意:给定n个宽度为1的矩形,求最大矩形面积

思路:单调栈维护左右值,出栈时计算   max{(r[i]-l[i])*h[i]} 

*/



#include<bits/stdc++.h>

using namespace std;
const int maxn=1e5+10;

long long r[maxn],l[maxn],h[maxn],sta[maxn],top;

int main(){
	long long n,x;
	while(~scanf("%lld",&n)){
		long long maxx=0;
		if(n==0) break;
		top=0;
		for(int i=0;i<n;i++){
			scanf("%lld",&x);
			if(top==0){
				sta[top]=x;
				l[top]=i;
				r[top]=i+1;
				top++;
			}else if(top && sta[top-1] > x){
				while(top && sta[top-1] > x){
					maxx=max(sta[top-1]*(r[top-1]-l[top-1]),maxx);
					top--;			
					r[top-1]=r[top];
				}
				sta[top]=x;
				r[top++]=i+1;
			}else{
				sta[top]=x;
				l[top]=i;
				r[top++]=i+1;
			}
		}
		while(top){
			maxx=max(sta[top-1]*(r[top-1]-l[top-1]),maxx);
			top--;	
			r[top-1]=r[top];
		}
		printf("%lld\n",maxx);
	}
}

笛卡尔树:

/*
hdu1506

Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0

Sample Output
8
4000

思路:笛卡尔树,维护小顶堆,计算 max{ 树 大 小 * h }    ,注意long long 

*/

#include<bits/stdc++.h>

using namespace std;
const int maxn=1e5+10;
int h[maxn],l[maxn],r[maxn],pre[maxn],sta[maxn];
int n;
long long ans;

int build(){
	int top=0;
	for(int i=1;i<=n;i++){
		while(top && h[sta[top-1]]>h[i]) top--;
		if(!top){
			int t=sta[0];
			pre[t]=i;
			l[i]=t;
		}else{
			int t=sta[top-1];
			if(r[t]){
				l[i]=r[t];
				pre[r[t]]=i;
			}
			pre[i]=t;
			r[t]=i;
		}
		sta[top++]=i;
	}
	return sta[0];
}

int dfs(int x){
	if(x==0) return 0;
	int num = dfs(r[x])+dfs(l[x])+1;	
	long long tmp=(long long)num*h[x];			//乘法溢出 
	ans=max(ans,tmp);
	return num;
}

int main(){
	while(~scanf("%d",&n)){
		memset(r,0,sizeof r);					//这初始化有点难受 
		memset(l,0,sizeof l);
		memset(pre,0,sizeof pre);
		memset(sta,0,sizeof sta);
		memset(h,0,sizeof h);
		
		if(n==0) break;
		
		for(int i=1;i<=n;i++){
			scanf("%d",&h[i]); 
		}
		
		int t=build();
		ans=0;
		dfs(t);
		printf("%lld\n",ans);
	}
} 

猜你喜欢

转载自blog.csdn.net/Rvelamen/article/details/81481315