HDU 1056 Largest Rectangle in a Histogram (dp) (find the Largest rectangular area), hduhistogram

Source: Internet
Author: User

HDU 1056 Largest Rectangle in a Histogram (dp) (find the Largest rectangular area), hduhistogram

Problem DescriptionA 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.
InputThe 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. 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.
OutputFor 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
 
SourceUniversity of Ulm Local Contest 2003
Recommend


Idea: Take the height of each row as high, look at the places where the left and right can be extended separately, and calculate the area for Optimization



Code:



#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;#define N 100005__int64 h[N],le[N],ri[N];int main(){    __int64 n,i;    while(scanf("%I64d",&n),n)    {        for(i=1;i<=n;i++)        {            scanf("%I64d",&h[i]);            le[i]=ri[i]=i;        }        for(i=1;i<=n;i++)            while(le[i]>1&&h[le[i]-1]>=h[i])               le[i]=le[le[i]-1];        for(i=n;i>=1;i--)            while(ri[i]<n&&h[ri[i]+1]>=h[i])            ri[i]=ri[ri[i]+1];        __int64 ans=0;        for(i=1;i<=n;i++)        {            __int64 temp=((ri[i]-le[i]+1)*h[i]);            if(temp>ans)                ans=temp;        }        printf("%I64d\n",ans);    }    return 0;}






Hangzhou 1506, Largest Rectangle in a Histogram.

# Include <stdio. h >__ int64 h [100001], dp [100001], m1 [100001], m2 [100001]; // The array is too large to be placed out, others are correct _ int64 n, s, m, I, j, k; int main () {while (scanf ("% I64d", & n )! = EOF & n! = 0) {for (I = 1; I <= n; I ++) scanf ("% I64d", & h [I]); m1 [1] = 1; for (I = 1; I <= n; I ++) {k = I; while (k> 1) {if (h [k-1]> = h [I]) {m1 [I] = m1 [k-1]; k = m1 [k-1];} else {m1 [I] = k; break ;}} m2 [n] = n; for (I = n; I> = 1; I --) {k = I; while (k <n) {if (h [k + 1]> = h [I]) {m2 [I] = m2 [k + 1]; k = m2 [k + 1];} else {m2 [I] = k; break ;}}for (I = 1; I <= n; I ++) dp [I] = (m2 [I]-m1 [I] + 1) * h [I]; m = 0; for (I = 1; I <= n; I ++) if (dp [I]> m) m = dp [I]; printf ("% I64d \ n", m) ;}return 0 ;} after reading it for a long time, I helped you verify it, but the result was not executed. Then I thought it might be the reason for the array. So pay attention to this problem later. I hope it will help you.



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.