前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【PAT甲级】Maximum Subsequence Sum

【PAT甲级】Maximum Subsequence Sum

作者头像
喜欢ctrl的cxk
发布2019-11-08 15:44:48
4100
发布2019-11-08 15:44:48
举报
文章被收录于专栏:Don的成长史

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/90111462

Problem Description:

Given a sequence of K integers {

N_{1}
N_{1}

​​,

N_{2}
N_{2}

​​, ...,

N_{k}
N_{k}

}. A continuous subsequence is defined to be {

N_{i}
N_{i}

,

N_{i+1}
N_{i+1}

, ...,

N_{j}
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:

代码语言:javascript
复制
10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

代码语言:javascript
复制
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分代码:

代码语言:javascript
复制
#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代码:

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/05/11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
  • 解题思路:
  • AC代码:22分代码:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档