A- largest rectangle

topic:

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 Description

Input:

Comprising 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.

Output:

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

Sample Output:

8
4000

Topic analysis:

Require a maximum rectangular area in the histogram, according to a known area of ​​the rectangle at the height of the formula premise must require the width of the rectangle, and the rectangle in the histogram height [l, r] subject to the minimum height section, the rectangular width = l-r + 1. So the key question is how to determine the appropriate l, r as a range.

  • To make the entire largest rectangle, the height to a minimum it is determined as r in l.

  • Similarly, 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 endpoint may be determined as the first small number of points left height thereto.

  • It may be determined as the right end of this first small height point number right.

  • Monotone stack from the stack to the stack elements stack bottom monotone.

    • monotonically increasing stack can be found to the left / right the first element is larger than the current element.
    • monotonically decreasing stack can be found to the left / right the first element is smaller than the current element.
    Respectively monotonically decreasing monotonically increasing stack and the stack is obtained at each point is high around the endpoint.

wa many times, because the data type, title to height in the range 0 <= hi <= 1000000000, although not beyond the range of hi int range, but in the area in the process of obtaining and h (l-r + 1 ) multiplied burst int range, it will wa, it needs to be a strong operation in which the converted long long type, where I have the height and area converted to long long type. Trained long long correspondence lld Oh, do not forget (I just forgot the addition of a wa alas)

Code:

#include<iostream>
#include<stdio.h> 
#include<stack>
using namespace std;
const int maxn=1e5+100;
long long  L[maxn],R[maxn],w[maxn],a[maxn];
int n;
void solve()//找出各点右端点
{	
	stack<int> sa; 
	int l=1;int r=0;
	for(int i=1;i<=n;i++)
	{
		while(sa.size()>0 &&a[sa.top()]>a[i] )//单调递减栈找出右边第一个比当前元素小的元素
		{
			R[sa.top()]=i-1;
			sa.pop(); 
		} 
		sa.push(i);
	}
	while(sa.size()>0)//单调栈里的元素赋区间最右端点
	{
		R[sa.top()]=n;
		sa.pop();
	}
}
void solve1()//找出各点左端点
{	

	stack<int> sa; 
	int l=1;int r=0;
	//while(n--)
	for(int i=n;i>=1;i--)
	{
		while(sa.size()>0 &&a[sa.top()]>a[i] )//单调递减栈找出左边第一个比当前元素小的元素
		{
			L[sa.top()]=i+1;
			sa.pop(); 
		} 
		sa.push(i);
	}
	while(sa.size()>0)//单调栈里的元素赋区间最左端点
	{
		L[sa.top()]=1;
		sa.pop();
	}
}

int main()
{	
	//int n;//直方图小矩形个数 
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)
		{
			return 0;
		}	
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		solve(); 
		solve1();
		long long ans=0;
		for(int i=1;i<=n;i++)//计算面积
		{
			w[i]=a[i]*(R[i]-L[i]+1);
		}
		for(int i=1;i<=n;i++)//找出最大面积
		{
			if(w[i]>ans)
			{
				ans=w[i];
			}
		}
		printf("%lld\n",ans);
	} 
	return 0;	
} 
Published 21 original articles · won praise 0 · Views 1096

Guess you like

Origin blog.csdn.net/qq_43746837/article/details/105012659