Programming thinking week5 homework A-maximum rectangle

topic

Given a histogram, find the area of ​​the largest rectangle in the histogram. For example, the height of the histogram in the picture below is 2, 1, 4, 5, 1, 3, 3 from left to right, and their width is 1, and the largest rectangle is the shadow.
Insert picture description here

Input

The input contains multiple sets of data. Each set of data uses an integer n to represent the number of small rectangles in the histogram. You can assume 1 <= n <= 100000. Then the next n integers h1,…, hn, satisfy 0 <= hi <= 1000000000. These The numbers indicate the height of each small rectangle from left to right in the histogram, and the width of each small rectangle is 1. The test data ends with 0.

Output

For each set of test data, an integer is output on one line to indicate the answer.

Sample Input

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

Sample Output

8
4000

Ideas

The monotonic stack is of linear complexity, where the monotonically increasing stack can find the first element larger than the current element to the left / right , and the monotonically decreasing stack can find the first element smaller than the current element to the left / right .
Traverse each small rectangle, take the height of the current small rectangle as the height, and find the largest rectangle that can be composed, that is, find the first small rectangle on its left and right that is smaller than its height.
Then traverse all the small rectangles twice, the first time from left to right, use the monotonically decreasing stack to record the small rectangle subscripts, and find the first small rectangle of each small rectangle i to the right that is shorter than it. After the traversal is completed, if there are still remaining in the stack, it means that the remaining small rectangle does not find a shorter one than it in the subsequent traversal process, that is, the corresponding largest rectangle can be extended to the far right, and assign r [i] Is n. For the second time, the array l [i] is obtained.
The monotonically decreasing stack ensures that the elements in the stack areTop to bottomforDiminishing, That is, when traversing to the i-th small rectangle from left to right, ifh[i]<h[s.top()], Meaning wearThe small rectangle with subscript s.top () encountered the first small rectangle i which is shorter than it, Then r [s.top ()] = i. The same goes from right to left.
Area S [i] = h [i] * (r [i] -l [I] -1).

Code

#include <cstdio>
#include <algorithm>
#include <stack>
using namespace std;

int height[100005],r[100005],l[100005];
stack<int> s;

int main() {
    int n;
    while(scanf("%d",&n)!=EOF&&n!=0){
        for(int i=0;i<n;i++)
            scanf("%d",&height[i]);
        for(int i=0;i<n;i++){
            while(s.size()>0&&height[s.top()]>height[i]){
                r[s.top()]=i;
                s.pop();
            }
            s.push(i);
        }
        while(s.size()>0){
            r[s.top()]=n;
            s.pop();
        }
        for(int i=n-1;i>=0;i--){
            while(s.size()>0&&height[s.top()]>height[i]){
                l[s.top()]=i;
                s.pop();
            }
            s.push(i);
        }
        while(s.size()>0){
            l[s.top()]=-1;
            s.pop();
        }
        long long max=0;
        for(int i=0;i<n;i++){
            long long tmp=(long long)height[i]*(r[i]-l[i]-1);
            if(max<tmp)
                max=tmp;
        }
        printf("%lld\n",max);
    }
}

to sum up

This question needs to pay attention to the data range. The height of the small rectangle hi <= 10 9 and the number of small rectangles <= 10 5 , the maximum area may be 10 14 , which is much larger than the int range, so long long is needed. The result of int type calculation is int type. When assigning int type to long long type, type conversion is required: long long tmp =(long long)height [i] * (r [i] -l [i] -1).
Title link

Published 24 original articles · praised 2 · visits 435

Guess you like

Origin blog.csdn.net/weixin_43805228/article/details/105169008