(在线处理)01-复杂度2 Maximum Subsequence Sum

01-复杂度2 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

 题意:求序列的最大子段和,并且给出该子段第一个数和最后一个数的值,当有多个子段和并列时,给出第一个与最后一个数的下标之和最小的那个子段,不难知道,从前往后选择,当有多个子段的和相等时,选择第一个子段即可。当所有值为负数时,最大子段和为0,此时子段即为整个序列,当要注意最大值为0时,子段第一个数和最后一个数的值并不是所有序列的第一个数和最后一个数。

first  coding :根本不需要记录下标之和的变量,但既然是最开始的想法,那么我想就有必要记录下来,下一个代码我简化了一下。

/**
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int k;
    scanf("%d",&k);
    int a[k+1];
    int maxsum=-1,thissum=0,sumindex=1e7,sumindextemp=0;
    int start=1,stop=k,starttemp=1;
    for(int i=1;i<=k;++i)
    {
       scanf("%d",&a[i]);
       thissum+=a[i];
       sumindextemp+=i;
       if(thissum>maxsum)
       {
           if(sumindextemp<sumindex)  //如果后面的下标之和
                sumindex=sumindextemp;
           maxsum=thissum;
           start=starttemp;
           stop=i;
       }
       else if(thissum<0)
       {
           thissum=0;
           starttemp=i+1;
           sumindextemp=0;
       }
    }
    if(summax<0)
        printf("%d %d %d\n",0,a[1],a[k]);
    else
        printf("%d %d %d\n",maxsum,a[start],a[stop]);
    return 0;
}
*/

second coding :umindex=1e7,sumindextemp=0这两个记录下表的变量根本就不需要;
    一开始是我想复杂了,这就告诫我,做题之前一定要把题目看清楚,
    否则一切的努力都是白忙活一场


#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int k;
    scanf("%d",&k);
    int a[k+1];
    int maxsum=-1,thissum=0;
    int start=1,stop=k,starttemp=1;
    for(int i=1;i<=k;++i)
    {
       scanf("%d",&a[i]);
       thissum+=a[i];
       if(thissum>maxsum)
       {
           maxsum=thissum;
           start=starttemp;
           stop=i;
       }
       else if(thissum<0)
       {
           thissum=0;
           starttemp=i+1;
       }
    }
    if(maxsum<0)
        printf("%d %d %d\n",0,a[1],a[k]);
    else
        printf("%d %d %d\n",maxsum,a[start],a[stop]);
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值