[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... 阅读详情

相关推荐

【C++】auto关键字(C++11,超详细解析,小白必看系列)

本文详细解析了C++中的“auto”关键字,介绍了其定义、基本用法、限制和高级用法,特别是与范围for循环和Lambda表达式结合的应用。

m0_73494049的博客 6万+

1125 子串与子列

找出最短子列

Moliay的博客 422

【微信小程序开发与设计】垃圾智能分类小程序

垃圾分类是将来生活可持续发展的必需项。但是很多人由于缺乏垃圾分类知识,或是因为垃圾分类困难,费时费力,所以没有好好贯彻垃圾分类政策,此时设计一款方便于群众进行智能化垃圾识别分类的微信小程序就显得尤为必要。

Markelle_Fultz的博客 8246

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

Synopys 验证系列-EDA 总结

VC Formal 解决方案包括一整套正式应用程序 (App),包括属性验证 (FPV)、自动提取属性 (AEP)、覆盖分析器 (FCA)、连接性检查 (CC)、时序等效性检查 (SEQ)、寄存器验证 (FRV)、测试平台分析仪 (FTA)、形式导航器 (NAV) 以及用于验证标准总线协议的一组断言 IP (AIP)。

qq_41289491的博客 1428

【SLG游戏服务器开发手册】(ROK Like)沙盘地图篇一,菱形瓦片与笛卡尔坐标系的转换(附调试工具)

轴向坐标:以菱形网格对角方向为基准轴,记作(q, r)菱形瓦片宽度 w=2a,高度 h=a(宽高比2:1)

代码时光 983

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

Spring Boot 3.5.4 + LangChain4j实战:用Milvus搭建企业级RAG问答系统(附阿里百炼API配置)

本文详细介绍了如何利用Spring Boot 3.5.4、LangChain4j与Milvus构建企业级RAG问答系统。内容涵盖从架构设计、阿里百炼API安全配置、Milvus部署优化,到RAG检索、会话管理等核心实现,并提供了生产级API设计、监控集成及性能优化等实战经验,旨在帮助开发者打造高可用、可维护的Java AI应用。

weixin_29054399的博客 610

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

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

吃货小跟班的博客 2144

详解从无人机倾斜实景三维建模→模型修复→成果网络发布展示分享全流程

1、倾斜实景三维建模软件:CC(Smart3D)软件2、实景三维模型网页在线发布展示、分享平台:图新地球网页端(Wish3D·Earth平台)图新地球网页端(Wish3D·Earth平台)是基于 WebGL技术研发的国产实景三维模型Web端发布展示平台,可实现TB级倾斜实景三维数据快速加载,支持多种格式的三维模型,如OSGB实景模型、OBJ人工模型、SKP人工模型、RVT格式BIM模型等,同时支持模型单体化、三维模型测量分析、水面特效、日照分析等特殊效果呈现,此外也可以根据您的需要进行本地化部署级功能定制。

GISWorld3D的博客 4379

OpenCV教程:超详细的OpenCV入门教程,值得收藏

OpenCV(Open Source Computer Vision Library)是一个开源的计算机视觉库,它提供了很多函数,这些函数非常高效地实现了计算机视觉算法(最基本的滤波到高级的物体检测皆有涵盖)。 OpenCV 使用 C/C++ 开发,同时也提供了 Python、Java、MATLAB 等其他语言的接口。 OpenCV 是跨平台的,可以在Windows、Linux、Mac OS、Android、iOS 等操作系统上运行。 OpenCV 的应用领域非常广泛,包括图像拼接、图像降噪、产品质检、..

pythonandaiot的博客 14万+

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

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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万+

visio安装包

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

Devexpress16.2.6破解工具

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

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

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

qq_35624947的博客 935

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

【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、付费专栏及课程。

余额充值