HDU 1506 largest rectangle in a histogram matrix series (1)

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): 7496 accepted submission (s): 2106 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
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 & lt; = Hi & lt; = 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

Question meaning:

Give you n, and then tell you the height of the N segment, thus forming a Histogram

What is the area of the largest rectangle in your histogram?

Solution: for every height H [I], iteration to find its leftmost and rightmost for H [I], if it is big or equal to the left H [I-1], then his leftmost must be the leftmost of the I-1, and then the iteration continues PS: the iteration process is very fast
# Include <iostream> # include <cstdio> # define maxn 100005 using namespace STD; int n, m ;__ int64 H [maxn], le [maxn], RI [maxn]; __int64 ans; void left () // iteration to the left {int I, j; for (I = 1; I <= N; I ++) {le [I] = 1; while (H [I-le [I]> = H [I]) {le [I] + = Le [I-le [I] ;}} void right () // right iteration {int I, j; for (I = N; i> = 1; I --) {Ri [I] = 1; while (H [I + Ri [I]> = H [I]) {Ri [I] + = Ri [I + Ri [I] ;}} void solve () {int I; ans =-1; for (I = 1; I <= N; I ++) // update ans {If (le [I] + Ri [I]-1) * H [I]> ans) ans = (le [I] + Ri [I]-1) * H [I] ;}} int main () {int I, J; while (scanf ("% d", & N), n) {for (I = 1; I <= N; I ++) {scanf ("% i64d ", & H [I]);} H [0] = H [n + 1] =-1; // Add the boundary condition left (); Right (); solve (); 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.