JNU练习赛——B

Author Avatar
patrickcty 2月 01, 2017

Problem B:Max Sum

原题链接

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

MarkDown显示不出来相应的效果,还是得看原题

个人感想

这一题其实并不难,只是我很多情况都没考虑进去,所以提交了八次都是WA。自己的考虑问题的能力还需要锻炼!

比如start不为负,比如所有数全为负这些情况。

我的思路

我错误的认为第一个数必须是正数,其实负数,零都可以。

可以用sum来判断第一个数,假设sum是从指定的第一个数到现在的和,如果和大于零则第一个数不改变,小于零就变成新输入的数。但是如果从头到尾都是负数,也就是说sum值一直在变小,这样直接改就会出错,因此start值的更新要等到新的sum比原来的max值大,这时候更新start和end值。

代码

#include<stdio.h>

int main()
{
    freopen("in.txt","r",stdin);
    int T;
    scanf("%d", &T);  // 测试的次数
    for (int i = 0; i < T; ++i)
    {
        int n;
        scanf("%d", &n);  // 序列的长度
        int sum;
        int num;
        int max;
        int pos = 1;
        int start = 1;
        int end = 1;
        scanf("%d", &num);
        max = sum = num;
        for (int j = 2; j <= n; ++j)
        {
            scanf("%d", &num);
            if (sum + num < num)  // 如果都是负数的话
            {
                pos = j;  // 开始点不能立即更新,应该和结束点一起更新,不然可能出现结束点比开始点还大的情况
                sum = num;
            } else
            {
                sum += num;
            }
            if (sum > max)
            {
                max = sum;
                start = pos;
                end = j;
            }
        }
        printf("Case %d:\n%d %d %d%s", i + 1, max, start, end, (i == (T - 1))?"\n\0":"\n\n");
    }
    return 0;
}

最后

ACM是一个团队活动,就算我的思维有局限性,但是大家一起问题就不会一直卡住了~

一个参考范例