[PAT甲级]1007. Maximum Subsequence Sum(求连续最大子序列和)

PAT练习题(甲级) 1007 Maximum Subsequence Sum (25分)(简要理解动态规划)(Java实现) PAT练习题 1007 Maximum Subsequence Sum (25分)题干 题干 Given a sequence of K integers { N1, N2, …, NK}. A continuous subsequence is defined to be { N​i, Ni+1, …, Nj} where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of 阅读详情

1007. Maximum Subsequence Sum

原题链接

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

题目大意:

  • 给一个数组,数组长度k<=10000,找出和最大的连续子序列
  • 输出该子序列和,开始元素,结束元素
  • 若数组全是负数,定义和为0,同时输出数组第一个元素和最后一个元素

思路:

  • sum 最大连续子序列和
  • tempsum 临时的连续子序列和
  • left 最大子序列左下标
  • right 最大子序列右下标
  • index 临时的序列左下标
  • tempsum += arr[i],当tempsum >sum,就更新sum的值、left和right的值;当tempsum小于sum且小于0时,舍弃前面的序列,重新定义tempsum和index

代码:

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

int main()
{
    int n;
    cin >> n;
    vector<int> arr(n);
    int sum=-1;//最大连续子序列和
    int tempsum=0;//临时的连续子序列和
    int left=0;//最大子序列左下标
    int right=0;//最大子序列右下标
    int index=0;//临时的序列左下标
    bool flag = true;//检验是否全是负数
    for(int i=0; i<n; i++){
        scanf("%d", &arr[i]);
        if(arr[i] >= 0)
            flag = false;
        tempsum += arr[i];
        if(tempsum > sum){
            sum = tempsum;
            left = index;
            right = i;
        }else if(tempsum < 0){//如果临时序列和已经小于sum的情况下还是负数,应该舍弃之前的序列
            index = i+1;
            tempsum = 0;
        }
    }
    if(flag){
        printf("0 %d %d", arr[0], arr[n-1]);
    }else{
        printf("%d %d %d", sum, arr[left], arr[right]);
    }
    return 0;
}
PAT-甲级-1007 Maximum Subsequence Sum 1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is defined to be { N~i~, N~i+1~, ..., N~j~ } where 1 &lt;= i &lt;= j &lt;= K... 阅读详情

相关推荐

16G显存+llama.cpp跑大模型的黄金配置指南

大语言模型(LLM)本地部署的核心瓶颈在于显存与推理效率的平衡。llama.cpp作为轻量级C/C++推理引擎,通过GGUF模型格式、量化压缩(如Q4_K_M)和CUDA底层优化,显著降低GPU资源需求。在消费级硬件中,16G显存成为兼顾模型能力(如Qwen3-4B、Llama-3-8B)、长上下文支持(4K token)与系统稳定性的关键阈值。它既规避了12G显存的容量焦虑,又绕开了24G高端卡的成本与散热负担,特别适合Windows 11原生环境下的生产级AI应用,如RAG、智能客服与文档分析。

weixin_32629853的博客 407

1125 子串与子列

找出最短子列

Moliay的博客 422

STM32学习笔记【江科协】【4-2】OLED显示屏

可以注意到接电源的GND和VCC同时和PB6、PB7号口连接,我们可以直接不初始化这两个引脚解决,因为STM32的引脚上电后,如果不初始化,默认是浮空输入的模式,在这个模式下,引脚不会输出电平,所以不会有什么影响,或者直接给PB6口输出低电平,PB7口输出高电平,用GPIO口直接给OLED供电,这个也是没问题的,因为OLED的功率很小这个也是可以驱动的,不过用GPIO口供电不是很规范,实际项目还是用电源供电。然后点击这里放大镜里面带d的图标,进入调试模式,这个界面里,

m0_74651303的博客 1595

PAT--5-1 最大子列和问题 (20分)

题目描述给定KK个整数组成的序列{ N_1N ​1 ​​ , N_2N ​2 ​​ , …, N_KN ​K ​​ },“连续子列”被定义为{ N_iN ​i ​​ , N_{i+1}N ​i+1 ​​ , …, N_jN ​j ​​ },其中 1 \le i \le j \le K1≤i≤j≤K。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2

爱敲代码的小鼠的博客 445

GX WORKS2通过交换机连接PLC的方法

三菱PLC连接方法的分享

weixin_64927621的博客 3620

天池长期赛:二手车价格预测(422方案分享)

本次比赛是一次很好的锻炼数据挖掘能力的入门赛,从EDA-数据清洗-特征工程-模型调参-模型融合,能够完整的学习到大数据的分析和挖掘,值得历练。

weixin_46685991的博客 1万+

PAT甲级1044 Shopping in Mars//二分法求子序列和

Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diamonds are taken off the

冷眼看world的博客 150

SIPVicious实战指南:从零开始掌握VoIP渗透测试(Kali Linux环境)

本文为SIPVicious实战指南,详细介绍了在Kali Linux环境下进行VoIP渗透测试的完整流程。从环境准备与工具安装开始,逐步讲解如何使用svmap扫描SIP服务器、svwar枚举PBX分机号,以及svcrack进行密码安全测试,并整合svreport生成报告。旨在帮助安全从业者从零开始,系统掌握这套经典开源工具,在授权范围内有效评估VoIP系统的安全性。

weixin_29327977的博客 541

【数据结构与算法】最大子序列和问题的求解

题目:给一个int类型的数组,就最大子序列的和 解法:一共有四种算法,算法的时间复杂度分别为: 【算法一】 结果是求一个子序列,即其实序列号和终止序列号,那我们就对所有可能的起止序列号的组合进行遍历,求和,找到最大的和以及最大和所对应的起止序列号。这个算 法包括三层循环,最内层循环求和。算法的代码如下: 【算法二】 对算法一进行改进,核心仍然是穷举

吃货小跟班的博客 2144

Python学习第1天:初识Python

Python 作为目前最流行的编程语言之一,凭借其简洁优雅的语法和强大的生态系统,成为了无数开发者的首选。本系列文章将带你从零基础入门,逐步掌握 Python 编程的核心技能。今天我们先来认识 Python,并安装好开发环境。Python(英式发音:/ˈpaɪθən/;美式发音:/ˈpaɪθɑːn/)是由荷兰人吉多·范罗苏姆(Guido von Rossum)发明的一种编程语言,是目前世界上最受欢迎和拥有最多用户的编程语言。

tigerhhzz的博客 435

把数据库事务边界握在手里:ABAP 里的 COMMIT CONNECTION 与 ROLLBACK CONNECTION 深度拆解

与表面是两条很短的语句,背后却牵动了连接管理、数据库 LUW、SAP LUW、隐式提交、游标生命周期、GTT 平台差异等一整片工程现实。把它们用对,你就能在不破坏主业务事务的前提下,实现跨库写入、审计落库、隔离型日志、数据集成落地等一系列“看似矛盾”的目标;用错了,它们也会悄无声息地制造一致性缺口,让问题在高峰期以 dump 或数据异常的方式爆出来。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1842

PAT甲级练习题-Maximum Subsequence Sum (25)-最大子序列和

1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a sequence of K integers { N1, N2, ...,

ly59782的博客 414

C# 上位机程序中的数据采集与存储技术:从采集到持久化的完整解决方案

本文系统讲解C#上位机程序如何实现设备数据采集与持久化。主要内容包括:1)多种数据采集方式(串口通信、Modbus、OPC等)及代码示例;2)详细对比不同存储技术(CSV、SQLite、关系型数据库等)的优缺点;3)提供SQLite等存储实现代码;4)推荐分层系统架构(采集-缓存-存储-可视化);5)性能优化建议(多线程采集、异步存储、批量写入等)。文章强调应根据项目需求选择合适的技术方案,构建高效稳定的数据采集存储闭环系统。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1026

1007. Maximum Subsequence Sum (25)-PAT甲级真题(最大连续子序列和、动态规划dp)

1007. 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 &lt;= i &lt;= j &lt;= K. The Maximum Subsequen...

柳婼 の blog 1万+

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

PAT 甲级题目讲解:1007《Maximum Subsequence Sum》

本文是 PAT 甲级 1007 题《Maximum Subsequence Sum》的完整题解,围绕"最大连续子序列和"这一经典问题,从题目约束分析入手,结合样例直观演示"字典序优先"规则的判断过程。核心解法采用 Kadane 算法,以线性时间动态维护当前子序列和与全局最优解,并详细拆解了"前缀和为负则重启"的关键决策逻辑。文中提供了变量说明表、分步代码实现以及完整可运行 C++ 代码,最后梳理了常见错误类型与复杂度分析,帮助读者透彻理解并安全落地该算法。

qq_35624947的博客 935

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

PAT甲级 1007 Maximum Subsequence Sum

PAT1007 Maximum Subsequence Sum Given a sequence of K integers N1,N​2​​,...,N​K{ N_1, N​_2​​ , ..., N_​K}N1​,N​2​​​,...,N​​K. A continuous subsequence is defined to be Ni,N​i+1​​,...,N​j{ N_i , N​_{i+...

旺崽的博客 2882

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

【PAT甲级】1007 Maximum Subsequence Sum (25 分)

Maximum Subsequence Sum 题目描述 Given a sequence of K integers { N​1 , N​2, …, NK }. A continuous subsequence is defined to be { N​i​ , N​i+1​​ , …, Nj​​ } where 1≤i≤j≤K. The Maximum Subsequence is the...

初见还是重逢的博客 392

【PAT甲级】 1007 Maximum Subsequence Sum

最大子序列和

lemon1219的博客 192

PAT 甲级 1007 Maximum Subsequence Sum

1007Maximum Subsequence Sum(25 point(s)) Given a sequence ofKintegers {N​1​​,N​2​​, ...,N​K​​}. A continuous subsequence is defined to be {N​i​​,N​i+1​​, ...,N​j​​} where1≤i≤j≤K. The Max...

漫浸天空的雨色的备忘录~ 971

「PAT甲级真题解析」Advanced Level 1007 Maximum Subsequence Sum

题设给定一组序列, 要求最大的子序列和. (子序列是要求数在原数字中位置连续, 而子数组不要求)。 理论上, 我们只要枚举出所有的子序列进行求和, 然后记录和最大的一组即可。

过美好生活,做高效程序员 5008

PAT 甲级 1007(Maximum Subsequence Sum)

PAT 甲级 1007题目要求翻译 题目要求 Given a sequence of K integers { N1N_1N1​, N2N_2N2​, …, NKN_KNK​ }. A continuous subsequence is defined to be {NiN_iNi​, NjN_jNj​, …, NjN_jNj​ } where 1≤i≤j≤K. The Maximum Subse...

qq_42865772的博客 187

【测试点5】1007 Maximum Subsequence Sum (25 分)

立志用最少的代码做最高效的表达 Given a sequence of K integers { N​1​​ , N​2​​ , …, 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 el

weixin_43899069的博客 2068

[PAT 甲级] 1007 Maximum Subsequence Sum (25 分)【动态规划】

1007 Maximum Subsequence Sum (25 分) 原题链接 Given a sequence of K integers { N ​​ }. A continuous subsequence is defined to be { N ​​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subseque...

cp0328的博客 3587

1007 Maximum Subsequence Sum

Given a sequence ofKintegers {N1​,N2​, ...,NK​}. A continuous subsequence is defined to be {Ni​,Ni+1​, ...,Nj​} where1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given seq...

weixin_53199925的博客 279

PAT 甲级 1007 Maximum Subsequence Sum

题目描述 从题目名称就可以看出,求和最大的子序列,第一反应是动态规划题 输入描述 输入样例 10 -10 1 2 3 4 -5 -23 3 7 -21 输出描述 For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum s...

qq_26073557的博客 209
上一篇: [PAT甲级]1006. Sign In and Sign Out (25)(找出机房开门锁门的人)
下一篇: [PAT甲级]1008. Elevator (20)(求电梯运行时间)
whl_program
博客等级 码龄11年 101粉丝 279原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值