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

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:

    Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.
    Input
    The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1<=n<=100000. Then follow n integers h1,...,hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.
    Output
    For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.
    Sample Input
    7 2 1 4 5 1 3 3
    4 1000 1000 1000 1000
    0
    
    Sample Output
    8
    4000
    
    Hint
    Huge input, scanf is recommended.
     
    题意:柱状图是由一些宽度相等的长方形下端对齐后横向排列得到的图形。现在有n个宽度为1,高度分别为h1,h2...,hn的长方形从左到右依次排列组成的柱状图。问里面包含的长方形的最大面积是多少.
     
    分析:
    可以枚举每一个长方形的高度,作为组合后的长方形的高度。。
    那么我们需要找到这个长方形,然后求出能够向左右两边延伸的长度。
    找左右边界的时候,需要用到单调栈
    代码如下:
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <stack>
    using namespace std;
    typedef long long LL;
    const int MAXN=1e5+100;
    LL ll [MAXN];
    LL rr[MAXN];
    LL a[MAXN];
    LL n;
    LL maxx;
    int main()
    {
      int k;
        while(scanf("%lld",&n)&&n)
        {
         maxx=0;
         stack<int>S;
         for(int i=1;i<=n;i++)
         scanf("%lld",&a[i]);
    
         for(int i=1;i<=n;i++)
         {
             if(S.empty())
            {
               ll[i]=1;
               S.push(i);
            }
            else
            {
    
                k=S.top();
               while(a[k]>=a[i])
               {
                  rr[k]=i;
                  S.pop();
                  if(S.empty())
                  break;
                  k=S.top();
               }
                if(S.empty())
                ll[i]=1;
                else
                ll[i]=k+1;
                S.push(i);
            }
         }
    
             while(!S.empty())
             {
               k=S.top();
               rr[k]=n+1;
               S.pop();
             }
           for(int i=1;i<=n;i++)
            maxx=max(maxx,a[i]*(rr[i]-ll[i]));
           printf("%lld
    ",maxx);
        }
        return 0;
    }
     
  • 相关阅读:
    DLL相关注意
    给我个理由要用虚析构函数
    字符指针常量和字符数组
    给自己普及网络基本知识
    面向对象三大基本特性,五大基本原则
    C/C++内存区域划分
    C++多态性
    Java XML Schema详解
    Java SE 第一百零五讲 对象的克隆与浅拷贝
    Java SE 第九十七~一百零五讲 线程与进程
  • 原文地址:https://www.cnblogs.com/a249189046/p/8723281.html
Copyright © 2011-2022 走看看