This question requires the largest sum of consecutive subsequences. So when I see the question, I think of listing all the situations and comparing the two, taking the maximum value (that is, the meaning of updating the maximum value), this idea is very simple, but it will time out, the time complexity is O (n ^ 3), because there are three for statements
# Include <stdio. h> # define maxn 101000int main () {int ncase, flag = 1, n, Max, sum = 0, H, Z, a [maxn]; long I, j, k; scanf ("% d", & ncase); While (flag <= ncase) {max =-1001; scanf ("% d", & N ); for (I = 0; I <n; I ++) {scanf ("% d", & A [I]) ;}for (I = 0; I <N; I ++) {for (j = I; j <n-1-i; j ++) {sum = 0; For (k = I; k <= J; k ++) sum + = A [k]; If (sum> MAX) {max = sum; H = I; Z = J ;}}}} printf ("case % d \ n", flag); printf ("% d % LD \ n", Max, H + 1, Z + 1 ); printf ("\ n"); flag ++ ;}}
Hangdian 1003 Max sum TLE