其他分享
首页 > 其他分享> > 【PAT甲级】Maximum Subsequence Sum

【PAT甲级】Maximum Subsequence Sum

作者:互联网

Problem Description:

Given a sequence of K integers {N_{1}​​, N_{2}​​, ..., N_{k} }. A continuous subsequence is defined to be { N_{i}N_{i+1}, ..., N_{j}} where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

解题思路:

设最大连续子序列和sum、临时的最大连续子序列和tempsum、临时的最大连续子序列的起始下标tempindex、最大连续子序列的起始下标start、最大连续子序列的结束下标end。如果tempsum<0的话,则舍去前面的子序列 直接更新tempindex;如果tempsum>sum的话,则更新sum、start、end。最后输出最大连续子序列和sum、起始下标start和结束下标end所对应的值即可。然而我第一次提交代码的时候只有22分,有个测试用例WA啦。百思不得其解的我向数据结构群里的大佬求助,我超级膜拜的晴神说了一个测试用例"2 -1 0"。我试了一下这组测试用例,当输入"2 -1 0"时,预期输出应该是"0 0 0"才对,但我的代码实际输出的却是"0 -1 0"。表白旋风小晴天!表白晴神!实名表白胡凡巨佬!我爱死晴神啦!

我晓得哪种情况下会WA啦:当输入n个数时,若前面n-i个都为负数,且最后i个为0时,并不会执行for循环里面的if-else模块中的语句,从而导致实际输出与预期输出不符合。解决方法就是:令sum的初始值为-1,这样的话(当输入"2 -1 0"这种情况的测试用例时)就能执行else if中的语句来更新start和end,从而输出"0 0 0"。仅仅把sum初始值修改成-1是不行的,因为把sum的初始值修改成-1后,另一个测试用例会WA:若输入的数字全是负数时,最后输出的sum会是-1而不是0。所以还得针对全是负数的情况来加上一条if语句:if(sum == -1) sum = 0; 修改了这俩个地方后就能得25分啦。

AC代码:22分代码: 

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int N;
    cin >> N;
    int a[N];
    //最大连续子序列和sum、临时的最大连续子序列和tempsum、临时的最大连续子序列的起始下标tempindex、最大连续子序列的起始下标start、最大连续子序列的结束下标end
    int sum = 0,tempsum = 0,tempindex = 0, start = 0, end = N-1;
    for(int i = 0; i < N; i++)
    {
        cin >> a[i];
        tempsum += a[i];
        if(tempsum < 0)
        {
            tempsum = 0;
            tempindex = i+1;
        }
        else if(tempsum > sum)
        {
            sum = tempsum;
            start = tempindex;
            end = i;
        }
    }
    cout << sum << " " << a[start] << " " << a[end];
    return 0;
}

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int N;
    cin >> N;
    int a[N];
    //最大连续子序列和sum、临时的最大连续子序列和tempsum、临时的最大连续子序列的起始下标tempindex、最大连续子序列的起始下标start、最大连续子序列的结束下标end
    int sum = -1,tempsum = 0,tempindex = 0, start = 0, end = N-1;
    for(int i = 0; i < N; i++)
    {
        cin >> a[i];
        tempsum += a[i];
        if(tempsum < 0)
        {
            tempsum = 0;
            tempindex = i+1;
        }
        else if(tempsum > sum)
        {
            sum = tempsum;
            start = tempindex;
            end = i;
        }
    }
    if(sum == -1)
    {
        sum = 0;
    }
    cout << sum << " " << a[start] << " " << a[end];
    return 0;
}

 

标签:下标,end,int,sum,Maximum,Subsequence,tempsum,序列,PAT
来源: https://blog.csdn.net/weixin_42449444/article/details/90111462