IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> Problem - 1003: Max Sum -> 正文阅读

[C++知识库]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
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-28 07:31:54  更:2021-07-28 07:32:53 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/29 15:22:06-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码