E-largest Rectangle in a histogramTime
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i6 4u Submit Status Practice HDU 1506Appoint Description:
Description
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles has equal widths but could 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 is used to represent discrete distributions, e.g., the frequencies of characters in texts. Note the order of the rectangles, i.e, their heights, is important. Calculate the area of the largest rectangle in a histogram that's aligned at the common base line, too. The 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. Assume that 1 <= n <= 100000. Then follow n integers h1, ..., HN, where 0 <= hi <= 1000000000. These numbers denote the heights of the rectangles in histogram order. The width of each rectangle is 1. A Zero follows the input for the last test case.
Output
The For all test case is output on a single line, the area of the largest rectangle in the specified histogram. Remember that this rectangle must is aligned at the common base line.
Sample Input
Sample Output
84000 The test instructions of the subject is to give a number of rectangles, height is not necessarily, ask you to find out in the range of the entire graph of the largest area is how much · because the height of the high, so with int will problem, it is best to change to a long long can be passed the following code
#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespacestd;Const intmaxn=100005; typedefLong Long intLl;ll L[MAXN],R[MAXN],H[MAXN];intMain () {intN; while(SCANF ("%d", &n)! =EOF) { if(!N) Break; memset (L,0,sizeof(l)); Memset (R,0,sizeof(R)); memset (H,0,sizeof(h)); //memset (Dp,0,sizeof (DP)); for(intI=1; i<=n;i++) {scanf ("%lld",&H[i]); } l[1]=1; R[n]=N; for(intI=2; i<=n;i++){ inttmp=i; while(tmp>1&&h[i]<=h[tmp-1]) TMP=l[tmp-1]; L[i]=tmp; } for(inti=n-1; i>=1; i--){ inttmp=i; while(tmp<n&&h[i]<=h[tmp+1]) TMP=r[tmp+1]; R[i]=tmp; } ll ans=-1; for(intI=1; i<=n;i++) {ll tmp= (r[i]-l[i]+1)*H[i]; Ans=Max (ans,tmp); } printf ("%lld\n", ans); } return 0;}
HDU 1506 largest Rectangle in a histogram (DP left and right processing boundary rectangle problem)