Title Description: Find the maximum number of successive sub-sequences in a sequence (if all negative, then output 0)
Analysis idea: A simple DP problem
1. Analysis: The first thought is the division of treatment, the complexity of the division is O[N*LB (n) [put lb as the base of the logarithm of 2], due to the treatment of the connection at the time of the problem, accidentally think of the original this is a DP problem.
2. State transition equation:
(1) sum[i]=max{sum[i-1]+base[i],base[i]};
(2) If sum[i]<0,sum[i]=0;
3. Analysis of the state transition equation:
(1)Sum[i] represents the first I element, containing the maximum subsequence of I and;
(2) Base[i] denotes the first element;
(3) The value of Sum[i] is directly influenced by base[i] and Sum[i-1] (as for why not by sum[i-2],sum[i-3] ... Effect, that's the charm of DP. Very well thought of, just not good explanation. To put it simply, in fact sum[i-2],sum[i-3] ... The effect is already in the calculation sum[i-1],sum[i-2] ... is considered.)
(4) If sumi[i] is negative, in order to guarantee "if all is negative, then output 0", need to initialize sum[i] to 0;
(5) Algorithm complexity: O (N)
(6) Example analysis:
Code Analysis:
1 /*2 3 title:the Bigest sblist sum4 5 DATE:2015/3/116 7 writed by Yanglingwell8 9 Class:software 1403Ten One */ A -#include <stdio.h> - the intMaxintAintb) - { - - returnA>b?a:b; + - } + A intMain () at { - - intT//T-group test data - -scanf"%d",&T); - in while(t--){ - to inti; + intN//a sequence of n data for each set of data - intmaxsum=0;//record maximum subsequence and the int Base[ +]={0};//record data for each group * intsum[ +]={0};//Sum[i] Represents the first I element, which contains the maximum number of sub-sequences of I and $ Panax Notoginsengscanf"%d",&N); - the for(i=1; i<=n;i++) + { A thescanf"%d",&Base[i]); + - } $ $ for(i=1; i<=n;i++) - { - theSum[i]=max (sum[i-1]+Base[I],sum[i]);//main state transfer equation - Wuyi if(Maxsum<sum[i]) maxsum=Sum[i]; the - if(sum[i]<0) sum[i]=0;//when Sum[i] is less than 0 o'clock before the data will be the burden behind, so the sum should be initialized to 0; Wu - } About $printf"%d\n", maxsum); - - } - A return 0; + the}
Test data:
Digression: It is because these two days in research DP problem and memory search, so just feasted this problem.
DP is really interesting.
The maximum continuous sub-sequence of small jobs after the data structure class and