POJ 2559/hdu 1506 largest Rectangle in a histogram (DP)

Source: Internet
Author: User
Tags time limit

Largest Rectangle in a histogram

http://poj.org/problem?id=2559

Time limit:1000ms

Memory limit:65536k

Description

A histogram is a polygon composed of a sequence of rectangles aligned in a common base line. The rectangles have equal widths but may have different. For example, the "figure on" the left shows the histogram, 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., the e.g of frequencies in characters. Note This order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram this is aligned on the common base line, too. The figure on the right 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 compos Ed of. You are assume that 1<=n<=100000. Then follow n integersh1,..., hn, where 0<=hi<=1000000000 . These numbers denote the heights's 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.

Output

For each test case output on a single line the ' largest rectangle in the specified histogram. Remember that this rectangle must is aligned at the common base line.

Sample Input

7 2 1 4 5 1 3 3 4
1000 1000 1000 1000
0

Sample Output

8
4000

Ideas:

Use 2 array r[i],l[i] to represent the left and right position of the largest rectangle with a h[i] height.

Attention:

Use the previously calculated r,l to speed up R[i],l[i's calculations (otherwise tle).

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.