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 ...
定义一个sum用于表示当前子序列的和。 从第一个数开始处理。 把当前的数加到sum上,如果加上之后sum小于0,说明无论之后的数是什么,加上当前这个序列之后都会比原来的小,那么我们果断把sum清零,重新开始累计。 之后判断sum是否比maxSum大,如果大的话,我们更新last和maxSum。令last等于当前数的下标,maxSum等于sum。
10 1 2 0 -5 1 0 0 2 3 0 正确输出:6 1 3 如果测试点3错误,可能会输出:6 3 3 测评结果.png PYTHON K=int(input())K_list=input().split()foriinrange(0,K):K_list[i]=int(K_list[i])tem=0max_sum=0f_ind=-1fi_ind=-1l_ind=-1f_flag=Truefi_flag=Truel_flag=Truep_flag=Fal...
1007 Maximum Subsequence Sum (25 分) Given a sequence ofKintegers {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 se...
前缀和(Prefix Sum)是一种常用的算法技巧,用于高效地计算数组或序列中某个区间的元素和。通过预处理数组的前缀和,可以在O(1)的时间复杂度内计算出任意区间的元素和,而不需要每次都重新计算。 以下是一些与前缀和相关的PTA题目名称: 1. PTA习题7-6 Prefix Sum. 2. PTA习题7-7 Maximum Subsequence Sum. 3. ...
1007 Maximum Subsequence Sum (25分) 题目链接题目大意 给定一个数列,找出其具有最大和的子列 思路 首先从第一个节点开始,若为负则跳过,为正则假定位子列的头结点,用另一个指针指向子列的尾结点,并计算子列和,若大于mansun 则更新maxsum和输出的头尾节点,若和小于零,则可以确定结果必定不能包含此子列,从尾结点...
免费在线预览全文 PTA作业题集 目录 PTA作业题集 1 1001 A+B Format (20分) 1002 A+B for Polynomials (25分) 1005 Spell It Right (20分) 1006 Sign In and Sign Out (25分) 1007 Maximum Subsequence Sum (25分) 1008 Elevator (20分) 1009 Product of Polynomials (25分) 1010 Radix (25分)...
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, out...
77coming / PTA-mooc Public forked from Yuri0314/PTA-mooc Notifications Fork 0 Star 0 Files master 01-复杂度1 最大子列和问题.c 01-复杂度2 Maximum Subsequence Sum.c 01-复杂度3 二分查找.c 02-线性结构1 两个有序链表序列的合并.c 02-线性结构2 一元多项式的乘法与加法运算.c 02-线性...
1Branch 0Tags Code Folders and files Name Last commit message Last commit date Latest commit stcai update readme Apr 17, 2017 39b1a9a·Apr 17, 2017 History 8 Commits 01-MaxSequenceSum1 init project Mar 28, 2017 01-MaxSequenceSum2