Problem 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 ' 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 B etween-1000 and 1000).
Output
For each test case, you should output two lines. The the "the" "Case #:", # means 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 are more than one result, output the one. Output a blank line between two cases.
Sample Input
2
5 6-1 5 4-7
7 0 6-1 1-6 7-5
Sample Output
Case 1:
14 1 4
Case 2:
7 1 6
This is a simple question of finding the largest substring:
#include <iostream> #include <stdio.h> using namespace std;
int main () {int n;//total test Data Set number scanf ("%d", &n); for (int i=1;i<=n;i++) {int left=1,right=0,lefttemp=1;//to the left of the largest substring, to the right, and a tentative temporary left int max=-999999,maxtemp=0;//
The maximum and value of the substring, tentatively temporary maximum and value int m;//numeric string length int temp;//Get the data of the temporary variable scanf ("%d", &m);
for (int j=1;j<=m;j++) {scanf ("%d", &temp);
maxtemp+=temp;//added to the temporary maximum and medium if (max<maxtemp) {//If temporary maximum and ratio is determined before maximum and large max=maxtemp;//will be temporarily maximum and certainty down left=lefttemp;//at this point the tentative left is also ok right=j;//the right side is the currently selected right} if (Maxtem
p<0) {//If the temporary maximum and negative numbers, the previous data will not necessarily be able to add to the maximum substring, may be found in the back of the most large string (need to use the temporary left) lefttemp=j+1;//let the temporary left from the following number not less than 0 to start again maxtemp=0;//again, let the temporary Max and also restart from 0} if (i!=n) printf ("Case%d:\n%d%d%d\n\n"
, i,max,left,right); else printf ("Case%d:\n%d%d%d\n", I,max,left,rigHT);
return 0; }