首页 > 编程语言 >PAT-甲级-1007 Maximum Subsequence Sum C++

PAT-甲级-1007 Maximum Subsequence Sum C++

时间:2023-07-13 12:44:54浏览次数:36  
标签:PAT seq int Sum Maximum lastSum subsequence tempSum numbers

Given a sequence of K integers { N1​, N2​, ..., N​K }. 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

题意:

求一个数列的最长连续子序列和,输出序列和大小,和起始位置

分析:

很容易想到,需要用一个tempSum来记录临时的子序列和,用lastSum来记录之前的子序列和,遍历数组,当tempSum<0时,需要抛弃之前的子序列,令tempSum重新为0,tempIndex = i + 1;当tempSum≥0时,则可以分为两种情况,第一种是当前子序列和tempSum大于之前的子序列和lastSum,此时需要更新left和right的指向位置,并更新lastSum。因为这时有更大的子序列和出现了,所以right指向当前数组的指向i,而left更新为第一个使子序列和≥0的位置,即tempIndex。若tempSum ≤ lastSum,但是这时tempSum依然≥0,后面可能出现一个更大的正数,来使tempSum > lastSum,所以继续遍历,如果之后没有出现tempSum > lastSum的情况,比如 [1, 2, 3, 4, -5, -2],这时因为最后一次更新left和right是在i指向4的位置,后面均不用更新,因此这种情况不需要处理,只用继续遍历即可。综上,最长连续子序列和一次遍历可以解决问题。

代码:

//
// Created by yaodong on 2023/7/12.
//
#include "iostream"


int main() {
    int n;
    std::cin >> n;
    int seq[n];
    int temp;
    for (int i = 0; i < n; ++i) {
        std::cin >> temp;
        seq[i] = temp;
    }

    int left = 0, tempSum = 0, right = n - 1, lastSum = -1;
    int tempIndex = 0;
    for (int i = 0; i < n; ++i) {
        tempSum += seq[i];
        if(tempSum < 0){
            tempSum = 0;
            tempIndex = i + 1;
        }else if(tempSum > lastSum){
            lastSum = tempSum;
            left = tempIndex;
            right = i;
        }
    }
    if(lastSum < 0) lastSum = 0;
    printf("%d %d %d", lastSum, seq[left], seq[right]);
}

 

标签:PAT,seq,int,Sum,Maximum,lastSum,subsequence,tempSum,numbers
From: https://www.cnblogs.com/langweixianszu/p/17550159.html

相关文章

  • Nginx:client_body_temp_path 指令的上传文件测试
    结论硬盘必须要有上传文件3倍大小的剩余空间。否则会报错“nospaceleftondevice”。需要注意,这3份数据都会写到硬盘。大文件上传,实时观察硬盘剩余空间watch-n0.1"df-hm/",会看到很大的波动。默认临时文件路径文档Syntax: client_body_temp_pathpath[level1[lev......
  • [LeetCode] 2542. Maximum Subsequence Score
    Youaregiventwo 0-indexed integerarrays nums1 and nums2 ofequallength n andapositiveinteger k.Youmustchoosea subsequence ofindicesfrom nums1 oflength k.Forchosenindices i0, i1,..., ik-1,your score isdefinedas:Thes......
  • [论文速览] Hard Patches Mining for Masked Image Modeling
    Pretitle:HardPatchesMiningforMaskedImageModelingaccepted:CVPR2023paper:https://arxiv.org/abs/2304.05919code:https://github.com/Haochen-Wang409/HPMref:CVPR2023|挖掘困难样本的MIM框架:HardPatchesMiningforMaskedImageModeling关键词:MIM......
  • resume points
    多任务模型:1.平行依赖-软共享:MMOE\PLE,顺序依赖-硬共享:ESMM\ESM2\AITM2.MMOE就是embedding+多个专家网络,每个tower都有个gate控制专家网络的输出。3.PLE多层特征抽取层,每层特征抽取层又分为共享专家网络和任务专家网络。4.ESMM硬共享embedding后配合概率联乘构建tower(ctr......
  • UESTC 2023 Summer Training #02 Div.2
    Preface都给我丑完了这怎么办啊,被血虐了苦路西这场本来前面感觉都还可以,但是当中期看了眼C的题意后准备开C后就不对劲了起来最后1h扔掉一直T的C题去做H,结果因为被卡自然溢出的Hash一直挂到比赛结束,直接红温感觉这场策略问题挺大的,比如没有跟榜去写更加简单的E题(比赛的时候题......
  • 使用selenium、xpath、半自动点赞、自动登录
    selenium等待元素加载#程序执行速度很快---》获取标签---》标签还没加载好---》直接去拿会报错#显示等待:当你要找一个标签的时候,给它单独加等待时间#隐士等待:只要写一行,代码中查找标签,如果标签没加载好,会自动等待 bro.implicitly_wait(10)selenium元素操作#输入框输......
  • AT_agc062_c [AGC062C] Mex of Subset Sum 思维妙妙题--zhengjun
    思路比较巧妙。首先排序。考虑目前维护出\(a_{1\simi}\)不能表示的数的集合\(S\)。考虑如何加入\(a_{i+1}\)。如果当前\(<a_i\)的数足够了,直接输出(因为这些数将会一直留在\(S\)中)。记\(sum=\sum\limits_{j=1}^{i}a_j\)。\(a_{i+1}>sum\)\[S'=S\cup[sum+1,a_{i+......
  • selenium、xpath、打码平台
    目录1selenium等待元素加载2selenium元素操作3selenium执行js4selenium切换选项卡5selenium前进后退,异常处理6selenium登录cnblogs7抽屉半自动点赞8xpath使用9动作链10自动登录1230611打码平台12打码平台自动登录1selenium等待元素加载#程序执行速度很快---》获取......
  • xpath使用
    ###xpath使用```python页面中定位元素(标签),两种通用方式# -css选择器#-xpath:XPath即为XML路径语言(XMLPathLanguage),它是一种用来确定XML文档中某部分位置的语言#xpath语法#div 选取div标签#/ 从根节点选取#// 从匹配选择的当前节点选择文档中......
  • cksum
    cksum检查文件的CRC是否正确补充说明cksum命令是检查文件的CRC是否正确,确保文件从一个系统传输到另一个系统的过程中不被损坏。这种方法要求校验和在源系统中被计算出来,在目的系统中又被计算一次,两个数字进行比较,如果校验和相等,则该文件被认为是正确传输了。注意:CRC是指一种......