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

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

Posted by ccnu_xieshuo at 2008-05-09 13:19:31 on Problem 2479
In Reply To:我的想法清晰,但超时了,哪位大牛帮我看看,不好意思贴出代码 Posted by:ccnu_xieshuo at 2008-04-30 22:01:10
> >将数分段,求前一段和后有一段的的最大字段和,求出其中的最大值。被一个想法困在这里了,>没办法跳出这个圈子,大牛帮忙看看。
> >
> #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;
> 

教科书上动态规划求最大子段和,不过这里分成了两段,上面的那个不行是个0(n^2)现在知道怎么做了,谢谢,还以为没人理呢,感动中!

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