PTA: Maximum Subsequence Sum

题目
Given a sequence of K K K integers { N 1 , N 2 , . . . , N K } \{N_1,N_2,..., N_K\} {N1,N2,...,NK}. A continuous subsequence is defined to be { N i , N i + 1 , . . . , N j } \{N_i, N_{i+1}, ..., N_j\} {Ni,Ni+1,...,Nj} where 1 ≤ i ≤ j ≤ K 1\leq i\leq j\leq K 1ijK. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence which has the largest sum of its elements. For example, given sequence { − 2 , 11 , − 4 , 13 , − 5 , − 2 } \{-2, 11, -4, 13, -5, -2\} {2,11,4,13,5,2}, its maximum subsequence is { 11 , − 4 , 13 } \{11, -4, 13\} {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 ) K(\leq 10000) K(10000). The second line contains K K 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 i i and j j j (as shown by the sample case). If all the K K 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

代码长度限制
16 KB
时间限制
200 ms
内存限制
64 MB

利用在线处理的方法

#include<stdio.h>
int main()
{
    int K;
    int a[10000];
    int ThisSum = 0;
    int MaxSum = 0;
    int LeftPosition;
    int RightPosition;
    int Left;
    scanf("%d",&K);
    for(int i=0;i<K;i++)
    {
        scanf("%d",a+i);
    }
    LeftPosition = 0;
    Left = LeftPosition;
    RightPosition = K-1;
    for(int i=0;i<K;i++)
    {
        ThisSum += a[i];
        if(ThisSum > MaxSum){
            MaxSum = ThisSum;
            Left = LeftPosition;
            RightPosition = i;
        }
        else if (ThisSum<0)
        {
            ThisSum = 0;
            LeftPosition = i+1;
        }
    }
    printf("%d %d %d",MaxSum, a[Left], a[RightPosition]);
}

提交后显示如下图:
在这里插入图片描述
经过分析,原因是当序列中仅存在0和负数时,上述算法的最大序列和输出仍然是0,但子列的输出却是整个序列的第一个和最后一个数,而题目中希望输出的是数字应该为0。

但又不能将ThisSum > > MaxSum更改为ThisSum ≥ \geq MaxSum,否则会不满足In case that the maximum subsequence is not unique, output the one with the smallest indices i and j 的条件。
方法是: 将初始最大值由0更改为-0.1,并将数据类型更改为浮点数。这样,当索引到达数字0所在的位置时,满足 0 ≥ − 0.1 0 \geq -0.1 00.1的判断条件,会更新子序列的左右位置。
但仅仅改变这些是不够的,对于全是负数的测试用例,此程序的输出是-0.1,而不是0。因此需要在最后额外加入一个判断最大值是否小于0的语句。
最终代码如下:

#include<stdio.h>
int main()
{
    int K;
    int a[10000];
    int ThisSum = 0;
    double MaxSum = -1e-1;
    int LeftPosition;
    int RightPosition;
    int Left;
    scanf("%d",&K);
    for(int i=0;i<K;i++)
    {
        scanf("%d",a+i);
    }
    LeftPosition = 0;
    Left = LeftPosition;
    RightPosition = K-1;
    for(int i=0;i<K;i++)
    {
        ThisSum += a[i];
        if(ThisSum > MaxSum){
            MaxSum = ThisSum;
            Left = LeftPosition;
            RightPosition = i;
        }
        else if (ThisSum<0)
        {
            ThisSum = 0;
            LeftPosition = i+1;
        }
    }
    if(MaxSum<0)MaxSum=0;
    printf("%d %d %d",(int)MaxSum, a[Left], a[RightPosition]);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

forever_fly_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值