HDU 1506 largest rectangle in a histogram (DP)

Source: Internet
Author: User

Largest rectangle in a Histogram Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 11137 accepted submission (s): 3047


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
 

Input the area of the largest rectangle in the bar chart to the number of entries. The height of each entry is hi (the following values are also regarded as high)

As long as you know the number of consecutive (a) higher than the number of consecutive (B) the area with this bar as the maximum height is hi * (A + B + 1 );

However, if you enumerate each code, the code will definitely time out.

#include<cstdio>using namespace std;const int N = 100005;typedef long long ll;ll h[N]; int n,wide[N];int main(){    while (scanf ("%d", &n), n)    {        for (int i = 1; i <= n; ++i)            scanf ("%I64d", &h[i]);        for (int i = 1; i <= n; ++i)        {            wide[i] = 1;            int k = i;            while (k > 1 && h[--k] >= h[i]) ++wide[i];            k = i;            while (k < n && h[++k] >= h[i]) ++wide[i];        }        ll ans = 0;        for (int i = 1; i <= n; ++i)            if (h[i]*wide[i] > ans) ans = h[i] * wide[i];        printf ("%I64d\n", ans);    }    return 0;}

It can be found that when the first I-1 is higher than the first I-1 all must be higher than the first I

So we can use the idea of dynamic planning.

Left [I] indicates the leftmost serial number in a continuous sequence that is higher than I, including I. Right [I] indicates the rightmost serial number.

Then, when H [left [I]-1]> = H [I], left [I] = left [left [I]-1] can be handed over to left. [I]

Similarly, when H [right [I] + 1]> = H [I], right [I] = right [right [I] + 1] can be pushed forward from the back to the right. [I]

The final answer is equal to max (right [I]-left [I] + 1) * H [I ]).

#include<cstdio>using namespace std;const int N = 100005;typedef long long ll;ll h[N];int n, left[N], right[N];int main(){    while (scanf ("%d", &n), n)    {        for (int i = 1; i <= n; ++i)            scanf ("%I64d", &h[i]), left[i] = right[i] = i;        h[0] = h[n + 1] = -1;        for (int i = 1; i <= n; ++i)            while (h[left[i] - 1] >= h[i])                left[i] = left[left[i] - 1];        for (int i = n; i >= 1; --i)            while (h[right[i] + 1] >= h[i])                right[i] = right[right[i] + 1];        ll ans = 0;        for (int i = 1; i <= n; ++i)            if (h[i] * (right[i] - left[i] + 1) > ans) ans = h[i] * ll (right[i] - left[i] + 1);        printf ("%I64d\n", ans);    }    return 0;}



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.