Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 249830 Accepted Submission(s): 59130Problem 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 -5Sample Output
Case 1:
14 1 4Case 2:
7 1 6
思路
1、从a[1]开始逐位相加,每加一项判断是否大于max(max最初值必须是-1001)。如果相加和小于0,则前面的值都将废弃。
2、利用while(h- -)来控制测试组
3、注意格式
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;
}
总结
1、这次oj里测试代码的时候经常有Runtime Error(ACCESS_VIOLATION)或者Time Limit Exceeded,经过一点测试应该是数组越界问题、空间不够、时间超时问题
2、题目 each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).这一句非常重要代表数组开的大小要100000+,而且输入值所有整数要大于-1001
3、自己本来的一个错误思路是先把所有输入的整数相加,然后从最后一位加到前面如果有和小于0那就失效一部分。可是后来想通这样又麻烦又难理解,直接从头来简单快捷。
4、要审清楚题目,输入序列的第一个数是代表后面有几个数,将这个数一同存放在数组中比较方便
5、有些时候用while比for来控制测试组更方便while(h- -)
6、理解子序列
以a[0]结尾的子序列只有a[0]以a[1]结尾的子序列有 a[0]a[1]和a[1]
以a[2]结尾的子序列有 a[0]a[1]a[2] / a[1]a[2] / a[2]
……
以a[i]结尾的子序列有a[0]a[1]……a[i-2]a[i-1]a[i] / a[1]a[2]……a[i-2]a[i-1]a[i] / a[2]a[3]……a[i-2]a[i-1]a[i] / …… / a[i-1]a[i] / a[i]
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])