zoukankan      html  css  js  c++  java
  • 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
    ",sum);
        }
        return 0;
    }
    
    实在放心不下这个单调栈,还是把他存到自己博客里面吧
  • 相关阅读:
    泰勒综合
    滤波器、窗等的系数为什么是对称的?
    l'alphabet en francais
    弄清for循环的本质
    js中的闭包
    js中用正则表达式
    java Calendar
    Android实现XML解析技术
    junit4 详解
    redhat vi 命令
  • 原文地址:https://www.cnblogs.com/you-well-day-fine/p/4671611.html
Copyright © 2011-2022 走看看