poj2559单调栈

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

#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;
} 

 

posted @ 2013-06-23 21:11  紫忆  阅读(729)  评论(0编辑  收藏  举报