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; }
相关文章推荐
- hdu~1003(简单dp)
- hdu 1003
- HDU 1003 Max Sum
- HDU 1003-Max Sum
- DP Problem A:Max Sum(HDU 1003)
- hdu1003 dp(最大子段和)
- 2017ccps网络赛 1003 Friend-Graph(暴力)HDU 6152
- 2016多校联合训练赛 第六场1003 A Simple Nim hdu 5795
- HDU 1003 Max Sum
- hdu 1003 Max Sum
- HDU 1003 - Max Sum
- HDU 1003 Max Sum
- HDU 5806 NanoApe Loves Sequence Ⅱ(尺取+思维)——BestCoder Round #86 1003
- HDU 1003(Max Sum)解题纠错
- HDU--1003 -- Max Sum [dp]
- HDU 1003 Max Sum(dp)
- sdut-1351-Max Sum-hdu-1003
- HDU 1003 联机算法
- HDU 1003 Max Sum
- HDU 1003 Max Sum 简单DP