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

I'm a beginner ....any one can show me the solution of this problem...This's mine...can not pass the limit of time; Can the solution have complexity of O(logn)?

Posted by HongHienNTU at 2009-09-02 12:10:55 on Problem 2479
import java.util.Scanner;
public class Main {
	public static int numTest;
	public static int n;
	
	public static int[] a = new int[50000];
	
	//e[i] = the maximum sum of sequence ending at i;
	public static long[] e = new long[50000];
	//b[i] = the maximum sum of sequence starting at i;
	public static long[] b = new long[50000];

	
	public static void main(String[] args) {

		Scanner sc = new Scanner( System.in );
		numTest = sc.nextInt();
			
		while( numTest > 0){
			
			n = sc.nextInt();
			for( int i = 0; i < n; i++)
				a[i] = sc.nextInt();
				
			//optimize
			e[0] = a[0];

			b[n-1] = a[n-1];
				
			//i for end;
			//j for beginning;
			int j;
			for( int i = 1; i < n; i++){
				j = n - 1 - i;
				
				e[i] = a[i];
				if ( e[i - 1] > 0)
					e[i] = a[i] + e[i-1];
				
				b[j] = a[j];
				if( b[j + 1] > 0)
					b[j] =  a[j] + b[j+1];
			}
			
			for( int i = n - 2; i >= 0; i--){
				if( b[i] < b[i + 1] )
					b[i] = b[ i + 1];
			}
				
				
			//find the maximum;
			long max = e[0] + b[1];
			for( int i = 1; i <= n-2; i++)
				if( max < e[i] + b[i+1])
					max = e[i] + b[i+1];
				
			System.out.println(max);
				
			--numTest;
		}//end while;
			
	}//end main;

}//end class;

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