HISTOGRA - 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 Specification

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 Specification

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

Added by:Wanderley Guimarăes
Date:2007-09-21
Time limit:0.800s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM32-GCC MAWK BC BF C-CLANG NCSHARP CPP14 CPP14-CLANG COBOL COFFEE D-CLANG D-DMD DART ELIXIR ERL FANTOM FORTH GRV JS-RHINO JS-MONKEY JULIA KTLN NIM OBJC OBJC-CLANG OCT PICO PROLOG PYPY3 R RACKET RUST SCM qobi CHICKEN SQLITE SWIFT UNLAMBDA VB.NET
Resource:University of Ulm Local Contest 2003

hide comments
2015-01-03 22:53:22 Jordan L
Thanks for the long long tip! Fixed my error.
2015-01-01 05:21:46 Forrest Glines
I had a lot of problems with too small integer types - make everything a "long long" - just to be safe
2014-12-31 22:32:49 Cameron Blocker
@Ken Reese
Thanks for the tip. I was having problems too when I saw your comment and that turned out to be it. At first I just changed my area variable to a long long and then I realized that you have to cast the ints your using to compute it as well.
Also @Christopher Almquist, thanks for the clue to use a stack, took a while to figure out how but very efficient.
2014-12-31 21:55:32 anonymous
Make sure to use the long long when computing your area; it will not pass on long or int alone. Some of those areas can get really large!

Last edit: 2014-12-31 21:56:36
2014-12-29 07:55:24 Matthew Carlson
I'm really struggling to figure out what I'm getting wrong. Maybe I shouldn't have but I found a few other algorithms on the net and I am testing those against the output of mine. I've run a few hundred randomized tests and so far they've all been correct. Any suggestions?

Last edit: 2014-12-29 07:55:50
2014-12-27 23:45:34 Elora
For all you getting as frustrated as I was with time limits, don't try it in Java. I transferred my code from java to c++ and went from a second of run time to 1/10 of a second.
2014-12-23 23:43:10 Christopher Almquist
I used a stack to store indices of smaller bars. Since each index was pushed once and popped once, it was O(n). I think a divide and conquer O(nlogn) algorithm might work too.

Last edit: 2014-12-23 23:43:28
2014-12-23 21:49:14 Kevin Haroldsen
@Jordan L
Agreed. Seems a very unique algorithm is needed.
2014-12-20 20:09:26 Jordan L
Wow these time limits are strict!
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.