给你一个图形,让你找到最大的长方形.
思路:
dp,开两个数组L[i],R[i],记录当前的这个点,左边连续大于等于他,和右边连续大于等于他的下标,O(n)的时间处理完这两个数组,然后枚举每一个点,如果以当前的点为长方形的高那么 now = (R[i] - L[i] + 1) * H[i],更新最大就行了...
#include<stdio.h> #define N 100000 + 100 __int64 L[N] ,R[N]; __int64 H[N]; int main () { int n ,i; while(~scanf("%d" ,&n) && n) { for(i = 1 ;i <= n ;i ++) scanf("%I64d" ,&H[i]); L[1] = 1 ,R[n] = n; for(i = 2 ;i <= n ;i ++) { int k = i; while(k > 1 && H[i] <= H[k-1]) k = L[k-1]; L[i] = k; } for(i = n - 1 ;i >= 1 ;i --) { int k = i; while(k < n && H[i] <= H[k+1]) k = R[k+1]; R[i] = k; } __int64 max = 0; for(i = 1 ;i <= n ;i ++) { __int64 now = (R[i] - L[i] + 1) * H[i]; if(max < now) max = now; } printf("%I64d\n" ,max); } return 0; }