[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 ;}`