Max Sum
Description
Given 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.
Input
The 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 is b etween-1000 and 1000).
Output
For the test case, you should output of the lines. The first line was "Case #:", # means the number of the the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end posi tion of the sub-sequence. If there is more than one result, output the first one. Output a blank line between cases.
Sample Input
Sample Output
Exercises
The topic of dynamic planning, encountered this problem before, when using two arrays, direct calculation, the results are unknown. Then did not do, this time met again, understand the dynamic planning thought, the solution of this problem has a new idea the following is the variable description: T test Data Group N the length of each set of data A[i] current dataMin_n The starting position of the last Max sum
Max_n End position of last Max Sum
Max_sum the Max Sumsum that is currently being read into the data, the mostK record the maximum and start position because you don't know the size of the previous Max value, so save it first.
M record the maximum and the terminating position, because I don't know the size of the previous Max value, so save it first .
The following simulation process: 1. First, read the first data, make max_sum equal to the first data, initialize the min_n,max_n,k,m position and Sum value 2. Then, read the second data and judge the ①. If sum > Max_sumThat represents the current data and is larger than the previously stored data, and updates the storage
②. If sum < 0, then the item is definitely not suitable for back-up, so the sum is copied to 0, and the current position starts with 3. Output results after traversing over again
Attached code:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include < algorithm> #include <vector> #define INF 0x3f3f3f3fusing namespace Std;int a[100010];int main () {int t,num = 1;cin >> T;while (t--) {int n;cin >> n;memset (a,0,sizeof (a)), for (int i = 1;i <= n;i++) {scanf ("%d", &a[i]);} A long long int sum = 0;int Min_n = 1,max_n = 1,max_sum = A[1];int k = 1,m = 1; for (int i = 1;i <= n;i++) {sum + = A[i];m = i;if (Sum > Max_sum) // If the current added value is larger than the original, make an update {max_sum = sum; Min_n = k; Max_n = m;} if (Sum < 0) /// If the current value is negative, then it is definitely not suitable to add, because the lower is definitely lower than the next start, so give sum a value of 0, and then do an initial position of the tag. {sum = 0;k = i + 1;m = i + 1;}} if (num > 1) printf ("\ n");p rintf ("Case%d:\n", num++), cout << max_sum << "<< min_n <<" <& Lt Max_n << Endl; }return 0;}
HDU1003 Max Sum (classic DP,)