HDOJ1506-Largest Rectangle in a Histogram

Largest Rectangle in a Histogram

 

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

 

 

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 34 1000 1000 1000 10000

 

 

Sample Output

 

84000

 

  一道动态规划题,然而因为某些原因TLE了无数次,所以特意写下来。。以供后人警戒,唉~~都是泪(╥╯^╰╥)

  下面说一下思路:

        首先,我们要求的是最大的矩形的面积,那么我们如何保证面积最大呢?当然是宽(一段连续的区间构成的矩形)和高(这段区间内的最小值)尽量大了。于是,我们就想到了用一个数组来模拟,数组下标为第i个矩形,数组的第i个数就代表着这个矩形的高了。那么问题又来了,我们如何取得尽量大的面积呢?看了题目上的图我们可以发现:如果我们确定这个矩形的高,即数组的值,然后来找矩形的宽,即对应的区间长度,是不是会简单一点呢?如下面代码所示,找到每个高和他对应的区间长(即从左边第一个高度小于他的高度到右边第一个高度小于他的高度的这段区间即是这个高所对应的矩形面积最大值),这里我们分别设置l和r两个数组来存储每一个高所对应的左右区间端点,最后在从左到右比较就行了。代码如下↓↓↓↓↓

#include <stdio.h>
#include <string.h>

long long h[100100];
long long l[100100]; //储存左边第一个高度比当前高度小的下标
long long r[100100]; //储存右边第一个高度比当前高度小的下标

int main() {
	long long n,i,j,Max;
	while( ~scanf( "%lld",&n ) && n!=0 ) {
		Max = 0;
		l[0] = 0;		//第一个元素对应的区间左端点当然是自己了 
		r[n-1] = n-1;	//相应的,最后一个元素对应的区间右端点也是自己 
		for( i=0 ; i<n ; i++ ) {
			scanf( "%lld",&h[i] );
		}
		//找到每个元素对应的区间左端点 
		for( i=1 ; i<n ; i++ ) {
			j = i;	//先设置为i 
			//如果他左边一个元素大于他,那么根据l数组的初衷,直接看比j-1小的第一个元素是不是也比j小 
			while( h[j-1]>=h[i] && j>0 )
				j = l[j-1];
			l[i] = j;
		}
		//找到每个元素对应的区间的右端点 
		for( i=n-2 ; i>=0 ; i-- ) {
			j = i;
			//和上面的l数组一样的思想哦 
			while( h[j+1]>=h[i] && j<n-1 )
				j = r[j+1];
			r[i] = j;
		}
		//遍历整个数组取得最大值然后输出 
		for( i=0 ; i<n ; i++ ) {
			if( Max < ( r[i]-l[i]+1 ) * h[i] )
				Max = ( r[i]-l[i]+1 ) * h[i];
		}
		printf( "%lld\n",Max );
	}
}

对了,,,再多说一点,在确定l和r的时候j是不能等于数组起点和终点的,,不然会TLE的,我就TLE了好多次哇/(ㄒoㄒ)/~~

对了,突然发现这个题用单调栈做好像,,更简单一点?双手奉上代码

#include <stdio.h>
#include <string.h>
#define N 100010
#define Max(x,y) x>y?x:y
typedef long long ll;

ll c[N]; //记录i之前有几个高度大于等于i的高度(即矩形的宽度) 
ll q[N]; //数组模拟单调栈 

int main(){
	int n,h;
	while( ~scanf( "%d",&n ) && n ) {
		int top = 0;
		ll ans = 0;
		memset( q,-1,sizeof(q) );
		memset( c,0,sizeof(c) );
		for( int i=0 ; i<=n ; i++ ){
			//当i<n时输入数据 
			if( i<n )
				scanf( "%d",&h );
			//设置最后一个高度为0,以使得模拟单调栈清空栈底 
			else
				h = 0;
			//如果当前h大于栈顶的高度,我们将h入栈,此时以h为高的矩形最大宽度为1 
			if( h>q[top] ){
				q[++top] = h;
				c[top] = 1;
			}else{
				//若当前h小于栈顶高度,则执行出栈操作直到栈顶元素小于h 
				ll cnt = 0;
				while( h<=q[top] ){
					//同时更新矩形的面积 
					ans = Max( ans,( cnt+c[top] )*q[top] );
					//更新矩形的宽 
					cnt += c[top--];
				}
				//将h入栈 
				q[++top] = h;
				//记录当前高为h时能得到的矩形最大的宽 
				c[top] = cnt+1;
			}
		}
		printf( "%lld\n",ans );
	}
}

OK~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值