Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 142647 Accepted Submission(s): 33192
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
Author
Ignatius.L
这道题的题目虽然是大数 但是不用大数也是能够做得出来的
题目的意思就是给你一段数字,开头还有结尾都是可以变化的,
在两者都不确定的情况下,让你求最长和的子列,并且将最
大的值,子列的起始位置,终止位置给输出来;
这是简单的dp问题
代码如下:
#include<stdio.h>
int main()
{
int n;
int max,i,j,m,start,end,k,temp,a;
scanf("%d",&n);
for(i=0;i<n;i++)//此处写成while(n--)也行 但是后面对case值的变化 要多一个变量进行描述
{
max=-1001;
end=start=k=temp=0;
scanf("%d",&m);
for(j=0;j<m;j++)
{
scanf("%d",&a);
temp+=a;//temp起到了缓存的效果,将总和的值加起来之后再与最大值比较
if(temp>max)
{
max=temp;
start=k;
end=j;
}
if(temp<0)//此处要是前面的和小于零 说明 不可能从其那面开始 所以其实位置也要发生变化
{
temp=0;
k=j+1;
}
}
printf("Case %d:\n",i+1);
printf("%d %d %d\n",max,start+1,end+1);
if(i!=n-1)
printf("\n");
}
return 0;
}
杭电 1003