01-Complexity of 1. Maximum child columns and problems (20)

Source: Internet
Author: User

Given a sequence of K integers {N1, N2, ..., NK}, "Continuous child column" is defined as {Ni, ni+1, ..., Nj}, where 1 <= i <= J <= K. "Maximum child columns and" are defined as the and the largest of all contiguous child column elements. For example, given sequence {-2, 11,-4, 13,-5,-2}, its contiguous sub-columns {11,-4, 13} have the largest and 20. You are now asked to write a program that calculates the maximum sub-columns of a given integer sequence.

Input format:

Enter line 1th to give the positive integer k (<= 100000), and the 2nd line to give the k integers, separated by a space.

Output format:

Outputs the maximum child columns in a row. If all integers in the sequence are negative, the output is 0.

Input Sample:

6-2 11-4 13-5-2

Sample output:

20
1 ImportJava.util.Scanner;2 3 //there can be only one common class in a file, and it is consistent with the file name, case note4  Public classmain{5   //entrance to the program6    Public Static voidMain (String args[]) {7Scanner sc =NewScanner (system.in);8String num =sc.nextline ();9string[] str = Sc.nextline (). Split ("");Ten sc.close (); One       A        -       intn =integer.parseint (num); -       int[] Numarr =New int[n]; the        for(inti=0;i<n;i++){ -Numarr[i] =Integer.parseint (Str[i]); -       } -  +       intresult =Maxsumrec (Numarr); - System.out.print (result); +        A   } at  -   Private Static intMaxsumrec (int[] a) { -     //TODO auto-generated Method Stub -     returnMaxsumrec (a,0,a.length-1); -   } -  in   /** - * Find the maximum subsequence in array A and, if all negative, return 0 to    * @parama +    * @paramleft Border -    * @paramRight Border the    * @return  *    */ $   Private Static intMaxsumrec (int[] A,intLeftintRight ) {Panax Notoginseng     //Base Case -     if(left = =Right ) { the       if(a[left]>0){ +           returnA[left]; A}Else{ the           return0; +       }     -     } $      $     intCenter = (left + right)/2; -     intMaxleftsum =Maxsumrec (a,left,center); -     intMaxrightsum = Maxsumrec (a,center+1, right); the      -     intmaxleftbordersum = 0,leftbordersum = 0;Wuyi      for(inti=center;i>=left;i--){ theLeftbordersum + =A[i]; -         if(leftbordersum>maxleftbordersum) { WuMaxleftbordersum =leftbordersum; -         } About     } $      -     intmaxrightbordersum = 0,rightbordersum = 0; -      for(inti=center+1;i<=right;i++){ -Rightbordersum + =A[i]; A         if(rightbordersum>maxrightbordersum) { +Maxrightbordersum =rightbordersum; the         } -     } $      the     returnMax3 (maxleftsum,maxrightsum,maxrightbordersum+maxleftbordersum); the   } the    the   Private Static intMAX3 (intAintBintc) { -       intMax = 0; in       if(a>b) { the         if(a>c) { theMax =A; About}Else{ theMax =C; the         } the}Else{ +           if(b>c) { -Max =b; the}Else{BayiMax =C; the           } the       } -     returnMax; -   } the    the}
Evaluation results
Time Results score Topics language spents (MS) memory (KB) User
April 27 21:28 The answer is correct 20 01-Complexity 1 Java (Javac 1.6.0) 308 24700 Liyuhui
Test point
Test Point Results spents (MS) memory (KB) Score/Perfect
0 The answer is correct 78 10380 4/4
1 The answer is correct 80 10296 4/4
2 The answer is correct 122 12704 4/4
3 The answer is correct 218 13952 4/4
4 The answer is correct 308 24700 4/4

View Code

1 ImportJava.util.Scanner;2 3 //there can be only one common class in a file, and it is consistent with the file name, case note4  Public classmain{5   //entrance to the program6    Public Static voidMain (String args[]) {7Scanner sc =NewScanner (system.in);8String num =sc.nextline ();9string[] str = Sc.nextline (). Split ("");Ten sc.close (); One       A        -       intn =integer.parseint (num); -       int[] Numarr =New int[n]; the        for(inti=0;i<n;i++){ -Numarr[i] =Integer.parseint (Str[i]); -       } -  +       intresult =MAXSUMREC4 (Numarr); - System.out.print (result); +        A   } at  -   Private Static intMAXSUMREC4 (int[] a) { -         intMaxsum = 0;  -         intThissum = 0; -          for(intj=0;j<a.length;j++){ -Thissum + =A[j]; in           if(thissum>maxsum) { -Maxsum =thissum; to}Else if(thissum<0){ +Thissum = 0; -           } the         } *          $         returnmaxsum;Panax Notoginseng   } -}
Evaluation results
Time Results score Topics language spents (MS) memory (KB) User
April 27 21:38 The answer is correct 20 01-Complexity 1 Java (Javac 1.6.0) 281 24764 Liyuhui
Test point
Test Point Results spents (MS) memory (KB) Score/Perfect
0 The answer is correct 77 10380 4/4
1 The answer is correct 79 10400 4/4
2 The answer is correct 124 12656 4/4
3 The answer is correct 199 13896 4/4
4 The answer is correct 281 24764 4/4

View Code

01-Complexity of 1. Maximum child columns and problems (20)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.