您的位置:首页 > 其它

POJ 1506 Largest Rectangle in a Histogram (dp的思想)

2013-08-24 14:13 381 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: