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, where0<=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.

    单调栈的最入门题目。O(n)求一下l,r两个数组即可。

     1 #include<cstdio>
     2 #include <iostream>
     3 #include <vector>
     4 #include <algorithm>
     5 #include <stack>
     6 typedef long long ll;
     7 typedef unsigned long long ull;
     8 #define pii pair <int, int>
     9 #define pll pair <ll, ll>
    10 #define mp make_pair
    11 using namespace std;
    12 const int MAX=1e5+5;
    13 int n;
    14 int a[MAX],l[MAX],r[MAX];
    15 stack <int> mono;
    16 ll an;
    17 int main()
    18 {
    19     while(scanf("%d",&n)&&n)
    20     {
    21         an=0;
    22         for(int i=1;i<=n;i++)
    23             scanf("%d",&a[i]);
    24         while(!mono.empty())
    25             mono.pop();
    26         for(int i=1;i<=n;i++)
    27         {
    28             while(!mono.empty()&&a[mono.top()]>=a[i])
    29                 mono.pop();
    30             if(mono.empty())
    31                 l[i]=0;
    32             else
    33                 l[i]=mono.top();
    34             mono.push(i);
    35         }
    36         while(!mono.empty())
    37             mono.pop();
    38         for(int i=n;i>=1;i--)
    39         {
    40             while(!mono.empty()&&a[mono.top()]>=a[i])
    41                 mono.pop();
    42             if(mono.empty())
    43                 r[i]=n+1;
    44             else
    45                 r[i]=mono.top();
    46             mono.push(i);
    47         }
    48         for(int i=1;i<=n;i++)
    49             an=max(an,(ll)((ll)r[i]-(ll)l[i]-1LL)*(ll)a[i]);
    50         printf("%lld
    ",an);
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    java基础学习笔记四(异常)
    关于linux下crontab mysql备份出来的数据为0字节的问题
    转:国内优秀npm镜像推荐及使用
    webpack使用总结~
    php下载远程文件方法~
    腾讯开放平台web第三方登录获取信息类(包含签名)
    windows 平台 php_Imagick 拓展遇到的那些坑!
    转:CentOS/Debian/Ubuntu一键安装LAMP(Apache/MySQL/PHP)环境
    composer 报错:Your requirements could not be resolved to an installable set of packages 解决方法
    Javascript模块化编程(三):require.js的用法
  • 原文地址:https://www.cnblogs.com/quintessence/p/6618848.html
Copyright © 2011-2022 走看看