(Hdu step 3.2.1) Max Sum (simple dp: Maximum subsequence and, start point, end point), hdu3.2.1
Make an advertisement for yourself before writing a question ~.. Sorry, I hope you can support my CSDN video courses at the following address:
Http://edu.csdn.net/course/detail/209
Question:
Max Sum |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission (s): 1390 Accepted Submission (s): 542 |
|
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 Input25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5 |
Sample OutputCase 1:14 1 4Case 2:7 1 6 |
AuthorIgnatius. L
|
Question Analysis:
Simple DP. This question is the sum of the largest subsequences, and the starting and ending points of the largest subsequences are required. If you do not need the start and end points. You can directly use last = max (0, last) + a [I]; ans = max (last, ans) to solve the problem. However, even if the start point and the end point are required, the idea is the same. This means that you cannot directly use max (0, last ). This logic should be expanded to find the start and end points for generating the current maximum sequence sum.
If only seek and can refer to: http://blog.csdn.net/hjd_love_zzt/article/details/31782777
If this blog does not understand, you can first look at this: http://blog.csdn.net/hjd_love_zzt/article/details/43562931
The Code is as follows:
/** A1.cpp ** Created on: February 6, 2015 * Author: Administrator */# include <iostream> # include <cstdio> using namespace std; int main () {int t; scanf ("% d", & t); int k; for (k = 1; k <= t; ++ k) {int n; scanf ("% d ", & n); int start = 1; // the starting point of the maximum subsequence and. the default value is 1int end = 1; // the end point of the maximum subsequence and. the default value is 1.int thisSum = 0; // The sum of the current subsequence and int maxSum =-100005; // The sum of the largest subsequence and int index = 1; // start point of the current subsequence int I; int temp; for (I = 1; I <= n; ++ I) {scanf ("% d", & temp ); ThisSum + = temp; // The sum of the current subsequence is constantly increasing if (thisSum> maxSum) {// if the sum of the current subsequence and> maximum subsequence and maxSum = thisSum; // update the maximum subsequence and start = index; // update the starting point of the largest subsequence TO end = I of the current subsequence; // update the end point of the largest subsequence to the number that is currently traversed (because this number generates the largest subsequence and)} // note that, even if thisSum is not larger than maxSum. the sequence is not broken at this time. It depends on whether the sequence will be recovered later/*** note that the two if statements should not be written as * if () {*} else if () {*} structure ** because thisSum <maxSum & thisSum <0 exists */if (thisSum <0) {// if it is not recovered later, on the contrary, it has <0 thisSum = 0; // disconnected the original sub-sequence, which is heavy Index = I + 1; // update the starting point of the current subsequence} printf ("Case % d: \ n", k ); printf ("% d \ n", maxSum, start, end); if (k! = T) {printf ("\ n") ;}} return 0 ;}