http://acm.hdu.edu.cn/showproblem.php?pid=1506
第一种方法:
#include<stdio.h>
int a[100005];
int right[100005];
int left[100005];
int main()
{
int n;
int i;
__int64 max;
while(scanf("%d",&n)!=EOF&&n)
{
for(i=0;i<n;i++){
scanf("%d",&a[i]);
left[i]=right[i]=i;
}
for(i=0;i<n;i++)
{
while(left[i]>0&&a[left[i]-1]>=a[i]){ //left[i]不能等于0
left[i]=left[left[i]-1];
}
}
for(i=n-1;i>=0;i--){
while(right[i]<n-1&&a[right[i]+1]>=a[i])//right[i]不能等于n-1
{
right[i]=right[right[i]+1];
}
}
max=-1;
for(i=0;i<n;i++)
{
__int64 m=(__int64)(right[i]-left[i]+1)*a[i];
if(m>max) max=m;
}
printf("%I64d\n",max);
}
return 0;
}
第二种方法:
#include<iostream>
using namespace std;
#define MAX 100005
#define max(a,b) a>b?a:b;
int q[MAX]={-1},w[MAX];
int main()
{
int n,h,i;
__int64 ans;
while(cin>>n&&n)
{
int top=0;
ans=0;
for(i=1;i<=n+1;i++)
{
if(i==n+1)
h=0;
else cin>>h;
if(h>q[top]){
q[++top]=h;
w[top]=1;
}
else
{
__int64 cnt=0;
while(h<=q[top])
{
ans=max(ans,(cnt+w[top])*q[top]);
cnt+=w[top--];
}
q[++top]=h;
w[top]=cnt+1;
}
}
printf("%I64d\n",ans);
}
return 0;
}
浙公网安备 33010602011771号