算法:Problem - 1003: Max Sum

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 375054
Accepted Submission(s): 90051

Problem Description【问题描述】

Given a sequence a[1],a[2],a[3]…a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
给定一个序列 a[1],a[2],a[3]…a[n],你的任务是找出和最大的子序列。比如,给的序列是 (6,-1,5,4,-7),那么这个在这个序列中和子序列的最大和为 6 + (-1) + 5 + 4 = 14。

Input【输入】

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
输入的第一行包含一个整数T(1≤T≤20),表示测试用例的数量。接下来会输入T行,每一行以数字N开始(1≤N≤100000),然后是N个整数(所有整数都在-1000到1000之间)。

Output【输出】

For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
对于每个测试用例,您应该输出两行。第一行是“Case #:”,#表示测试用例的编号。第二行包含三个整数,序列中的Max Sum,最大和的子序列的起始位置,结束位置。如果有多个结果,则输出第一个结果。每两个结果之间有一空行。

Sample Input【输入示例】

2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output【输出示例】

Case 1:
14 1 4
.
Case 2:
7 1 6

Author【作者】

Ignatius.L

Recommend【相关推荐】

We have carefully selected several similar problems for you: 1176 1087 1069 2084 1058

My Code in C++【我的C++代码】

#include<iostream>
using namespace std;

struct cell { int start, end, sum; }; //子序列标识

cell dp(int* L) {
    cell o = { 1, 1, L[1] }, max = o;
    for (o.end = 2; o.end <= L[0]; o.end++) {
    //我们用sum_{end}表示序列L中以第end个元素结尾的序列的最大子段和
    //那么有sum_{end} = max{ sum_{end - 1} + L_{end}, L{end} }
    //随着end在序列中的前进,要根据max的选择调整当前的start、sum的值
        int sum = o.sum + L[o.end];
        if (sum >= L[o.end]) o.sum = sum;
        else {
            o.start = o.end;
            o.sum = L[o.end];
        }
        if (sum >= max.sum) max = o; //记录从头到尾sum值最大的序列标识
    }
    return max;
}

int main() {
    int T, ** LL, i, j, t;
    cell max;
    cin >> T;
    LL = new int* [T];
    for (i = 0; i < T; i++) { //读入T个序列
        cin >> t;
        LL[i] = new int[t + 1];
        LL[i][0] = t;
        for (j = 1; j <= t; j++) cin >> LL[i][j];
    }
    for (i = 0; i < T; i++) { //处理测试用例并输出结果
        max = dp(LL[i]);
        cout << "Case " << i + 1 << ":" << endl << max.sum << " " << max.start << " " << max.end << endl;
        if (i < T - 1) cout << endl;
    }
    return 0;
}

Realtime Status【测试结果】

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
363699442021-07-27 23:01:46Accepted1003109MS2192K874 BC++Best1thCODER
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lcy_Knight

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

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

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

打赏作者

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

抵扣说明:

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

余额充值