1007. Maximum Subsequence Sum (25)

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } 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

题目大意:

给定一个K个整数的序列{N1, N2,…,NK }。一个连续的子序列被定义为{Ni, Ni+1,…,Nj}其中1 <= i <= j <= k=K。最大子序列是元素之和最大的连续子序列。例如,给定序列{ -2, 11, -4, 13, -5, -2 }, 它的最大子序列是{ 11, -4, 13 },最大值为20。
现在你应该找到最大和和最大子序列的第一个数和最后一个数。
输入规格:
每个测试文件包含一个测试用例。每个案例有两行。第一行包含一个正整数K(,=10000).第二行包含K个用空格隔开的数字。
输出规范:
对于每个测试用例,在一行上输出最大的和,并且输出最大子序列的第一个数字和最后一个数字。数字之间必须用一个空格隔开,在一行的末尾不允许有多余的空格。在一个案例中最大子序列可能不唯一,那么输出的是i和j最小的索引(如样例所示)。如果K个数字都是负数,那么它的最大和就是0,你应该输出整个序列的第一个数字和最后一个数字。

代码:

#include<bits/stdc++.h>
int a[10001];
int main()
{
    int n,i,sum,Max,start_index,end_index,index1,index2,num=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
       scanf("%d",&a[i]);
       if(a[i]<0)
       {
           num++;
       }
    }
    if(num==n)
    {
       printf("0 %d %d",a[0],a[n-1]);
    }
    else
    {
        Max=a[0];
    sum=a[0];
    start_index=0;
    end_index=0;
    index1=0;
    index2=n-1;
    for(i=1;i<n;i++)
    {
        if(sum>Max)
        {
            Max=sum;
            index1=start_index;
            index2=i-1;
        }
        if(sum<=0)
        {
            sum=a[i];
            start_index=i;
        }
        else
        {
            sum+=a[i];
        }
    }
    printf("%d %d %d",Max,a[index1],a[index2]);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值