标签:规划 需要 text 就会 sample 网上 out 天才 最好
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 250714 Accepted Submission(s): 59365
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
Case 1: 14 1 4 Case 2: 7 1 6
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int j,i,k,n,m,t; 6 int a[100002]; 7 scanf("%d",&t); 8 for (j=1;j<=t;j++) 9 { 10 scanf("%d",&n); 11 for (i=0;i<n;i++) 12 { 13 scanf("%d",&a[i]); 14 } 15 int sum=0,maxsum=-1001,first =0, last = 0, temp = 1; 16 for (i=0;i<n;i++) 17 { 18 sum += a[i]; 19 if (sum > maxsum) 20 { 21 maxsum = sum;first = temp;last = i+1; 22 } 23 if (sum < 0) 24 { 25 sum = 0;temp = i+2; 26 } 27 } 28 29 printf("Case %d:\n%d %d %d\n",j,maxsum,first,last); 30 if (j!=t) 31 { 32 printf("\n"); 33 } 34 } 35 36 return 0; 37 }
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int j,i,k,n,m,t; 6 int a; //不需要数组,只需要一个输入变量 7 scanf("%d",&t); 8 for (j=1;j<=t;j++) 9 { 10 scanf("%d",&n); 11 int sum=0,maxsum=-1001,first =0, last = 0, temp = 1; 12 for (i=0;i<n;i++) 13 { 14 scanf("%d",&a); 15 sum += a; 16 if (sum > maxsum) 17 { 18 maxsum = sum;first = temp;last = i+1; 19 } 20 if (sum < 0) 21 { 22 sum = 0;temp = i+2; 23 } 24 } 25 //注意格式,我就因为将冒号写到了数的前边而wrong answer,郁闷半天才发现…… 26 printf("Case %d:\n%d %d %d\n",j,maxsum,first,last); 27 if (j!=t) 28 { 29 printf("\n"); 30 } 31 } 32 33 return 0; 34 }
HDU 1003 Max Sum【动态规划求最大子序列和详解 】
标签:规划 需要 text 就会 sample 网上 out 天才 最好
原文地址:http://www.cnblogs.com/ECJTUACM-873284962/p/7219912.html