题目

1007 Maximum Subsequence Sum (25分)

Given a sequence of K integers { N1, N2, ..., N**K }. A continuous subsequence is defined to be { N**i, N**i+1, ..., N**j } where 1≤ijK. 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

理解与算法

经典的最大子序列和的问题,不过要多求两个值:最大子序列的始末值,相当于要求出子序列的位置!

最右侧的值很容易给出,因为每一次更新最大值时都会更新这个值,不需要考虑太多。

问题在于怎么找到起始值?

这里我想了很多,后来觉得是多虑了!只需要抓住一个点就行:

当之前的子序列不可能是最大子序列的时候,更新起始值的下标(temp_index)!那么我们怎么知道这个子序列不可能是最大子序列呢?它的和小于零,出现这个特征不管后面是多大的值,只要加上前面一部分,就会比不加上这一部分要小,所以我们应该直接舍弃,进入下一个子序列的搜寻!于是我们可以把下一个搜寻的起始下标设置为i+1,这里的i为迭代变量!

因为这两个下标的更新时间点是互斥的,不可能同时更新,所以要用else if连接,否则会导致错误输出!

代码实现

#include <iostream>
#include <vector> using namespace std;
int main() {
int count;
cin >> count;
vector<int> v(count);
// 最大值默认为-1,方便后面判断有没有找到最大值
// 左右下标初始值为整个数组的左右下标!
int max = -1, temp_max = 0, temp_index = 0, left = 0, right = count - 1;
for (int i = 0; i < count; ++i) {
cin >> v[i];
temp_max += v[i];
// 如果和已经小于零了,那么再往下走肯定不会是最大子序列!
if (temp_max < 0) {
// 重置临时最大值变量
temp_max = 0;
// 然后将下标移动到下一个值
temp_index = i + 1;
} else if (temp_max > max) {
// 如果找到了一个更大的和,就记录这个最大值和左右下标,因为起伏不定所以左下标会一直变化,就使用temp_index来作为下标
max = temp_max;
left = temp_index;
right = i;
}
}
// 如果最大值小于0,那么说明没找到大于0的最大值,就将其置为0!
if (max < 0) max = 0;
cout << max << " " << v[left] << " " << v[right];
return 0;
}

PAT Advanced 1007 Maximum Subsequence Sum的更多相关文章

  1. PAT Advanced 1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  2. PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  3. PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  4. PAT 甲级 1007 Maximum Subsequence Sum

    https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168 Given a sequence of K  ...

  5. PAT 甲级 1007. Maximum Subsequence Sum (25) 【最大子串和】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1007 思路 最大子列和 就是 一直往后加 如果 sum < 0 就重置为 0 然后每次 ...

  6. python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)

    python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...

  7. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  8. 1007 Maximum Subsequence Sum (PAT(Advance))

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  9. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

    1007 Maximum Subsequence Sum (25分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

随机推荐

  1. 工具-Git与GitHub-分支管理(99.5.2)

    @ 目录 1.分支介绍 2.基本使用分支 1.查看分支 2.创建一个分支dev并切换到其上进行工作 3.在dev分支中变更已经追踪的文件,并进行提交 4. dev分支的工作完成,可以切换回master ...

  2. 从苹果BigSur官网学点东西

    从苹果BigSur官网学点东西 Awsome配色 这个 蓝紫渐变大底 + 简洁的 矩形状字块 + 粗细层次字形,看着就蛮舒服. 看看css配色: .section-hero div[data-comp ...

  3. OpenSNS后台文件上传漏铜分析

    前言 这几天正在想找个文件上传漏洞分析一波,以加深对文件上传漏洞的理解,正好看到FreeBuf的一片文章记对OpenSNS的一次代码审计,由于其只对漏洞进行复现,故在此进行代码层面的分析. 漏洞分析 ...

  4. metinfo小于v6.2.0版本SQL盲注利用脚本

    #coding=utf-8 import requests import re import sys import time #获取config_safe.php中的 key def getKey(u ...

  5. Angular *ngIf length

    Angular *ngIf length 在Angular中如何判断*ngIf Arrary的长度? 具体代码如下: result = []; <div class="kt-secti ...

  6. 解决UE4缓存使C盘膨胀的问题

    使用UE4的时候会发现C盘越来越小了,那是因为UE4引擎的缓存文件默认保存在C盘的缘故. 概述 一.出现的问题:UE4的缓存文件会导致C盘膨胀. 二.解决的方式:请严格按照下列步骤来执行.1. 更改U ...

  7. 远程桌面连接(出现身份验证错误。要求的函数不支持)这可能由于CredSSP加密Oracle修正。

    家庭版解决方案 在进行远程桌面时会遇到这种情况.对于Windows 10家庭版用户,是不支持组策略功能的.本文根据官方文档采用修改注册表的方式达到相同的目的. 1.打开注册表   win + R  键 ...

  8. springboot 启动jar正确方式

    首先需要pom.xml配置一个插件: IDEA 在右侧执行顶上m图片按钮 在command Line 中执行clean package命令 执行打包注意 打完jar包后最好解压jar查看一下META- ...

  9. WebService的开发手段

    一.WebService的开发手段 目前有关webService的开发手段有2种 1.JDK开发(jdk必须是1.6及以上版本,因为jdk是在1.6版本中引入并支持webservice开发的); 2. ...

  10. JavaCV FFmpeg采集麦克风PCM音频数据

    前阵子用一个JavaCV的FFmpeg库实现了YUV视频数据地采集,同样的采集PCM音频数据也可以采用JavaCV的FFmpeg库. 传送门:JavaCV FFmpeg采集摄像头YUV数据 首先引入 ...