SDU program design thinking Week5- largest rectangle job A-

Program design thinking Week5- jobs

A- largest rectangle

Description

To a histogram, find the largest rectangular area in the histogram. For example, the following image height in the histogram from left to right are 2, 1, 4, 5, 1, 3, 3, 1 they are wide, the largest rectangle is shaded.
Here Insert Picture DescriptionComprising a plurality of sets of input data. Each set of data is represented by an integer number n of small rectangular histogram, you can assume that 1 <= n <= 100000. then the next n integers h1, ..., hn, satisfying 0 <= hi <= 1000000000. These histogram from left to right the digital representation of each small rectangle of height, the width of each small rectangle is 1. Test data to 0 at the end.
For each line the test data output represents an integer answer.

Sample

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

output:
8
4000

Idea

For each small rectangle to limit its height, left and right extended to the maximum, large rectangle at this time is to get the little rectangle rectangular base can be expanded to a maximum out of such calculations for all the small rectangle Finally, taking the maximum you can get the answer.
Monotone stack used here thought, with an array of monotonically s behalf of the stack, the stack is incremented from bottom to top of the stack.
First, the first rectangle from the histogram based on a first search starts right height less than its point i.e. its right end points, if the rectangle is greater than the top element, the top element put out and tag out this element after the right border, until the top of the stack is not greater than the rectangle, this rectangle placed on the stack, so that throughout the process, out of all the right boundary will be marked rectangle is then added to the stack, and after the end of the cycle, still rectangular stack shows that they have not less than the right side of their rectangles that right boundary is n.
Left looking the same way, find the first element of each rectangle less than.
Found around the boundary of each rectangle can be regarded as the largest rectangle.

Summary

If it is determined the height of the rectangle, then the left end point must be more to the left, to the right the right point, the only possible maximum area of a rectangle
left point left to be determined is the number of the first small height of this point, the right end point can be determined this is a small number of right first point height
twice increasing monotonically from the bottom of the stack to the stack top to the stack processing for the high point of each of the left and right end points.

Here we can use the array instead of stack elements into a convenient out, only need to change its stack top index
Note: 0 <= hi <= 1000000000 so the result use long long storage, otherwise there might be exceeded.

Codes

#include <iostream>

using namespace std;
int n;
int a[100010],L[100010],R[100010],s[100010];

int main()
{
	while (true) {
		cin >> n;
		if (n == 0)break;
		for (int i = 0; i < n; i++)
			cin >> a[i];
		long long ans = 0;
		int  top = -1;		//栈顶
		for (int i = 0; i < n; i++) {		//搜索每个元素往右第一个小于它的元素
			while (top>=0 && a[i]<a[s[top]]) {
				R[s[top]] = i;
				top--;
			}
			s[++top] = i;
		}
		while (top >=0)
			R[s[top--]] = n;
		top = -1;
		for (int i = n - 1; i >= 0; i--) {		//搜索每个元素往左第一个小于它的元素
			while (top >=0 && a[i] < a[s[top]]) {
				L[s[top]] = i;
				top--;
			}
			s[++top] = i;

		}
		while (top >= 0)
			L[s[top--]] = -1;

		for (int i = 0; i < n; i++)
			if (ans < (long long)a[i] * (R[i] - L[i] - 1))ans = (long long)a[i] * (R[i] - L[i] - 1);

		
		cout << ans << endl;
	}
}


Published 21 original articles · won praise 5 · Views 782

Guess you like

Origin blog.csdn.net/weixin_44578615/article/details/104977224