[ACM] maximum rectangle, [ACM] rectangle

Source: Internet
Author: User

[ACM] maximum rectangle, [ACM] rectangle
Maximum rectangle Problem Description

Question No.: 3
Question name: the largest rectangle
Time Limit: 1.0 s
Memory limit: 256.0 MB
Problem description:

Place n adjacent rectangles on the horizontal axis. The width of each rectangle is 1, and the height of the I (1 ≤ I ≤ n) rectangle is hi. The n rectangles form a histogram. For example, the height of the six rectangles is 3, 1, 6, 5, 2, and 3.

Find the rectangle with the largest area in the given histogram, and its side must be parallel to the coordinate axis. For the above example, the shadow area of the largest rectangle is 10.

Input Format

The first line contains an integer n, that is, the number of rectangles (1 ≤ n ≤ 1000 ).

The second row contains n integers h1, h2 ,... , Hn, adjacent numbers are separated by spaces. (1 ≤ hi ≤10000 ). Hi is the height of the I-th rectangle.

Output Format

The output line contains an integer, that is, the area of the largest rectangle in the given histogram.

Sample Input

6

3 1 6 5 2 3

Sample output

10

Algorithm Description

The largest rectangle in a bar chart has a smaller rectangle (the smallest number), so we can traverse all the smaller rectangles, each small rectangle is divided into two ways to find the largest rectangle that it can match, one forward, one backward, and both sides are looking for a rectangle higher than it (so it is the smallest rectangle), each finding one, even if the area of the rectangle appears, the area between the rectangle and the current maximum rectangle is used. If the area is larger than the current maximum rectangle, it is set to the current maximum rectangle area; otherwise, loop to the next small rectangle (that is, adding 1 to the X axis)

C ++ code
# Include <iostream> # include <string> # include <time. h> using namespace std; int a [1001]; int main () {int n, max = 0; cin> n; for (int I = 0; I <n; I ++) {cin> a [I];} max = a [0]; for (int I = 1; I <n; I ++) {int now = a [I]; // forward for (int j = I-1; j> = 0; j --) {if (a [I] <= a [j]) now + = a [I]; else break;} // go back to for (int j = I + 1; j <n; j ++) {if (a [I] <= a [j]) now + = a [I]; else break;} if (now> max) {max = now ;}} cout <max; getchar (); 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.