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 celia01 at 2011-05-24 16:33:04 on Problem 2593
#include<stdio.h>
#include<stdlib.h>

int a[100005], max1[100005], max2[100005];
int n;

/*int fmax1 (int i){
	int k, max=0;
	int s = 0;
	for (k=0;k<=i;k++){
		s += a[k];
		if (s<0)  
			s=0;
		if (s>max)
			max=s;
	}
	return max;
}

int fmax2 (int i){
	int k, max=0;
	int s = 0;
	for (k=n-1;k>=i;k--){
		s += a[k];
		if (s<0)  
			s=0;
		if (s>max)
			max=s;
	}
	return max;
}*/

int findmax(int a[], int size){
	int i, j, max=-1005;
	for (i=0;i<=size-1;i++){
		if (a[i]>max){
			j = i;
			max = a[i];
		}
	}
	a[j] = -1005;
	return max;
}

//#define local

int main(){
	int flag,sum,max;
	int i;
	int k, s;


#ifdef local
	freopen("input1.txt","r",stdin);   //-5 9 -5 11 20
#endif

	scanf("%d",&n);
	while(n!=0){
		flag = 0;
		for (i=0;i<=n-1;i++){
			scanf("%d",&a[i]);
			if (a[i]>=0)
				flag++;
		}
		if (flag<=2){
			max = findmax(a,n)+findmax(a,n);
		}
		else{
			for (i=0;i<=n-2;i++){
				max1[i]=0;s = 0;
				for (k=0;k<=i;k++){
					s += a[k];
					if (s<0)  
						s=0;
					if (s>max1[i])
						max1[i]=s;
				}
			}
			for (i=n-1;i>=1;i--){
				max2[i]=0;s = 0;
				for (k=n-1;k>=i;k--){
					s += a[k];
					if (s<0)  
						s=0;
					if (s>max2[i])
						max2[i]=s;
				}
			}
		
			max = 0;
			for (i=0;i<=n-2;i++){
				sum = max1[i]+max2[i+1];
				if (sum>max)
					max = sum;
			}
		}
		printf("%d\n",max);
		scanf("%d",&n);
	}
	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