| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 18942 |
|
Accepted: 6083 |
Description
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles has equal widths but could 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 is used to represent discrete distributions, e.g., the frequencies of characters in texts. Note the order of the rectangles, i.e, their heights, is important. Calculate the area of the largest rectangle in a histogram that's aligned at the common base line, too. The 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. Assume that
1<=n<=100000. Then follow
NIntegers
H1,..., HN, where
0<=hi<=1000000000. These numbers denote the heights of the rectangles in histogram order. The width of each rectangle is
1. A Zero follows the input for the last test case.
Output
The For all test case is output on a single line, the area of the largest rectangle in the specified histogram. Remember that this rectangle must is aligned at the common base line.
Sample Input
7 2 1 4 5 1 3 34 1000 1000 1000 10000
Sample Output
84000
Hint
Huge input, scanf is recommended.
Source
ULM Local 2003
At first did not notice the interval length of the accumulation, various calculation errors.
Maintain a monocytogenes monotonic stack, updating interval lengths and answers every step.
1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7 using namespacestd;8 Const intmxn=100020;9 structstk{Ten Long Longh,p; One }ST[MXN]; A inttop; - intN; - Long Longans; the intMain () { - while(SCANF ("%d", &n) &&N) { -ans=0; - inti,j; + intnum; - for(i=1; i<=n+1; i++){ + if(i>n) num=0;//after reading the data, clear the stack A Elsescanf"%i64d",&num); at if(num>=st[top].h) st[++top].h=num,st[top].p=1;//calculate the new zone between - Else{ - intlen=0; - while(Top && num<=st[top].h) { -len+=ST[TOP].P; -Ans=max (ans,len*st[top].h); intop--; - } tost[++top].p=len+1;//Cumulative Length +St[top].h=num;//Update Height - } the } *printf"%i64d\n", ans); $ }Panax Notoginseng return 0; -}
POJ 2559 largest Rectangle in a histogram