Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 218298 Accepted Submission(s): 51549
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
#include<stdio.h>
#include<string.h>
int main(){
int n, i, a[100001], t = 1, m;
int thissum, maxsum,star,end,mark;
scanf("%d",&n);
while(n--){
memset(a, 0, sizeof(a));
scanf("%d",&m);
for(i = 0; i < m; i++){
scanf("%d",&a[i]);
}
thissum = 0; maxsum = a[0];
star = 1; end = 1; mark = 1;
for(i = 0; i < m; i++){
thissum += a[i];
if(thissum>maxsum){
maxsum = thissum;
star = mark;
end = i + 1;
}
if(thissum < 0){
mark = i + 2;
thissum = 0;
}
}
printf("Case %d:\n%d %d %d\n",t++,maxsum,star,end);
if(n) printf("\n");
}
return 0;
}
#include<stdio.h>
int a[100010];
int dp[100010];
int main()
{
int n,i,t;
int qq=1;
scanf("%d",&t);
while(t--)
{
int start,end,maxu,j1,j2;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
dp[0]=a[0];
j1=j2=start=end=0;
maxu=-100000;
for(i=0;i<n;i++)
{
if(dp[i-1]+a[i]>=a[i])
{
dp[i]=dp[i-1]+a[i];
end=i;
}
else
{
dp[i]=a[i];
start=i;
end=i;
}
if(maxu<dp[i]) //除非·出现比以前状态更大的和,才更新j1,j2;
{
maxu=dp[i];
j1=start;
j2=end;
}
}
printf("Case %d:\n",qq++);
printf("%d %d %d\n",maxu,j1+1,j2+1);
if(t!=0)
printf("\n");
}
}
+才开始看这道题的时候,还以为给出输出有问题呢,还是自己不细心,第一个数字是N......