【HDOJ】1506 Largest Rectangle in a Histogram
Twitter还是Amazon拿这个题目当过面试题。DP区间求面积。
1 /* 1506 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define MAXN 100005 7 #define INF 999999 8 9 int l[MAXN], r[MAXN]; 10 __int64 a[MAXN]; 11 12 __int64 max(__int64 a, __int64 b) { 13 return a>b ? a:b; 14 } 15 16 int main() { 17 int n; 18 int i, j, k; 19 __int64 ans; 20 21 #ifndef ONLINE_JUDGE 22 freopen("data.in", "r", stdin); 23 #endif 24 25 while (scanf("%d",&n)!=EOF && n) { 26 for (i=1; i<=n; ++i) 27 scanf("%I64d", &a[i]); 28 a[0] = a[n+1] = -INF; 29 l[0] = 0; 30 for (i=1; i<=n; ++i) { 31 if (a[i-1] < a[i]) { 32 l[i] = i; 33 } else { 34 j = i; 35 while (j--) { 36 if (a[j] < a[i]) { 37 l[i] = j+1; 38 break; 39 } else { 40 j = l[j]; 41 } 42 } 43 } 44 } 45 r[n+1] = n+1; 46 for (i=n; i>0; --i) { 47 if (a[i+1] < a[i]) { 48 r[i] = i; 49 } else { 50 j = i; 51 while (j++ <= n) { 52 if (a[j] < a[i]) { 53 r[i] = j - 1; 54 break; 55 } else { 56 j = r[j]; 57 } 58 } 59 } 60 } 61 62 ans = -INF; 63 for (i=1; i<=n; ++i) { 64 ans = max(ans, a[i]*(r[i]-l[i]+1)); 65 } 66 printf("%I64d\n", ans); 67 } 68 69 return 0; 70 }