题目链接:https://vjudge.net/contest/126708#problem/J
题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 比较就行,如果加上一数小于0了那么肯定要重新开始找,否则就不断更新最大值就行
AC代码:
1 #include<stdio.h> 2 #include<string.h> 3 int dp[1000000]; 4 int main() 5 { 6 int t,a,i,max,n,sum,start,ends,f; 7 scanf("%d",&t); 8 for(int t1 = 1; t1 <= t;t1++) 9 { 10 11 scanf("%d",&n); 12 max =-1001;start = 1;ends = 1;f = 1;sum = 0; 13 for(i = 0; i < n ; i ++) 14 { 15 scanf("%d",&a); 16 sum += a; 17 if(sum > max) {max = sum;ends = i+1;start = f;} 18 if(sum<0) {sum = 0; f = i+2;} 19 } 20 printf("Case %d:\n",t1); 21 printf("%d %d %d\n",max,start,ends); 22 if(t1 != t) printf("\n"); 23 } 24 return 0; 25 }