PAT甲级 1007. Maximum Subsequence Sum(25分) 复杂度优化到O(n)

简介: PAT甲级 1007. Maximum Subsequence Sum(25分) 复杂度优化到O(n)

1007. Maximum Subsequence Sum(25分)


Given a sequence of K integers { N1, N2, …, N**K }. A continuous subsequence is defined to be { N**i, N**i+1, …, N**j } 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
结尾无空行
#include <iostream>
using namespace std;
int main()
{
    int N;
    cin >> N;
    int A[N];
    for (int i = 0; i < N; i++)
    {
        cin >> A[i];
    }
    // 最大子序列和:在线处理,直接累加,如果累加到当前的和为负数,置当前值或0 时间复杂度N
    int sum = 0, maxsum = -1, start = 0, end = N - 1, index = 0;
    for (int i = 0; i < N; i++)
    {
        sum += A[i];
        if (sum < 0)
        {
            index = i + 1;
            sum = 0;
        }
        else if (sum > maxsum)
        {
            maxsum = sum;
            start = index;
            end = i;
        }
    }
    if (maxsum < 0)
    {
        maxsum = 0;
    }
    cout << maxsum << " " << A[start] << " " << A[end];
    return 0;
}


时间复杂度:O ( n ) ,直接一遍遍历,更新最大值,如果 s u m < 0 ,说明前面这段对我的子序列来说,只会产生负反馈,所以直接剔除。


卡壳的主要原因是英文水平不行,又或者说没有仔细阅读题目。together with the first and the last numbers of the maximum subsequence,好好看看它的意思,求的是元素而不是索引。

目录
相关文章
|
弹性计算 Cloud Native 5G
藏经阁2023年书籍推荐
好书一起看,技术干货学习就上藏经阁。藏经阁收录内容涵盖编程语言、云原生、数据库、大数据、AI等热门技术领域,让开发者们享受阅读优质内容。这次为您带来2023年4月至2024年1月的书籍推荐。
2471 5
|
存储 Go
Golang底层原理剖析之map
Golang底层原理剖析之map
842 1
|
缓存 JavaScript 前端开发
JavaScript 的三种引入方法详解
在网页开发中,JavaScript 可通过内联、内部脚本和外部脚本三种方式引入 HTML 文件,各具适用场景。本文详解其用法并附完整示例代码,帮助开发者根据项目需求选择合适的方式,提升代码维护性与开发效率。
2033 111
|
9月前
|
人工智能 自然语言处理 定位技术
从手动催办到AI驱动:基于OpenClaw的物流开船通知自动发送最佳实践
【背景】在传统国际物流货代行业中,“开船通知”(Shipment Advice)是连接货主与代理的关键纽带。然而,传统的处理模式通常依赖操作人员手动在ERP系统提取数据、编写邮件并逐一发送。面对海量订...
|
8月前
|
人工智能 安全 测试技术
开发者的新武器:利用Claude Skill实现自动化代码审查与单元测试生成
本文详解Anthropic于2025年10月推出的Claude Skill功能,聚焦实战:用Skill自动化代码审查与单元测试。通过本地Markdown技能包(如`code-review`、`test-gen`),实现精准提示复用、Git版本管理、多维度AI审查及结构化测试生成,显著提升开发效率与代码质量。
|
存储 分布式计算 资源调度
Hadoop入门(一篇就够了)(上)
Hadoop入门(一篇就够了)(上)
1086 0
|
XML 自然语言处理 数据可视化
免费!文本表格不会总结、分析?文本表格可视化!帮你轻松解决困扰,保姆级级教程。【附Coze工作流】
本文介绍了如何利用“扣子”平台创建资源,通过可视化工作流处理并分析文档、表格、PDF等内容,帮助用户快速获取文件核心信息。内容涵盖登录、资源创建、工作流配置及运行操作,适用于多种文件格式,如CSV、Excel、PDF等。
5227 4
|
网络协议 Linux 网络安全
Centos7 配置DNS服务
该教程指导配置DNS服务器:首先通过yum安装bind和cach件,然后修改IP设置。接着,编辑/etc/named.conf,将第13行和21行的参数改为&quot;any&quot;。在/etc/named.rfc1912中更新正向和反向域名。创建正向解析文件/var/named.localhost和反向解析文件/var/named.loopback,按指定格式添加解析记录。最后,重启服务,确保防火墙关闭,通过nslookup检查DNS配置效果。
1809 1
|
前端开发
el-form-item label中的字体样式设置格式
这篇文章介绍了如何在Element UI的`el-form-item`组件中自定义`label`标签的样式,通过使用`slot`属性和内联CSS来改变字体颜色和加粗显示。
el-form-item label中的字体样式设置格式
|
Linux 开发工具
【Linux】vim中批量化注释
【Linux】vim中批量化注释
693 0
【Linux】vim中批量化注释