Title Link: http://poj.org/problem?id=2559
The rectangle with the largest area within the given histogram is obtained.
Using a thing called a monotone stack, the idea is much like DP, that is, each bar as a structure, the height of the current bar and its previous (left) can reach the maximum horizontal distance, and only meet the conditions when the stack.
1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <bitset> One#include <vector> A#include <deque> -#include <queue> -#include <stack> the#include <ctime> -#include <Set> -#include <map> -#include <cmath> + - using namespacestd; + A typedef __int64 LL; at Const intMAXN =100010; -typedefstructNode { - LL H; - intp; - Node (ll _h, LL _p): H (_h), p (_p) {} - }; in - inline ll Max (ll A, ll b) { to returna > B?a:b; + } - the intN; * LL H[MAXN]; $ LL ans;Panax NotoginsengStack<node>St; - the intMain () { + //freopen ("in", "R", stdin); A while(~SCANF ("%d", &n) &&N) { theH[n] =0; + while(!st.empty ()) St.pop (); -Ans =0; $Node TMP (-1,0); $ St.push (TMP); - for(inti =0; I < n; i++) { -scanf"%i64d", &h[i]); the } -LL cur =0;Wuyi for(inti =0; I <= N; i++) { theTmp.h = H[i], TMP.P = i +1; - while(St.top (). h >tmp.h) { WuTMP =st.top (); - St.pop (); Aboutans = max (ans, (i+1-TMP.P) *tmp.h); $ } - St.push (Node (H[i], TMP.P)); - } -printf"%i64d\n", ans); A } + return 0; the}
[POJ2559] Largest Rectangle in a histogram