After reading the Dynamic Programming Method for this question, I suddenly felt that I had a better understanding.
Here we will explain the meaning of DP [I]. DP [I] indicates from 1 ~ I contains the largest substring of the number of I and, if the maximum sum of the number of I-1 containing the I-1 before, then the maximum substring containing the number of I is DP [I-1] + seq [I], otherwise DP [I] is equal to seq [I.
The dynamic recursion of this question does not directly give the final answer state, but it can be pushed to the answer according to another State. This is worth thinking about.
CodeAs follows:
# Include <cstring> # Include <Cstdlib> # Include <Cstdio> # Include <Algorithm> # Define Maxn 100005 Using Namespace STD; Int N, DP [maxn], seq [maxn], p [maxn], begin, end; inline Int Max ( Int X, Int Y ){ Return X> Y? X: Y ;} Void DP ( Int & Max) {memset (DP, 0 , Sizeof (DP); DP [ 1 ] = Seq [1 ]; Max = DP [ 1 ]; Begin = END = 1 ; For ( Int I = 2 ; I <= N; ++ I) {DP [I] = Max (DP [I- 1 ] + SEQ [I], seq [I]); If (DP [I- 1 ]> =0 ) {P [I] = P [I- 1 ];} If (Max < DP [I]) {begin = P [I]; End = I; max = DP [I] ;}} Int Main (){ Int T, ans; scanf ( " % D " ,& T ); For ( Int CA = 1 ; Ca <= T; ++ CA) {scanf ( " % D " ,& N ); For ( Int I = 1 ; I <= N; ++I) {scanf ( " % D " ,& SEQ [I]); P [I] = I;} printf ( " Case % d: \ n " , CA); DP (ANS); printf ( " % D \ n " , ANS, begin, end ); If (Ca! =T) {puts ( "" );}} Return 0 ;}