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 源自http://acm.hdu.edu.cn/ 1003题 欢迎大家踊跃参与。。。 |
[技术| 编程·课件·Linux] ACM题天天练 4 Max Sum
sunhongbo
· 发布于 2012-08-23 12:05
· 6121 次阅读
转载文章时务必注明原作者及原始链接,并注明「发表于 软院网 RuanYuan.Net 」,并不得对作品进行修改。
本帖最后由 hslx111 于 2012-8-23 13:36 编辑 [C++] 纯文本查看 复制代码 //hud 1003 #include <iostream> using namespace std; int main() { int i,j,T,N; int num=1; cin>>T; while(T--) { int start,end,pos,sum,now,temp; cin>>N>>temp; now=sum=temp; start=end=pos=1; for(i=2;i<=N;i++) { cin>>temp; if(now+temp<temp) now=temp,pos=i; else now+=temp; if(now>sum) sum=now,start=pos,end=i; } cout<<"Case "<<num<<":"<<endl; cout<<sum<<" "<<start<<" "<<end<<endl; if(T!=0) cout<<endl; num++; } return 0; } 好长时间不写,手都生了啊.... |
点评
评分
本帖最后由 yel_hb 于 2012-8-23 13:58 编辑 ...看了楼上的贴自己代码有点心虚的说... [C++] 纯文本查看 复制代码 #include <iostream> using namespace std; int main() { int t,n,i,k,max,sum,x1,x2,y1; int *list; cin >> t; k = 1; while(t--) { cin >> n; list = new int[n]; for(i = 0; i < n; ++i) { cin >> list[i]; } x1 = 0; y1 = 0; x2 = 0; max = list[x1]; sum = list[x1]; for(i = x1 + 1; i < n; ++i) { if(sum >= 0) { sum += list[i]; } else { x2 = i; sum = list[i]; } if(max < sum) { max = sum; x1 = x2; y1 = i; } } cout << "Case " << k++ << ":" << endl; cout << max << " " << (x1 + 1) << " " << (y1 + 1) << endl; if(t != 0) { cout << endl; } } return 0; } |
评分
本帖最后由 justcx 于 2012-8-23 14:41 编辑 [C++] 纯文本查看 复制代码 #include<iostream> #include<iostream> #define N 100000 static st,ed; int b[N]; int MaxSumDP(int *a, int n); using namespace std; int main() { int n1,n2; int a[N]; cin>>n1; for(int j=0;j<n1;j++) { cin>>n2; for(int i=0;i<n2;i++) { cin>>a[i]; } int M=MaxSumDP(a,n2); cout<<"Case :"<<j+1<<endl; cout<<M<<' '<<st<<' '<<ed<<endl; cout<<endl; } return 0; } int MaxSumDP(int *a, int n) { int i, maxSum = 0; int count=0; if(a[0] >=0) { maxSum=b[0]=a[0]; st=1; ed=1; count++; } for(i=1; i<n; ++i) { b = (b[i-1]+a > 0) ? (b[i-1]+a) : 0; if(b[i-1]==0) count++; while(count==0) { if(b[i-1]==0&&b>0) { st=i+1; } } if(maxSum < b[i]) { maxSum = b[i]; ed=i+1; } } return maxSum; } |
评分
无语,调试没问题,谁能告诉我为何一直wa? [Java] 纯文本查看 复制代码 package com.slk.MaxSumSubsequence; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t, n; int pos = 1; int spos = 1; int epos = 1; int caseNum = 1; t = Integer.parseInt(sc.nextLine()); while (t-- > 0) { n = Integer.parseInt(sc.nextLine()); int currentInput = Integer.parseInt(sc.nextLine()); int tempSum = currentInput; int maxSum = tempSum; for (int i = 1; i < n; i++) { currentInput = Integer.parseInt(sc.nextLine()); if (currentInput + tempSum < currentInput) { tempSum = currentInput; pos = i + 1; } else { tempSum += currentInput; } if (tempSum > maxSum) { maxSum = tempSum; spos = pos; epos = i + 1; } } System.out.println("Case " + caseNum++ + ":"); System.out.println(maxSum + " " + spos + " " + epos + "\n"); } sc.close(); } } |
本帖最后由 maxOrder石 于 2012-8-23 17:55 编辑 不用检测T的值和N的值吗,我就纳闷了,每次写完结果正确就是提交不了,求改,谢谢 #include<stdio.h> int main() { int T; scanf("%d",&T); if(T<1||T>20) { return 0; } int N; int i; int j; int s,MaxSum[20]={0}; int starP[20],endP[20]={0}; for(j=0;j<T;j++) { scanf("%d",&N); if(N<1||N>100000) return 0; starP[j]=1; int *number=(int *)malloc(sizeof(int)*N); for(i=0;i<N;i++) { scanf("%d",number+i); } s=0; for(i=0;i<N;i++) { s=s+*(number+i); if(MaxSum[j]<s) { MaxSum[j]=s; endP[j]=i+1; } } } for(i=0;i<T;i++) { printf("Case %d:\n",i+1); printf("%d %d %d\n\n",MaxSum,starP,endP); } return 0; } 字体变了,最后的printf(是MaxSum中括号i,后面都是一样的额) |