码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1506 Largest Rectangle in a Histogram(单调栈)

时间:2015-04-06 16:59:57      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

题意:给出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\n",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\n",maxx);
    }
    return 0;
}

  

hdu 1506 Largest Rectangle in a Histogram(单调栈)

标签:

原文地址:http://www.cnblogs.com/sola1994/p/4396024.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!