Poj 2559 最大矩形面积 v单调栈 分类: Brush Mode 2014-11-13 20:48 81人阅读 评论(0) 收藏

#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;
}
实在放心不下这个单调栈,还是把他存到自己博客里面吧
posted @ 2014-11-13 20:48  天I火  阅读(149)  评论(0编辑  收藏  举报