Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一定要注意最大值为0的情况啊

Posted by 1098643527 at 2016-07-25 11:21:01 on Problem 2796
#include<stdio.h>
int n,t,al,ar;
int lmax[100001],rmax[100001],q[100001];
long long a[100001],ans,sum[100001];
int main()
{
	scanf("%d",&n);
	int i;
	for(i=1;i<=n;i++)
	{
		scanf("%I64d",&a[i]);
		sum[i]=sum[i-1]+a[i];
	}
	a[0]=-1;
	a[n+1]=-1;
	t=0;	q[0]=0;
	for(i=1;i<=n;i++)
	{
		while(t>0&&a[q[t]]>=a[i])
			t--;
		lmax[i]=q[t];
		q[++t]=i;
	}
	t=0;	q[0]=n+1;
	for(i=n;i>=1;i--)
	{
		while(t>0&&a[q[t]]>=a[i])
			t--;
		rmax[i]=q[t];
		q[++t]=i;
	}
	ans=-1;
	for(i=1;i<=n;i++)
	{
		if(ans<a[i]*(sum[rmax[i]-1]-sum[lmax[i]]))
		{
			ans=a[i]*(sum[rmax[i]-1]-sum[lmax[i]]);
			al=lmax[i]+1;
			ar=rmax[i]-1;
		}
	}
	printf("%I64d\n%d %d",ans,al,ar);
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator