注册 登录
编程论坛 C++教室

acm题目1003,求解

czj201610032 发布于 2016-11-04 21:41, 1063 次点击
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.

3 回复
#2
rjsp2016-11-05 14:56
有个毛毛虫,一开始在左边,头部往右爬,如果毛毛虫占据的数之和小于等于0,则将将尾部缩到头部。
以上就是遍历过程,记录每一次的和的最大值
#3
yangfrancis2016-11-05 16:48
回复 2楼 rjsp
不行的。如果数列是-1,-2,-3...最后得到的最大值不会是-1
#4
yangfrancis2016-11-05 16:51
回复 2楼 rjsp
刚才没想清楚。可以得到正确值。因为阶段性的最大值有记录的
1