GivenA sequence of k integers {N?1??,N?2??, ...,N?K??}, "Continuous child column" is defined as {N?I??,N?I+1??, ...,< Span class= "Mord" >n? j ?? }, where < Span class= "base textstyle uncramped" >1≤i< Span class= "Mrel" >≤j≤k. "Maximum child columns and" are defined as the and the largest of all contiguous child column elements. For example, given sequence {-2, 11,-4, 13,-5,-2}, its contiguous sub-columns {11,-4, 13} have the largest and 20.
Input format:
The input line 1th gives a positive integer k (≤), and the 2nd line gives the k integers, separated by a space.
Output format:
Outputs the maximum child columns in a row. If all integers in the sequence are negative, the output is 0.
Input Sample:
6-2 11 -4 13 -5 -2
Sample output:
20
I wrote the following code:
1#include <stdio.h>2 3 4 intMaxsubseqsum (intA[],intn);5 intMain ()6 {7 intN;8 inta[100005];9 intsum=0;Tenscanf"%d", &n); One for(intI=0; i<n; i++) A { -scanf"%d",&a[i]); - } thesum=maxsubseqsum (a,n); -printf"%d\n", sum); - return 0; - } + intMaxsubseqsum (intA[],intN)//To find the maximum subsequence length function - { + intthissum,maxsum; A inti; atthissum=maxsum=0; - for(i=0; i<n; i++) - { -thissum+=A[i]; - - if(thissum>maxsum) inmaxsum=thissum; - Else if(thissum<0) to +thissum=0; - the * $ }Panax Notoginseng returnmaxsum; -}
The time complexity of this program is relatively good, probably O (n) bar. Of course, the problem can also be violent with three cycles of violence out, but time complexity is too high, O (n^3), not recommended.
Well, explain it now.
Let's take a sample,
6-2 11 -4 13 -5 -2
6 numbers into the array a[], and then into the function maxsubseqsum (), passing in the array length n, and arrays of elements a[].
In the body of the function, first define a current sum of thissum and the sum of the subsequence maxsum.
Initialize them to 0, then a loop is done.
In the loop, we first give the first number of the sequence to thissum, in this case is 2 to thissum, to determine whether the thissum at this time is greater than maxsum, obviously, -2<maxsum,
Good! If the first if is not true, go to else if, discover -2<0, then we will reset the Thissum to 0. The first number ends.
Now start the second number, which is 11,thissum=11, at this point, thissum>maxsum=0 maxsum to 11. And at this point, we don't thissum=11>0 it to 0, it's still 11.
In this column push, at this time, thissum=11+ (-4) = 7, when, smaller than maxsum, will not give it to maxsum. But it's bigger than 0,thissum or 7.
Next, when Thissum=7+13=20, thissum=20>maxsum=11, we can set the Maxsum to 20. Obviously at this time thissum=20 is greater than 0, it temporarily pail 0
Encounter -5,thissum=15, it can not shake Maxsum 20, Encounter-2, thissum=13, or can not shake Maxsum 20,
So, finally maxsum=20, the function returns the MAXSUM=20.
A bit verbose, but detailed. Please correct me if there is any mistake.
2. This is slightly more difficult, to find the maximum subsequence at the same time, please give the maximum sub-sequence of the head and tail of the corresponding value.
#include <bits/stdc++.h>
using namespace Std;
void maxsubseqsum (int a[],int n);
int main ()
{
int n;
int a[100];
int sum=0;
scanf ("%d", &n);
for (int i=0; i<n; i++)
{
scanf ("%d", &a[i]);
}
Maxsubseqsum (A,n);
return 0;
}
void maxsubseqsum (int a[],int N)
{
int thissum,maxsum;
int i;
The position of the int cnt=0;///header
The position of the int outs=0;///tail
int flag=0,flag_max=0;///Change Flag_max when single maximum value is changed
int mfirst=0;///Possible head
thissum=maxsum=0;
for (i=0; i<n; i++)
{
Thissum+=a[i];
if (thissum>maxsum)
{
Flag_max=1;
if (flag==0)
{
Cnt=mfirst;
flag=1;
}
Maxsum=thissum;
Outs=i;
}
else if (thissum<0)
{
mfirst=i+1;
thissum=0;
flag=0;
}
}
if (flag_max==0)
{
printf ("0%d%d\n", a[0],a[n-1]);
}
Else
{
printf ("%d%d%d\n", maxsum,a[cnt],a[outs]);
}
}
Sum problem of maximal sub-sequence