Hdu 1506 Largest Rectangle in a Histogram (find the Largest Rectangle)
1. Be sure to define a [] as LL. I wa N times here.
2. dp is used to search for boundaries.
AC code:
# Include
# Include
# Define LL long longusing namespace std; const ll inf = 1 <60; LL a [100005]; // define it as LLint L [100005]; int R [100005]; int main () {int n; while (scanf ("% d", & n), n) {for (int I = 1; I <= n; I ++) {scanf ("% lld", & a [I]) ;}for (int I = 1; I <= n; I ++) // use the dp ideology, constantly expanding the boundary {L [I] = I; while (L [I]> 1 & a [L [I]-1]> = a [I]) L [I] = L [L [I]-1];} for (int I = n; I> = 1; I --) {R [I] = I; while (R [I]
= A [I]) R [I] = R [R [I] + 1];} LL ans =-INF; for (int I = 1; I <= n; I ++) {LL temp = a [I] * (R [I]-L [I] + 1); if (ans
Timeout code for finding boundaries without using dp:
# Include
# Include
# Define LL long longusing namespace std; const ll inf = 1 <60; LL a [100005]; int L [100005]; int R [100005]; int main () {int n; while (scanf ("% d", & n), n) {for (int I = 1; I <= n; I ++) {scanf ("% lld", & a [I]) ;}for (int I = 1; I <= n; I ++) // timeout, optimize with dp {int j = I; while (j> 1 & a [j]> = a [I]) j --; L [I] = ++ j;} for (int I = 1; I <= n; I ++) {int j = I; while (j
= A [I]) j ++; R [I] = -- j;} LL ans =-INF; for (int I = 1; I <= n; I ++) {LL temp = a [I] * (R [I]-L [I] + 1); if (ans