hdu 1003 Max Sum(基础dp)
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 72615 Accepted Submission(s): 16626
Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Sample Output
Case 1: 14 1 4
Case 2: 7 1 6
算法分析:求最大字段和,d[i]表示已 i 结尾(字段和中包含 i )在 a[1..i] 上的最大和,d[i]=(d[i-1]+a[i]>a[i])?d[i-1]+a[i]:a[i];
max = {d[i],1<=i<=n} ;
1 #include<iostream> 2 #define N 100010 3 using namespace std; 4 int a[N],d[N]; 5 int main() 6 { 7 int test,n,i,max,k,f,e; 8 cin>>test; 9 k=1; 10 while(test--) 11 { 12 cin>>n; 13 for(i=1;i<=n;i++) 14 cin>>a[i]; 15 d[1]=a[1]; 16 for(i=2;i<=n;i++) 17 { 18 if(d[i-1]<0) d[i]=a[i]; 19 else d[i]=d[i-1]+a[i]; 20 } 21 max=d[1];e=1; 22 for(i=2;i<=n;i++) 23 { 24 if(max<d[i]) 25 { 26 max=d[i];e=i; 27 } 28 } 29 int t=0; 30 f=e; 31 for(i=e;i>0;i--) 32 { 33 t=t+a[i]; 34 if(t==max) f=i; 35 } 36 cout<<"Case "<<k++<<":"<<endl<<max<<" "<<f<<" "<<e<<endl; 37 if(test) cout<<endl; 38 } 39 return 0; 40 }
改进后的只处理最大和不处理位置
1 #include<cstdio> 2 int main() 3 { 4 int n,test,ans,t,a,i; 5 scanf("%d",&test); 6 while(test--) 7 { 8 scanf("%d",&n); 9 scanf("%d",&a); 10 ans=t=a; 11 for(i=1;i<n;i++) 12 { 13 scanf("%d",&a); 14 if(t<0) t=a; 15 else t=t+a; 16 if(ans<t) ans=t; 17 } 18 printf("%d\n",ans); 19 } 20 return 0; 21 }
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6 int T; 7 scanf("%d", &T); 8 9 int N; 10 int a; 11 int ans; 12 int sum; 13 int i; 14 int bg, ed;//起始,结束 15 int bg2; 16 int cas = 0; 17 18 while (T--) { 19 scanf("%d", &N); 20 21 ans = -1010;// 22 sum = 0;// 23 bg2 = 0;//默认起始位置 24 for (i = 0; i < N; ++i) { 25 scanf("%d", &a); 26 27 sum = sum + a; 28 if (sum > ans) { 29 ans = sum; 30 bg = bg2; 31 ed = i; 32 } 33 if (sum < 0) {//< 0 那么这段到此为止吧 34 sum = 0;// 35 bg2 = i + 1;//更新起始位置 36 } 37 } 38 39 printf("Case %d:\n", ++cas); 40 printf("%d %d %d\n", ans, bg + 1, ed + 1); 41 if (T > 0) { 42 printf("\n"); 43 } 44 } 45 return 0; 46 }
这个和上面的相比写法容易理解些
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6 int T; 7 scanf("%d", &T); 8 9 int N; 10 int a; 11 int ans; 12 int sum; 13 int i; 14 int bg, ed;//起始,结束 15 int bg2; 16 int cas = 0; 17 18 while (T--) { 19 scanf("%d", &N); 20 21 scanf("%d", &a); 22 ans = a; 23 sum = a; 24 bg = 0, ed = 0; 25 bg2 = 0; 26 27 for (i = 1; i < N; ++i) { 28 scanf("%d", &a); 29 30 if (sum <= 0) {//从样例2 看,这里要<,但是<= 也可以,只要找到一个最大的子串就可以 31 sum = a; 32 bg2 = i; 33 } else { 34 sum = sum + a; 35 } 36 37 if (sum >= ans) {//这里> 和>= 都可以 38 ans = sum; 39 bg = bg2, ed = i; 40 } 41 } 42 43 printf("Case %d:\n", ++cas); 44 printf("%d %d %d\n", ans, bg + 1, ed + 1); 45 if (T > 0) { 46 printf("\n"); 47 } 48 } 49 return 0; 50 }