# HDU 1003子序列最大和

## Max Sum

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 249830 Accepted Submission(s): 59130

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

## C解法

#include <stdio.h>
int main()
{
int h;
int a[10000];//数组大小要100000
int i;
int sum;
int max;
int g=1;
int k;
int p;//p、k、m、max、sum需要在while里面赋值，因为要使用初值多次
int m;
scanf("%d",&h);
while(h--)
{
max=-1001;//这里必须要-1001
p=1;
k=1;
m=1;
sum=0;
scanf("%d",&a[0]);
for(i=1;i<=a[0];i++)
{
scanf("%d",&a[i]);
sum+=a[i];
if(sum>max)
{
max=sum;
m=p;
k=i;
}
if(sum<0)
{
sum=0;
p=i+1;
}
}
printf("Case %d:\n",g++);
printf("%d %d %d\n",max,m,k);
if(h>0)
printf("\n");
}
return 0;
}

## C解法内存优化后（不利用数组，因为数组浪费空间太大，直接用整型变量存值）

#include <stdio.h>
int main()
{
int h;
int a;//变量存值
int i;
int sum;
int max;
int g=1;
int k;
int p;//p、k、m、max、sum需要在while里面赋值，因为要使用初值多次
int m;
int n;
scanf("%d",&h);
while(h--)
{
max=-1001;//这里必须要-1001
p=1;
k=1;
m=1;
sum=0;
scanf("%d",&a);
for(i=1;i<=a;i++)
{
scanf("%d",&n);
sum+=n;
if(sum>max)
{
max=sum;
m=p;
k=i;
}
if(sum<0)
{
sum=0;
p=i+1;
}
}
printf("Case %d:\n",g++);
printf("%d %d %d\n",max,m,k);
if(h>0)
printf("\n");
}
return 0;
}

### 上面的代码都是AC的，下面是之前一直写的比较没思路也是WA的。（这段代码如果只试验原题目中的测试值是没问题的。但是用一些更大的值或者多些负值那就程序奔溃了）！！这样提醒自己作为acm小白测试代码要尽量找多一些值来测试！

#include <stdio.h>
int main()
{
int h;
scanf("%d",&h);
while(h--)
{
int a[100000];
int j=0;
int l,p;
int m,q;
int sum2=0;
int max=-1001;
int g=1;
scanf("%d",&a[0]);
for(j=1;j<=a[0];j++)
{
scanf("%d",&a[j]);
}
j=1;
if(a[j]>=0)
{
m=j;
p=j;
l=j;
for(p=a[0];p>=l;p--)
{
sum2+=a[p];
if(sum2>max)
{
max=sum2;
q=a[0];
}
if(sum2<0)
{
sum2=0;
a[0]=p-1;
}

}
}
printf("Case %d:\n",g++);
printf("%d %d %d\n",max,m,q);
if(h>0)
{
printf("\n");
}
}
return 0;
}

## 总结

……

#### 7、推算（推算参考http://alorry.blog.163.com/blog/static/6472570820123801223397/）

maxsum a[0] = a[0]
maxsum a[1] = max( a[0] + a[1] ，a[1]) = max( maxsum a[0] + a[1] ，a[1])
maxsum a[2] = max( max ( a[0] + a[1] + a[2]，a[1] + a[2] )，a[2])
= max( max( a[0] + a[1] ，a[1]) + a[2] ， a[2])
= max( maxsum a[1] + a[2] ， a[2])
……

maxsum a[i] = max（ maxsum a[i-1] + a[i]，a[i])

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120