您的位置:首页 > 其它

HDU 1003

2017-08-02 15:10 225 查看

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 251969    Accepted Submission(s): 59728

[align=left]Problem Description[/align]
Given a sequence a[1],a[2],a[3]......a
, 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.

 

[align=left]Input[/align]
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).

 

[align=left]Output[/align]
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.

 

[align=left]Sample Input[/align]

2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

 

[align=left]Sample Output[/align]

Case 1:
14 1 4

Case 2:
7 1 6
题意:求最大和的子序列,即连续的一串数字中的和最大,并且还要输出起始和结束,简单的DP这道题目感觉有点坑,我原先一直以为是不能用数组或者数组没有初始化导致的超时,后来发现是数组开的太小了,导致越界,改变了其他值,从而陷入死循环,导致超时,总算是知道为什么数组开小了会导致超时了,也不枉我超时了那么多次,贴上代码
#include<stdio.h>
#in
4000
clude<string.h>
int main()
{
int n,a[100000],i,j,MAX,t,temp,strat,end,k,count=1;
scanf("%d",&n);
while(n--)
{
memset(a,0,sizeof(a));
temp=0;
MAX=-1001;
k=0;
scanf("%d",&t);
for(i=0; i<t; i++)
{
scanf("%d",&a[i]);
temp=temp+a[i];
if(temp>MAX)
{
MAX=temp;
strat=k;
end=i;
}
if(temp<0)
{
temp=0;
k=i+1;
}
}
printf("Case %d:\n",count++);
printf("%d %d %d\n",MAX,strat+1,end+1);
if(n>=1)
{
printf("\n");
}
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: