HDU 1506 largest rectangle in a Histogram

Source: Internet
Author: User
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 is not hard to understand. Strategy: record the current height of a [I] for each vertex. then there is the farthest seat between the left and right sides that is higher than the current height of a [I], that is, l [I], R [I];
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <limits. h> using namespace STD; const int maxn = 100000 + 100; long a [maxn], n; long l [maxn], R [maxn]; // R [I] records the right seat larger than the current vertex, and L [I] records the left seat larger than the current vertex. Int main () {While (~ Scanf ("% i64d", & N) {for (INT I = 1; I <= N; I ++) scanf ("% i64d ", & A [I]); L [1] = 1; R [N] = N; For (INT I = 2; I <= N; I ++) {int T = I; while (T> 1 & A [I] <= A [T-1]) t = L [T-1]; L [I] = T ;} for (INT I = n-1; I> = 1; I --) {int T = I; while (T <n & A [I] <= A [t + 1]) t = R [t + 1]; R [I] = T ;} long long ans =-1; for (INT I = 1; I <= N; I ++) {If (R [I]-l [I] + 1) * A [I]> ans) ans = (R [I]-l [I] + 1) * A [I];} 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.