其他分享
首页 > 其他分享> > POJ2559【Largest Rectangle in a Histogram】

POJ2559【Largest Rectangle in a Histogram】

作者:互联网

Largest Rectangle in a Histogram

题目

POJ2559


解析

单调栈
每次出栈时计算矩形的面积,求最大值即可

code:

#include<cstdio>
#define int long long
using namespace std;
inline bool idigit(char x){return (x<'0'|x>'9')?0:1;}
inline int max(int x,int y){return x>y?x:y;}
inline int min(int x,int y){return x<y?x:y;}//卡常行为
inline int read()
{
	int num=0,f=1;
	char c=0;
	while(!idigit(c=getchar())){if(c=='-')f=-1;}
	while(idigit(c))num=(num<<1)+(num<<3)+(c&15),c=getchar();
	return num*f;
}
inline void write(int x)
{
	int F[20];
	int tmp=x>0?x:-x;
	if(x<0)putchar('-');
	int cnt=0;
	while(tmp>0){F[cnt++]=tmp%10+'0';tmp/=10;}
	while(cnt>0)putchar(F[--cnt]);
	if(x==0)putchar('0');
	putchar('\n');
}//快读快输
int n,a[100010],tot,ans,r;
struct q
{
	int l,r;
}st[100010];//建立矩形类,为了方便手写栈
signed main()
{
	while(n=read())
	{
		ans=tot=0;
		for(int i=1;i<=n;++i)
		{
			a[i]=read();
			r=0;
			while(tot&&st[tot].r>=a[i])
			{
				r+=st[tot].l;
				ans=max(ans,r*st[tot].r);//求矩形面积
				--tot;
			}
			st[++tot].l=r+1,st[tot].r=a[i];
		}
		r=0;
		while(tot)
		{
			r+=st[tot].l;
			ans=max(ans,r*st[tot].r);
			--tot;
		}//彻底清空
		write(ans);
	}
	return 0;
}

标签:return,int,tot,st,POJ2559,while,ans,Histogram,Rectangle
来源: https://blog.csdn.net/zhanglili1597895/article/details/114433470