Language:DefaultLargest Rectangle in a histogram
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 15608 |
|
Accepted: 5040 |
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 integerN, denoting the number of rectangles it is composed of. Assume that1<=n<=100000. Then followNIntegersH1,..., HN, where0<=hi<=1000000000. These numbers denote the heights of the rectangles in histogram order. The width of each rectangle is1. 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 |
Test instructions: Gives the height of the n rectangles, and asks what the area of the largest rectangle they can be made of.
Idea: Define two arrays l[i] and R[i], recording the maximum position from I point to left to right. If a[i]<=a[L[i]-1], then l[i] = l[L[i]-1] (if I can extend to the l[i]-1, then l[i]-1 at l[l[i]-1 before calculation i] has been calculated, can directly make l[i]=l[l[i]-1]); , if a[i]<=a[r[i]-1], then r[i] = r[R[i]-1]. This takes full advantage of the data that was previously calculated.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #define MAXN 100010#define FRE (i,a,b) for (i = A; I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ( (t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (A , b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long Long L L;using namespace Std;ll n;ll l[maxn],r[maxn],a[maxn];int main () {ll i; while (scanf ("%lld", &n)) {if (n==0) break; MEM (a,0); FRE (i,1,n) scanf ("%lld", &a[i]); A[0]=-1; A[n+1]=-1; FRE (i,1,n) {l[i]=i; while (A[i]<=a[l[i]-1]) l[i]=l[l[i]-1]; } free (i,n,1) {r[i]=i; while (A[i]<=a[r[i]+1]) r[i]=r[r[i]+1]; } ll Ans=0; FRE (i,0,n) Ans=max (ans,a[i]* (r[i]-l[i]+1)); PF ("%lld\n", ans); } return 0;}
Largest Rectangle in a histogram (POJ 2559 && hdu 1506 Rectangle Series Iteration method)