题意:给出连续的矩形的高....求最大面积

#include<iostream>
#include<stack>
#include<stdio.h>
using namespace std;
struct node
{
	__int64 num,pre,next;
};
int main()
{
	int n;
	while(scanf("%d",&n)>0&&n)
	{
		stack<node>Q;
		node tmp;
		__int64 ans=0,sum=0,num;
		scanf("%I64d",&tmp.num);
		tmp.pre=1;
		tmp.next=1;
		Q.push(tmp);
		for(int i=1;i<n;i++)
		{
			scanf("%I64d",&tmp.num);
			tmp.pre=tmp.next=1;
			while(!Q.empty()&&tmp.num<=Q.top().num)
			{
				node tmp1=Q.top();
				Q.pop();
				ans=tmp1.num*(tmp1.pre+tmp1.next-1);
				if(!Q.empty())
				Q.top().next+=tmp1.next;
				tmp.pre+=tmp1.pre;
				if(ans>sum)
				sum=ans;
			}
			Q.push(tmp);
		}
		while(!Q.empty())
		{
			node tmp1=Q.top();
			Q.pop();
			if(!Q.empty())
			Q.top().next+=tmp1.next;
			ans=tmp1.num*(tmp1.pre+tmp1.next-1);
			if(ans>sum)
			sum=ans;
		}
		printf("%I64d\n",sum);
	}
	return 0;
} 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
猜你喜欢
  • 2021-11-29
  • 2021-10-21
  • 2022-12-23
  • 2022-12-23
  • 2021-08-20
  • 2021-08-07
相关资源
相似解决方案