HDU 1003 Max sum (maximum sum of consecutive subcolumns, Dynamic Planning)

Source: Internet
Author: User
Max sum

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 55792 accepted submission (s): 12580

Problem descriptiongiven a sequence a [1], a [2], a [3] ...... A [n], your job is to calculate the max sum of a sub-sequence. for example, given (6,-1, 5, 4,-7), the Max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Inputthe first line of the input contains an integer T (1 <= T <= 20) which means the number of test cases. then T lines follow, each line starts with a number N (1 <= n <= 100000), then n integers followed (all the integers are between-1000 and 1000 ).

Outputfor each test case, you should output two lines. the first line is "case #:", # means the number of the test case. the second line contains three integers, the max sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. if there are more than one result, output the first one. output a blank line between two cases.

Sample input2
5 6-1 5 4-7
7 0 6-1 1-6 7-5

Sample outputcase 1:
14 1 4

Case 2:
7 1 6

Authorignatius. L Algorithm : 1. always add until there is a negative number and a maximum sum can be obtained during the addition, record
2. Repeat Step 1 to update the largest sum until the input is complete.
Principle: If the substring <A1, A2, A3,... is in a row ,..., an> medium Sn <0; S1, S2 ,... S (n-1) are both greater than or equal to 0
Then there must be the largest Si, that is, the largest sum of the continuous substrings from A1 to.
We know that when A1> 0, the sum of any <AI... an> (I> 1) is smaller than 0,
Therefore, these elements cannot form the largest and largest substrings (if a1 = 0, A2> = 0) with an element );
At the same time, the sum of <AI... AJ> (I> 1, j <= N) cannot be greater than that of Si.
 /*  
Hdu1003
Dynamic Planning
*/
# Include < Stdio. h >
# Define Maxn 100000
Int Main ()
{
Int T, icase, I, sum, maxsum, J, X, Y,;
Int N;
Scanf ( " % D " , & T );
Icase = 0 ;
While (T -- )
{
Icase ++ ;
Sum = 0 ;
Maxsum =- 20000 ;
I = 1 ;
Scanf ( " % D " , & N );
For (J = 1 ; J <= N; j ++ )
{
Scanf ( " % D " , & A );
Sum + = A;
If (Maxsum < Sum)
{
Maxsum = SUM;
X = I;
Y = J;
}
If (Sum < 0 )
{
I = J + 1 ;
Sum = 0 ;
}
}
Printf ( " Case % d: \ n " , Icase );
Printf ( " % D \ n " , Maxsum, x, y );
If (T > 0 ) Printf ( " \ N " );
}
Return 0 ;
}

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.