1007 Maximum Subsequence Sum (25分)(一个字短!)

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 (25分)

题目

Given a sequence of K integers { N​1​​, N2​​, …, N​k }. A continuous subsequence is defined to be { Ni, 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

题意

求最大和子序列

分析

若从i到j的序列和为正,则以第j+1个元素结尾的子序列的最大和为从i到j+1的子序列的和;若从i到j的序列和为负,则以第j+1个元素结尾的子序列的最大和为从j+1到j+1的子序列的和

代码

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
 	int n, i, j = 0, m;
 	scanf("%d", &n);
 	vector<int> v(n), vv(n);
 	for (i = 0; i < n; ++i)scanf("%d", &v[i]);
 	m = vv[0] = v[0];
 	for (i = 1; i < n; i++) {
  		vv[i] = max(v[i], v[i] + vv[i - 1]);
  		if (m < vv[i]) {
   			m = vv[i];
   			j = i;
  		}
 	}
 	if (m < 0) {
  		printf("0 %d %d", v[0], v[n - 1]);
  		return 0;
 	}
 	for (i = j; i >= 0; --i) {
  		if (vv[i] < 0)break;
 	}
 	printf("%d %d %d", i < 0 ? vv[j] : vv[j], v[i + 1], v[j]);
 	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... 阅读详情

相关推荐

从KVM虚拟化到Openstack云化架构综述

目录 摘要 OpenStack的虚拟化技术 OpenStack架构介绍 Nova Swift KeyStone Neutron Glance Cinder Ceilometer Heat 摘要 这两年的工作中,接触到公有云和私有云的项目还不少。起初对于Openstack的了解,仅停留在其几大组件的交互上,如Nova、Neutron、Glance等,以及对各大云厂商功能点上...

liyunpeng229的博客 2051

1007 Maximum Subsequence Sum (25 分)【动态规划】

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 Subseq...

胡小涛的博客 770

LaTeX新手入门以及TeXlive和TeXstudio的安装使用

作为一个新手,LaTeX的了解和使用很是费了一番功夫,因此,在这里给找不到路的同志们稍微介绍一下。如有错误,敬请指正。本篇内容基于Win10系统,因此安装在Mac或者Linux上的同学可以再找找其他的介绍文章。 目录 LaTeX介绍 LaTeX环境配置 一、TeXlive下载 二、TeXlive安装 1. 安装main installer 2. 正式安装 三、TeXstudio安装...

zywhehe的博客 21万+

1007 Maximum Subsequence Sum (25 分)遇到的坑

坑1:注意“ 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.”   如果不注意这个要求,结果应该是这样的: 坑2:其实...

天啊野 8065

Ubuntu服务器通过IPV6端口转发搭建幻兽帕鲁私服实战

本文详细介绍了在Ubuntu服务器上通过IPV6端口转发搭建幻兽帕鲁私服的完整教程。针对只有公网IPV6地址的服务器环境,提供了从硬件检查、端口转发工具配置到游戏服务器安装的全流程解决方案,帮助玩家轻松实现多人联机游戏体验。

weixin_30652491的博客 490

国产麒麟系统下用Rider开发C#桌面应用:从环境配置到Avalonia UI实战

本文详细介绍了在国产麒麟系统上使用JetBrains Rider开发C#桌面应用的完整流程,重点讲解了Avalonia UI框架的实战应用。从环境配置、Rider安装到Avalonia项目创建和UI开发技巧,全面覆盖了在非Windows环境下进行C#开发的关键步骤和优化建议,助力开发者高效构建跨平台桌面应用。

weixin_27791839的博客 387

01-复杂度2 Maximum Subsequence Sum (25 分)

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 t

qq_51223278的博客 194

记一次Redis出问题及解决方案

Q1: redis服务关闭前确认数据还是在的,重启后发现一条数据都没了,key* 查也是空的 A1: 经仔细检查发现redis.conf配置文件配置的数据存储目录配置的是dir ./ 也就是相对目录,也就是说在哪个目录执行redis-server redis.config启动命令 redis数据就会存到哪个目录,同样就会加载哪个目录下的数据文件(dump.rdb),前后两次启动命令的位置不一样导致第二次启动没有加载到(dump.rdb)文件,从而导致查不出数据 Q2:springboot 集成r

u010013928的博客 3059

1007 Maximum Subsequence Sum (25 分)

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 Subsequ...

92377 241

STM32入门教程:图像处理

然后,我们使用 STM32 的 DSP 模块中的 arm_mean_u8 函数计算原始图像的平均值,并将结果存储在 meanValue 变量中。然后,我们需要编写代码配置 STM32 的外部中断输入引脚,并在中断处理函数中读取 OV7670 摄像头模块输出的图像数据。然后,我们需要编写代码配置 STM32 的 USB OTG 接口,并在回调函数中读取 USB 摄像头输出的图像数据。一、图像采集 图像采集是图像处理的第一步,我们需要将外部图像源的数据导入到 STM32 中进行处理。

2401_85625565的博客 1616

2025年华为杯中国研究生数学建模竞赛E题 高速列车轴承智能故障诊断问题 思路+代码

all_data, file_info = preprocessor.load_mat_files(file_list[:50]) # 加载前50个文件作为示例。[y_source, y_source, domain_labels], # 注意:这里使用伪标签。X_target = np.random.randn(100, X_source.shape[1], 1) # 示例数据。"""构建CNN-Transformer混合模型""""""构建Transformer模型"""

YOLO 1047

1007 Maximum Subsequence Sum (25)(25 分)

1 题目 2 解题思路 3 AC代码 1 题目 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. The ...

大山喵写博客的地方 975

dToF直方图之美_deadtime死区时间

IMX459的死区时间可以在6ns,这个时间是什么级别,车载激光雷达激光器的光脉宽一般都在4ns以上,也就是这个时间贴近去SPAD的死区时间,这就导致了pile up会非常弱,不会有太多数量的光子计数堆积,加上某些SPAD设计,对TDC有一些更深入的考量,所以不需要去校正pile up算法。Sensor层面决定了pile up的复杂程度,如果一个dToF的SPAD一致性非常好,那么pile up校正可以简单到极致,也就是一句代码的事,比如IMX611,优秀到不需要后端去考虑校正pile up。

致力于lidar应用开发 2151

【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

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

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

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

qq_35624947的博客 935

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

PAT甲级 1007 Maximum Subsequence Sum (25 分)(测试点分析)

1007Maximum Subsequence Sum(25分) 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 Maximum...

ItaLink 2755

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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 (25)

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1007 分析: (1)求最大连续子序列,最终输出最大子序列值以及子序列起始和终止元素。如果数组全为负数,则输出0以及数组的第一个和最后一个元素。 (2)一个特殊情况。如,输入的是:         4        -1 0 0 -1        那么我们输出的应该是: 0 0 0

此去经年 1814

[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的博客 3586

[PAT] 1007 Maximum Subsequence Sum (25 分)Java

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...

Simma1101的博客 274

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 subsequen...

W_Dreamland的博客 2196

1007 Maximum Subsequence Sum (25分)

PAT甲级1007 Maximum Subsequence Sum (25分) 题目: 解题思路:dp问题,用temp变量记录连续的和,当temp小于零时肯定不符合最大值题意,如果全都小于零的话,将temp赋值0,判断如果当前的temp大于sum,那么更新sum的值并且记录左右连续序列的下标~~ 下面附上AC代码: #include <iostream> #include <ve...

weixin_43795683的博客 198

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

1007 Maximum Subsequence Sum (25 分)

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 Su...

A_Bo的博客 504
上一篇: 1006 Sign In and Sign Out (25分)(一个字短!)
下一篇: 1008 Elevator (20分)(一个字短!)
大了七八个
博客等级 码龄8年 2粉丝 30原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值