zoukankan      html  css  js  c++  java
  • poj 2559 Largest Rectangle in a Histogram--单调栈

    最近一直在学单调栈,这是一种十分神奇的算法。

    看看这道题

    poj 2559 Largest Rectangle in a Histogram

    Description

    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

    有N个矩形,宽度都为1,给出N个矩形的高度,求由这N个矩形组成的图形包含的最大的矩形面积。

    我们可以维护一个不下降的单调栈,如果新元素的值大于或等于栈顶元素,那么就直接压入栈,否则就不断取出栈顶元素,计算出由矩形的面积更新答案并不断更新矩形的宽度,每次弹出一个矩形就用它的高度乘以它的宽度来更新答案,重要的一点是,为了保证所有的元素都可以弹出栈来更新答案,所以我们要在栈的最后添加一个极小值,以免有矩形剩余。这样我们经过一次扫描就可以在o(n)的时间内求出问题的答案了。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string>
     4 #include<cmath>
     5 #include<cstring>
     6 #include<queue>
     7 #include<stack>
     8 #include<algorithm>
     9 #define maxn 100005
    10 using namespace std;
    11 
    12 stack<int>s;
    13 
    14 inline int read()
    15 {
    16     char c=getchar();
    17     int res=0,x=1;
    18     while(c<'0'||c>'9')
    19     {
    20         if(c=='-')
    21         x=-1;
    22         c=getchar();
    23     }
    24     while(c>='0'&&c<='9')
    25     {
    26         res=res*10+(c-'0');
    27         c=getchar();
    28     }
    29     return x*res;
    30 }
    31 
    32 long long ans;
    33 int n,aa,p,top;
    34 int a[maxn],w[maxn];
    35 
    36 int main()
    37 {
    38     while(1)
    39     {
    40         n=read();
    41         if(n==0) return 0;
    42         memset(a,0,sizeof(a));
    43         memset(w,0,sizeof(w));
    44         ans=0;
    45         for(int i=1;i<=n;i++)
    46         {
    47             aa=read();
    48             a[i]=aa;
    49         }
    50         a[n+1]=0;
    51         for(int i=1;i<=n+1;i++)
    52         {
    53             if(s.empty()||a[i]>s.top())
    54             {
    55                 s.push(a[i]);
    56                 w[s.size()]=1;//宽度为1. 
    57             }
    58             else
    59             {
    60                 int mid=0;//统计矩形宽度 
    61                 while(!s.empty()&&s.top()>a[i])
    62                 {
    63                     mid+=w[s.size()];//更新矩形的宽度。 
    64                     ans=max(ans,(long long)s.top()*mid);//用矩形的面积来更新答案。 
    65                     s.pop();
    66                 }
    67                 s.push(a[i]);
    68                 w[s.size()]=mid+1;//把之前的矩形宽度累加,因为之前的矩形多余的高度已经没用了。 
    69             }
    70         }
    71         printf("%lld
    ",ans);
    72     }
    73     return 0;
    74 }
    View Code

    Live a noble and honest life. Reviving past times in your old age will help you to enjoy your life again.
    过一种高尚而诚实的生活。当你年老时回想起过去,你就能再一次享受人生。

                                                                                                                 --snowy

                                                                                                     2019-01-18  07:52:11

  • 相关阅读:
    bzoj1053(反素数)
    poj1442(对顶堆)
    poj2823(单调队列)
    poj3630(简单tire)
    poj1924(单调栈求最大矩阵)
    最大xor路径(poj3764)
    poj2689
    求n!末尾0的个数
    BigInteger和BigDecimal的基本用法
    大数乘法
  • 原文地址:https://www.cnblogs.com/snowy2002/p/10284614.html
Copyright © 2011-2022 走看看