zoukankan      html  css  js  c++  java
  • hdu 1506 Largest Rectangle in a Histogram(单调栈)

    题意:给出n个宽度为1 高度为hi的长方形

            问能圈出的最大长方形面积多大

    思路:http://blog.csdn.net/dgq8211/article/details/7740610

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<stack>
    using namespace std;
    #define ll __int64
    struct Node
    {
        int wid;
        ll hi;
        ll s;
    };
    Node node[100000+1000];
    int main()
    {
        int n,h;
        int i,j,k;
        int l,r;
        while(scanf("%d",&n),n)
        {
            stack<Node> q;
            ll maxx=0;
            Node now;
            now.hi=-1;
            now.wid=0;
            q.push(now);
            for(i=0;i<=n;i++)
            {
              //printf("%d
    ",i);
              if(i!=n) scanf("%d",&h);
              else h=0;
              if(h>q.top().hi)
              {
                now.hi=h;
                now.wid=1;
                q.push(now);
              }
              else
              {
                int cnt=0;
                while(q.top().hi>=h)
                {
                  now=q.top();
                  q.pop();
                  maxx=max(maxx,(cnt+now.wid)*now.hi);
                  cnt+=now.wid;
                }
                now.wid=cnt+1;
                now.hi=h;
                q.push(now);
              }
            }
    
            printf("%I64d
    ",maxx);
        }
        return 0;
    }
    

      

  • 相关阅读:
    滑雪
    2084 数塔HDU
    括号匹配(二)
    项链
    单调递增最长子序列
    矩形嵌套
    最长公共子序列
    poj3253
    表达式求值
    颜色16进制代码表显示和16进制数值对比显示方便查找
  • 原文地址:https://www.cnblogs.com/sola1994/p/4396024.html
Copyright © 2011-2022 走看看