【ACM】【单调栈】Largest Rectangle in a Histogram

版权声明:转载请注明出处~~~ https://blog.csdn.net/lesileqin/article/details/88956477

Largest Rectangle in a Histogram

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28968   Accepted: 9383

点我访问题目

Description

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:


Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.

Input

The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1<=n<=100000. Then follow n integers h1,...,hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.

Output

For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.

Sample Input

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

Sample Output

8
4000

Hint

Huge input, scanf is recommended.

Source

Ulm Local 2003

方法一:暴力搜索、枚举每个矩形,然后向左向右遍历,遇到比该矩形高度大的就更新面积,遇到小的就跳出循环。但是这种方法的时间复杂度较高,相当于O(n^2)。放在OJ上会超时。

#include<iostream>
#include <stack>
#define m 100000+2
using namespace std;

int main()
{

	int n;
	while(cin >> n&&n)
	{
		int maxn=-1;
		int a[m]={0};
		for(int i=0;i<n;i++)
			cin >>a[i];
		for(int i=0;i<n;i++)
		{
			int s=a[i];
			int right=i-1,left=i+1;
			//向右
			for(;right>=0;right--)
			{
				if(a[right]>=a[i])
					s+=a[i];
				else 
					break;
			}
			//向左 
			for(;left<n;left++)
			{
				if(a[left]>=a[i])
					s+=a[i];
				else
					break;
			}
			if(s>maxn)
				maxn=s;
		}
		cout << maxn << endl;
	}
	
    return 0;
}

方法二: 利用栈来解决。先定义pair组存储矩形的高度和宽度。输入的时候比较输入值与栈顶组的高度。

  • 若输入值比栈顶小,计算栈顶矩形的面积,更新最大面积值,然后保存栈顶宽度值,加到下一个组的宽度、最后弹出栈顶元素、压入新值。
  • 若输入值比栈顶大,直接压入栈。

(注意:Huge input, scanf is recommended.需要用long long存值,否则数据会溢出)。

#include<iostream>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> p;	

int main()
{
	stack<p> s;
	int n;
	while(cin >> n && n)
	{
		ll maxn=-1,h;
		while(!s.empty())
			s.pop();
		for(int i=0;i<n;i++)
		{
			cin >> h;
			ll w=0;
			while(!s.empty()&&s.top().first>=h)
			{
				ll th=s.top().first;
				ll tw=s.top().second;
			//	cout << s.top().first << " " << s.top().second <<endl;
				w=w+tw;
				s.pop();
				maxn=max(maxn,w*th);
			}
			s.push(make_pair(h,w+1));
		}
		int temp=0;
		while(!s.empty())
		{
			maxn=max(maxn,s.top().first*(temp+s.top().second));
			temp+=s.top().second;
			s.pop();
		}
		cout << maxn <<endl;
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lesileqin/article/details/88956477
今日推荐