HDU 1506 largest Rectangle in a histogram (DP left and right processing boundary rectangle problem)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.