zoukankan      html  css  js  c++  java
  • hdu 1506

    基本的单调栈
    作用:给出一排矩阵的高,让你从这些矩阵中划分出最大的矩阵
    时间复杂度接近线性了

    #include<stdio.h>
    #include<string.h>
    #define N 100005
    int q[N]={-1},w[N];//q数组是栈,w数组保存的是每个矩阵的左宽
    
    __int64 max(__int64 a,__int64 b)
    {
        return a>b?a:b;
    }
    
    int main()
    {
        int i,n,h;
        while(scanf("%d",&n)!=EOF&&n)
        {
            int top=0;
            __int64 ans=0;
            for(i=1;i<=n+1;i++)
            {
                if(i!=n+1)
                 scanf("%d",&h);
                else
                 h=0;
                if(h>q[top])//单调栈,保持底下的小,上面的大
                 q[++top]=h,w[top]=1;
                else
                {
                    __int64 cnt=0;
                    while(h<=q[top])
                    {
                        ans=max(ans,(cnt+w[top])*q[top]);//cnt相当于每个矩阵的右宽了
                        cnt=cnt+w[top--];
                    }
                    q[++top]=h;
                    w[top]=cnt+1;
                }
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    奔溃瞬间1
    面试知识点blog汇总
    贪心
    树 和 图
    DFS 和 BFS
    STL
    哈希表
    手写堆
    并查集
    二项式反演学习笔记
  • 原文地址:https://www.cnblogs.com/jiangjing/p/3295992.html
Copyright © 2011-2022 走看看