zoukankan      html  css  js  c++  java
  • poj 2559 (单调栈)

    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

    具体操作:

    建立一个单调递增栈,所有元素各进栈和出栈一次即可。每个元素出栈的时候更新最大的矩形面积。

    设栈内的元素为一个二元组(x, y),x表示矩形的高度,y表示矩形的宽度。

    如何压栈并更新呢?

    ① 如果当前元素比栈顶元素大或者栈为空,则直接压栈(x,1);

    ② 如果当前元素小于等于栈顶元素,则退栈,直到 当前元素大于栈顶元素或者栈为空时,压栈。

    ③在退栈的过程中要进行最大面积和累积宽度的更新:

    例:

    若原始矩形高度分别为2,1,4,5,1,3,3

    高度为2的元素进栈,当前栈为(2,1)

    高度为1的元素准备进栈,但必须从栈顶开始删除高度大于或等于1的矩形,因为2已经不可能延续到当前矩形。删除(2,1)这个元素之后,更新最大矩形面积为2*1=2,然后把它的宽度1累加到当前高度为1的准备进栈的矩形,然后进栈,当前栈为(1,2)

    高度为4的元素进栈,当前栈为(1,2) (4,1)

    高度为5的元素进栈,当前栈为(1,2) (4,1) (5,1)

    高度为1的元素准备进栈,删除(5,1)这个元素,更新最大矩形面积为5*1=5,把1累加到下一个元素,得到(4,2),删除(4,2),更新最大矩形面积为4*2=8,把2累加到下一个元素,得到(1,4),1*4=4<8,不必更新,删除(1,4),把4累加到当前准备进栈的元素然后进栈,当前栈为(1,5)

    高度为3的元素进栈,当前栈为(1,5) (3,1)

    高度为3的元素准备进栈,删除(3,1),不必更新,把1累加到当前准备进栈的元素然后进栈,当前栈为(1,5) (3,2)

    把余下的元素逐个出栈,(3,2)出栈,不必更新,把2累加到下一个元素,当前栈为(1,7),(1,7)出栈,不必更新。栈空,结束。

    最后的答案就是8。

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <stdbool.h>
     5 #include <stdlib.h>
     6 #include <string>
     7 #include <string.h>
     8 #include <math.h>
     9 #include <vector>
    10 #include <queue>
    11 #include <stack>
    12 #include <set>
    13 #include <map>
    14 
    15 #define INF 0x3f3f3f3f
    16 #define LL long long
    17 #define MAXN 2000005
    18 using namespace std;
    19 
    20 struct Node{
    21     LL val;
    22     LL len;
    23 };
    24 
    25 stack<Node> stc;
    26 Node a;
    27 LL n;
    28 
    29 int main()
    30 {
    31     //freopen("../in.txt","r",stdin);
    32     while (~scanf("%lld",&n)) {
    33         if (n == 0)
    34             break;
    35         LL Max = 0;
    36         LL m = 0;
    37         LL temp = 0;
    38         for (int i=1;i<=n;i++) {
    39             scanf("%lld", &a.val);
    40             a.len = 1;
    41             temp = 0;
    42             if (stc.empty())
    43                 stc.push(a);
    44             else if (a.val <= stc.top().val)
    45             {
    46                 while (!stc.empty() && a.val <= stc.top().val) {  //小的要注意更新len
    47                     stc.top().len += temp;
    48                     m = (stc.top().val) * stc.top().len;
    49                     if (m > Max)
    50                         Max = m;
    51                     temp = stc.top().len;
    52                     stc.pop();
    53                 }
    54                 a.len += temp;
    55                 stc.push(a);
    56             }
    57             else   //大的直接入栈
    58                 stc.push(a);
    59         }
    60         temp = 0;
    61         while (!stc.empty())
    62         {
    63             stc.top().len += temp;
    64             m = (stc.top().val) * stc.top().len;
    65             if (m > Max)
    66                 Max = m;
    67             temp = stc.top().len;
    68             stc.pop();
    69         }
    70         printf("%lld
    ",Max);
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    pyqt5 动画在QThread线程中无法运行问题
    几种编码格式
    Android精品课程—PullToRefresh 下拉刷新
    【张泽华】android视频教程下载地址及上课源代码
    Android中常用适配器及定义自己的适配器
    VirtualBOX 虚拟机安装 OS X 10.9 Mavericks 及 Xcode 5,本人X220亲测
    google maps api申请的问题
    如何在Android应用程序中使用传感器(OpenIntents开源组织SensorSimulator项目)
    cvsnt 设置用户、修改密码
    Windows下用Git下载android源码 转载
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11277038.html
Copyright © 2011-2022 走看看