1007 Maximum Subsequence Sum (25 分)

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

Submit1:

#include <iostream>
using namespace std;

struct s {
    int begin;
    int sum;
    int end;
}maxs,temp;

int main() {
    int k,n,firstnum=0,lastnum=0;
    bool mark = true;
    maxs.sum = -1;
    scanf("%d",&k);
    for (int i=0; i<k; i++) {
        scanf("%d", &n);
        if (i==0) firstnum = n;//用来标记第一个值
        else if (i==k-1) lastnum = n;//标记最后一个值
        if (mark) {//首次则是第一个数,第二次则是前边不符合,从新计数
            temp.begin = n;
            mark = false;
        }
        temp.sum += n;//求和
        temp.end = n;//更新
        if (maxs.sum < temp.sum){
            maxs = temp;//把对应的值存入max
        } else if (temp.sum < 0){
            temp.sum = 0;
            mark = true;
        }
    }
    if (maxs.sum < 0) printf("%d %d %d",0,firstnum,lastnum);//说明筛选的temp.sum中没有大于0的值
    else printf("%d %d %d",maxs.sum,maxs.begin,maxs.end);
    return 0;
}

Submit2:

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

int main() {
    int n;
    scanf("%d",&n);
    vector<int> v(n);
    int sum=-1,temp=0,tBegin=0,tempIndex=0,tEnd=n-1;//三个下标,分别是开始,临时,和结尾
    for (int i=0; i<n; i++) {
        scanf("%d", &v[i]);
        temp = temp + v[i];
        if (temp < 0) {
            temp = 0;//重置为0
            tempIndex = i+1;//更新临时索引,放弃前边的 i 项
        } else if (sum < temp) {
            sum = temp;
            tBegin = tempIndex;//记录temp重置后开始的位置
            tEnd = i;//记录tEnd跟随i往后移动
        }
    }
    if (sum < 0) sum = 0;
    printf("%d %d %d",sum,v[tBegin],v[tEnd]);
    return 0;
}

 

参考:

柳婼-https://blog.csdn.net/liuchuo/article/details/54561626

昵称五个字-https://blog.csdn.net/a617976080/article/details/89676670

posted @ 2021-08-09 16:06  白玉神驹  阅读(20)  评论(0编辑  收藏  举报