HDU - 1506 Largest Rectangle in a Histogram

https://vjudge.net/problem/HDU-1506
https://www.acwing.com/problem/content/133/

#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
int h[N],q[N],l[N],r[N];
typedef long long LL;
int main()
{
	int n;
	while(cin>>n,n)
	{
		for(int i=1;i<=n;i++) cin>>h[i];
		h[0]=h[n+1]=-1;
		
		int t=0;
		q[0]=0;
		for(int i=1;i<=n;i++) //从左维护一个升序单调栈 
		{
			while(h[q[t]]>=h[i]) t--;
			l[i]=q[t]; //记录位置 
			q[++t]=i; 
		}
		
		t=0;
		q[0]=n+1;
		for(int i=n;i>=1;i--)
		{
			while(h[q[t]]>=h[i]) t--;
			r[i]=q[t];
			q[++t]=i;
		}
		LL res=0;
		for(int i=1;i<=n;i++)
			res=max(res,(LL)h[i]*(r[i]-l[i]-1));
		cout<<res<<"\n";
	}
	return 0;
}
posted @ 2021-08-22 21:35  斯文~  阅读(11)  评论(0)    收藏  举报

你好!