hdu-1231-maximal continuous sub-sequence (JAVA+DP dynamic programming)

Source: Internet
Author: User

Maximum continuous subsequenceTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 21101 Accepted Submission (s): 9361


Problem description A sequence of k integers {N1, N2, ..., NK}, any contiguous subsequence can be expressed as {Ni, ni+1, ...,
Nj}, where 1 <= i <= J <= K. The maximal contiguous subsequence is the element and the largest of all successive subsequence sequences,
For example, given sequence {-2, 11,-4, 13,-5,-2}, its maximum contiguous subsequence is {11,-4, 13}, Max
to 20.
In this year's data structure exam, it is required to write the program to get maximum and, now add a requirement, that also need to output the
The first and last elements of a subsequence.

The input test inputs contain several test cases, each with 2 rows, a positive integer k (< 10000) in line 1th, and a K integer in line 2nd, separated by a space. When k is 0 o'clock, the input ends and the use case is not processed.

Output for each test case, the first and last elements of the largest and longest contiguous subsequence are exported in 1 rows
The middle is separated by a space. If the maximum consecutive subsequence is not unique, the output sequence number I and J is the smallest one (as in the 2nd, 3 groups of the input sample). If all k elements are negative, define their maximum and 0, outputting the entire sequence of the first and the end elements.

Sample Input
6-2 11-4 13-5-210-10 1 2 3 4-5-23 3 7-2165-8 3 2 5 01103-1 -5-23-1 0-20

Sample Output
1310 1 410 3 510 100-1 -20 0 0HintHintHuge input, scanf is recommended.

SOURCE Zhejiang University Computer Postgraduate exam on the machine-2005
recommendjgshining | We have carefully selected several similar problems for you:1003 1087 1176 1203 1257

Undoubtedly, the most common sub-series of the problem, the test is dynamic programming, that is, DP. Although not as simple as the 01 backpack, the idea I chose is very simple and understandable. (See the simplest of ideas in parsing ...)

Import Java.io.*;import java.util.*;p ublic class main{public static void Main (string[] args) {Scanner input = new Scanner (S ystem.in); while (Input.hasnext ()) {int n=input.nextint (); if (n==0) break;int a[] = new Int[n];            Open an array to store the given sequence for (int i=0;i<n;i++) {a[i]=input.nextint ();} int sum,max,start,end,cnt=0;    Sum is current and, Max is present Max and, start is the starting element point, end is the end element point int temp=0;                     Used to temporarily save the starting point, that is, the next start,cnt record negative number. sum=start=end=0;max=a[0];for (int i=0;i<n;i++) {sum+=a[i];                  if (Sum>max)                 //If sum is greater than the maximum value, I is the last marker point end and the maximum value is sum.{ end=i;max=sum;start=temp;}                           else if (sum<0)//If current sum<0 and Sum<=max, that is, the current sum is certainly not the maximum value {temp=i+1;               The starting position should be the next number subscript sum=0;cnt++;                  Record negative number}}if (cnt==n)                      //cnt==n All the numbers are negative {System.out.println ("0" + "" +a[0]+ "" +a[n-1]);} else {System.out.println (max+ "" "+a[start]+" "+a[end]");}}}





hdu-1231-maximal continuous sub-sequence (JAVA+DP dynamic programming)

Related Article

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.