• Largest Rectangle in a Histogram


     题目传送门:http://poj.org/problem?id=2559

     

    Largest Rectangle in a Histogram
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 24256   Accepted: 7841

    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
    

    Hint

    Huge input, scanf is recommended.

    Source

     

     题解:

      单调栈计算每个矩形往左、往右所能够达到的最远距离。(入栈判断左边界,出栈判断右边界)

      PS:我用的是左开右闭区间。

      一个小技巧:栈中直接维护位置信息,这样就不用开两个栈或是结构体了。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<stack>
     7 #define LL long long
     8 #define RI register int
     9 using namespace std;
    10 const int INF = 0x7ffffff ;
    11 const int N = 100000 + 10 ;
    12 
    13 inline LL read() {
    14     int k = 0 , f = 1 ; char c = getchar() ;
    15     for( ; !isdigit(c) ; c = getchar())
    16       if(c == '-') f = -1 ;
    17     for( ; isdigit(c) ; c = getchar())
    18       k = k*10 + c-'0' ;
    19     return k*f ;
    20 }
    21 LL n ; LL hh[N], l[N] ;
    22 stack<LL>s ;  // 直接存位置信息 
    23 
    24 inline void solve() {
    25 //    while(s.size()) s.pop() ;
    26     LL ans = 0 ;
    27     for(int i=1;i<=n;i++) hh[i] = read() ;
    28     for(int i=1;i<=n;i++) {
    29         while(s.size() && hh[s.top()] >= hh[i]) {
    30             ans = max(ans,hh[s.top()]*(i-l[s.top()]-1)) ; s.pop() ;
    31         }
    32         if(s.size()) l[i] = s.top() ; else l[i] = 0 ;  // 初始化l数组的话这里就不用加else了
    33         s.push(i) ;  // printf("%d %d
    ",i,l[i]) ;
    34     }
    35     while(s.size()) ans = max(ans,(n-l[s.top()])*hh[s.top()]), s.pop() ;
    36     printf("%lld
    ",ans) ;
    37 }
    38 
    39 int main() {
    40     while(1) {
    41         n = read() ; if(!n) return 0 ;
    42         solve() ;
    43     }
    44 }
  • 相关阅读:
    系列文章--突袭HTML5之Javascript
    arguments对象的实例使用
    Javascript常用的设计模式详解
    javascript日历插件
    系列文章--AJAX技术系列总结
    零基础汇编学习笔记
    VC++6.0注释快捷键的添加使用
    VC6++常用快捷键
    汇编题目:编写包含多个功能子程序的中断例程
    汇编题目:按A键,当松开的时显示字母A
  • 原文地址:https://www.cnblogs.com/zub23333/p/8746504.html
Copyright © 2020-2023  润新知