Maximum Subsequence Sum (25 分)

Given a sequence of K integers A continuous subsequence is defined to be 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

code

#include <stdio.h>
#include <stdlib.h>

void maxSubsequence(int arr[],int n);

int main()
{
    int k;
    scanf("%d",&k);
    int num[k];
    for(int i=0;i<k;i++)
    {
        scanf("%d",&num[i]);
    }
    maxSubsequence(num,k);
    return 0;
}

/*求最大子列和*/
void maxSubsequence(int arr[],int n)
{
    int maxSum,thisSum,frist,last,a;//first和last分别为最大子列的第一个和最后一个数,其中a用来记录最后一个数的在数组里的下标
    maxSum = thisSum = 0;
    for(int i =0;i<n;i++)
    {
        thisSum+=arr[i];
        if(thisSum>maxSum)
        {
            last = arr[i]; //每次maxSum更新时,更新last
            a=i;
            maxSum = thisSum;
        }
        if(thisSum<0)
        {
            thisSum = 0;
        }
    }
/*当最大子列和为0时,有两种情况,全为负数,或者存在0和负数,下面对着两种情况进行判断*/
    if(maxSum==0)
    {
        for(int i =0;i<n;i++)
        {
            if(arr[i]==0)
            {
                frist = last =0 ;
                break;
            }else
            {
                frist = arr[0];
                last = arr[n-1];
            }
        }
    }else
    {
        /*当最大子列和不为0时,求最大子列的第一个数字*/
        thisSum = 0;
        for(int j=a;j>=0;j--)
        {
            thisSum += arr[j];
            if(thisSum == maxSum)
            {
                frist = arr[j];
            }
        }
    }
    printf("%d %d %d\n",maxSum,frist,last);
}
全部评论

相关推荐

兄弟们,实习都是在接各种api,该怎么包装简历
仁者伍敌:感觉我自己做小项目也是各种api啊,我要怎么包装简历
点赞 评论 收藏
分享
Java抽象带篮子:简历怎么写可以看看我发的帖子,你的第一个是实习经历吗?那怎么写的是你的第一个练手项目呢?简历写的怎么样直接投小厂面试一下就知道了
没有实习经历,还有机会进...
点赞 评论 收藏
分享
牛客84809583...:举报了
点赞 评论 收藏
分享
就前几天旅游的时候,打开抖音就经常刷到这类视频:以前是高学历学生、老师、主持人,现在做着团播、擦边主播的工作,以及那些经过精心包装的“职业转型”故事——从铺天盖地的VLOG到所谓的“04年夜场工作日记”,这些内容在初中升学、高考放榜等关键时间节点持续发酵。可以说非常直接且精准地在潜移默化地影响着心智尚未成熟的青少年,使其对特殊行业逐渐脱敏。那我就想问了:某些传播公司、平台运营者甚至某些夜场的老板,你们究竟在传递怎样的价值观?点开那些视频,评论区里也是呈现明显的两极分化:一种是​​经济下行论​​:“现在就业市场已经艰难到这种程度了吗?”​​一种是事实反驳派​​:这些创作者往往拥有名校背景,从事着...
牛客刘北:被环境教育的,为了能拿到足够的钱养活自己,不甘心也得甘心,现在的短视频传播的思想的确很扭曲,但是很明显,互联网玩上一年你就能全款提A6,但你全心全意不吃不喝工作一年未必能提A6,但是在高考中考出现这个的确很扭曲,在向大家传播“不上学,玩互联网也可以轻松年入百万”,不是人变了,是社会在变
预测一下26届秋招形势
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务