POJ 1506 Largest Rectangle in a Histogram (dp的思想)
POJ 1506 Largest Rectangle in a Histogram (dp的思想)
#include <iostream> #include <cstdio> #include <queue> #include <cmath> using namespace std; const int maxn=100010; unsigned long long a[maxn],l[maxn],r[maxn]; int n; void computing(){ unsigned long long ans=0; l[0]=0;r[n-1]=n-1; for(int i=1;i<n;i++){ int pos=i; while(pos>0 && a[pos-1]>=a[i]) pos=l[pos-1]; l[i]=pos; } for(int i=n-2;i>=0;i--){ int pos=i; while(pos<n-1 && a[pos+1]>=a[i]) pos=r[pos+1]; r[i]=pos; } for(int i=0;i<n;i++){ ans=max( (r[i]-l[i]+1)*a[i] , ans); } cout<<ans<<endl; } int main(){ while(scanf("%d",&n)!=EOF && n>0){ for(int i=0;i<n;i++) scanf("%I64d",&a[i]); computing(); } return 0; }