标签:des style blog http io ar color os sp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20109 Accepted Submission(s): 8884
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define MAX 100005 5 6 int main(void) 7 { 8 int t,n,count; 9 int dp[MAX]; 10 int max,start,START,end; 11 count = 0; 12 13 scanf("%d",&t); 14 while(t --) 15 { 16 count ++; 17 18 scanf("%d",&n); 19 for(int i = 0;i < n;i ++) 20 scanf("%d",&dp[i]); 21 22 max = dp[0]; 23 start = START = end = 1; 24 25 for(int i = 1;i < n;i ++) 26 { 27 if(dp[i - 1] < 0 && dp[i - 1] != dp[i]) //讨论dp[i-1]小于0和大于等于0两种情况即可,后面的条件是为了符合题意 28 start = i + 1; //更新起点 29 else if(dp[i - 1] >= 0) 30 dp[i] = dp[i - 1] + dp[i]; //隐式地更新终点 31 32 if(max < dp[i]) 33 { 34 START = start; 35 max = dp[i]; 36 end = i + 1; 37 } 38 } 39 printf("Case %d:\n",count); 40 printf("%d %d %d\n",max,START,end); 41 if(t) 42 puts(""); 43 } 44 45 return 0; 46 }
上面的代码我用了两个循环,下面这个版本只用了一个,速度反而没第一个快,不知为何。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define MAX 100005 5 6 int main(void) 7 { 8 int t,n,count; 9 int dp[MAX]; 10 int max,start,START,end; 11 count = 0; 12 13 scanf("%d",&t); 14 while(t --) 15 { 16 count ++; 17 18 scanf("%d",&n); 19 for(int i = 0;i < n;i ++) //在读入的时候就顺便处理,不知为何会更慢 20 { 21 scanf("%d",&dp[i]); 22 if(!i) 23 { 24 max = dp[0]; 25 start = START = end = 1; 26 } 27 else if(dp[i - 1] < 0 && dp[i - 1] != dp[i]) 28 start = i + 1; 29 else if(dp[i - 1] >= 0) 30 dp[i] = dp[i - 1] + dp[i]; 31 32 if(max < dp[i]) 33 { 34 START = start; 35 max = dp[i]; 36 end = i + 1; 37 } 38 } 39 40 printf("Case %d:\n",count); 41 printf("%d %d %d\n",max,START,end); 42 if(t) 43 puts(""); 44 } 45 46 return 0; 47 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define MAX 10005 5 6 int main(void) 7 { 8 int k; 9 int dp[MAX],s[MAX],max,max_start,max_end,start; 10 11 while(scanf("%d",&k) && k) 12 { 13 for(int i = 0;i < k;i ++) 14 scanf("%d",&s[i]); 15 16 max = s[0]; 17 dp[0] = s[0]; 18 max_start = max_end = start = 0; 19 20 for(int i = 1;i < k;i ++) 21 { 22 if(dp[i - 1] < 0 && s[i] != dp[i - 1]) //一样的讨论是否为负就行了 23 { 24 start = i; 25 dp[i] = s[i]; 26 } 27 else if(dp[i - 1] >= 0) 28 dp[i] = dp[i - 1] + s[i]; 29 30 if(dp[i] > max) 31 { 32 max = dp[i]; 33 max_start = start; 34 max_end = i; 35 } 36 } 37 38 if(max < 0) 39 { 40 max = 0; 41 max_start = 0; 42 max_end = k - 1; 43 } 44 printf("%d %d %d\n",max,s[max_start],s[max_end]); 45 } 46 47 return 0; 48 }
这题还有下面这个版本,就是用个双重循环来选出起点和终点,然后就算这个区间的值,可以用一个循环算出以1为起点的值,然后再计算的时候就可以用这个数组推出来,感觉挺不错的,也有DP的思想在里面,虽然超时了。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define MAX 10005 5 6 int main(void) 7 { 8 int k,i,j; 9 long long s[MAX],dp[MAX],box,max,max_i,max_j; 10 11 while(scanf("%d",&k) && k) 12 { 13 scanf("%lld",&dp[0]); 14 s[0] = dp[0]; 15 for(int i = 1;i < k;i ++) 16 { 17 scanf("%lld",&dp[i]); 18 s[i] = dp[i]; 19 dp[i] += dp[i - 1]; //DP[i]保存以1为起点i为终点的区间的值 20 } 21 22 max = dp[0]; 23 max_i = max_j = 0; 24 for(int i = 0;i < k;i ++) 25 for(int j = i;j < k;j ++) 26 { 27 if(i) 28 box = dp[j] - dp[i - 1]; //i...j区间的值等于1...j的值减去1...i-1的值 29 else 30 box = dp[j]; 31 32 if(box > max) 33 { 34 max = box; 35 max_i = i; 36 max_j = j; 37 } 38 } 39 40 if(max < 0) 41 { 42 max = 0; 43 max_i = 0; 44 max_j = k - 1; 45 } 46 printf("%lld %lld %lld\n",max,s[max_i],s[max_j]); 47 } 48 49 return 0; 50 }
HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/xz816111/p/4159455.html