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

我的想法清晰,但超时了,哪位大牛帮我看看,不好意思贴出代码

Posted by ccnu_xieshuo at 2008-04-30 22:01:10 on Problem 2479
>将数分段,求前一段和后有一段的的最大字段和,求出其中的最大值。被一个想法困在这里了,>没办法跳出这个圈子,大牛帮忙看看。
>
#include<iostream>
using namespace std;

int getmax(int a[],int n);

int a[50001];

int main()
{
	int test,num;
	int i,j,k;
	cin>>test;
	while(test--)
	{
		int max=0;
		scanf("%d",&num);
		for(i=1;i<=num;i++) scanf("%d",&a[i]);
		for(i=1;i<num;i++)
		{
			int max1,max2;
			max1=getmax(a+1,i);
			max2=getmax(a+i+1,num-i);
			k=max1+max2;
			if(k>max) max=max1+max2;
		}
		printf("%d\n",max);
	}
	return 1;
}


int getmax(int a[],int n)
{
	int i,j;
	int b=0,max=0;
	for(i=0;i<n;i++)
	{
		if(b<0) {b=a[i];continue;}
		b+=a[i]; 
		if(b>max) max=b;
	}
	return max;
}

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