HDU 1506 Largest Rectangle in a Histogram
和POJ 2796 Feel Good类似。
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=100000+10; long long a[maxn]; int L[maxn],R[maxn]; int n; int main() { while(~scanf("%d",&n)) { if(n==0) break; for(int i=1;i<=n;i++) scanf("%lld",&a[i]); for(int i=1;i<=n;i++) L[i]=i; for(int i=2;i<=n;i++) { if(a[i]>a[i-1]) continue; int pre=L[i-1]; while(1) { L[i]=pre; if(pre==1||a[pre-1]<a[i]) break; pre=L[pre-1]; } } for(int i=n;i>=1;i--) R[i]=i; for(int i=n-1;i>=1;i--) { if(a[i]>a[i+1]) continue; int pre=R[i+1]; while(1) { R[i]=pre; if(pre==n||a[pre+1]<a[i]) break; pre=R[pre+1]; } } long long ans=-1; for(int i=1;i<=n;i++) ans=max(ans,a[i]*(R[i]-L[i]+1)); printf("%lld\n",ans); } return 0; }