zoukankan      html  css  js  c++  java
  • poj2559 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 that1<=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

    Ulm Local 2003
     
     
    题目大意:

    这题就是个单调栈。贴一下书上的题解吧:

    代码:

     1 program rrr(input,output);
     2 var
     3   q:array[0..100010]of longint;
     4   a,l,r:array[0..100010]of int64;
     5   n,i,t:longint;
     6   ans:int64;
     7 function max(a,b:int64):int64;
     8 begin
     9    if a>b then exit(a) else exit(b);
    10 end;
    11 begin
    12    assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
    13    while true do
    14       begin
    15          read(n);if n=0 then break;
    16          for i:=1 to n do read(a[i]);
    17          t:=0;q[0]:=0;
    18          for i:=1 to n do
    19             begin
    20                while (t>0) and (a[i]<=a[q[t]]) do dec(t);
    21                l[i]:=q[t];
    22                inc(t);q[t]:=i;
    23             end;
    24          t:=0;q[0]:=n+1;
    25          for i:=n downto 1 do
    26             begin
    27                while (t>0) and (a[i]<=a[q[t]]) do dec(t);
    28                r[i]:=q[t];
    29                inc(t);q[t]:=i;
    30             end;
    31          ans:=0;
    32          for i:=1 to n do ans:=max(ans,a[i]*(r[i]-l[i]-1));
    33          writeln(ans);
    34       end;
    35    close(input);close(output);
    36 end.
  • 相关阅读:
    haproxy 2.5 发布
    cube.js sql 支持简单说明
    基于graalvm 开发一个cube.js jdbc driver 的思路
    apache kyuubi Frontend 支持mysql 协议
    oceanbase 资源池删除说明
    基于obd 的oceanbase 扩容说明
    jfilter一个方便的spring rest 响应过滤扩展
    cube.js schema 定义多datasource 说明
    typescript 编写自定义定义文件
    meow 辅助开发cli 应用的工具
  • 原文地址:https://www.cnblogs.com/Currier/p/6686478.html
Copyright © 2011-2022 走看看