您的位置:首页 > 其它

Poj 2559 最大矩形面积 v单调栈

2014-11-13 20:48 453 查看
#include<iostream>
#include<stack>
#include<stdio.h>
using namespace std;
struct node
{
__int64 num,pre,next;
};
int main()
{
int n;
freopen("in.txt","r",stdin);
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;
}
实在放心不下这个单调栈,还是把他存到自己博客里面吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: