Topic Portal
1 /*2 Test Instructions: Width is 1, height varies, maximum rectangular area is obtained3 stack (array emulation): For each a[i] l[i],r[i] coordinate position representation A[l[i]] < A[i] < a[r[i]) limit conditions4 st[] is strictly monotonically increasing, if not recorded, O (n) to find L,r, with the stack words to reduce the complexity of5 */6#include <cstdio>7#include <cstring>8#include <algorithm>9#include <stack>Ten#include <iostream> One using namespacestd; A -typedefLong Longll; - the Const intMAXN = 1e5 +Ten; - Const intINF =0x3f3f3f3f; - intA[MAXN], L[MAXN], R[MAXN]; - intST[MAXN]; + - intMainvoid)//POJ 2559 Largest Rectangle in a histogram + { A //freopen ("poj_2559.in", "R", stdin); at - intN; - while(SCANF ("%d", &n) = =1) - { - if(n = =0) Break; - for(intI=1; i<=n; ++i) scanf ("%d", &a[i]); inmemset (St,0,sizeof(ST)); - to intp =0; + for(intI=1; i<=n; ++i) - { the while(P >=1&& a[st[p-1]] >= a[i]) p--; *L[i] = (p = =0) ?0: st[p-1]; $st[p++] =i;Panax Notoginseng } - thep =0; + for(intI=n; i>=1; --i) A { the while(P >=1&& a[st[p-1]] >= a[i]) p--; +R[i] = (p = =0) ? n +1: st[p-1]; -st[p++] =i; $ } $ -ll ans =0; - for(intI=1; i<=n; ++i) the { -ans = max (ans, (LL) a[i] * (R[i]-l[i]-1));Wuyi } theprintf ("%i64d\n", ans); - } Wu - return 0; About}
Stack (array emulation) POJ 2559 largest Rectangle in a histogram