PAT Advanced 1007 Maximum Subsequence Sum
题目
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≤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
理解与算法
经典的最大子序列和的问题,不过要多求两个值:最大子序列的始末值,相当于要求出子序列的位置!
最右侧的值很容易给出,因为每一次更新最大值时都会更新这个值,不需要考虑太多。
问题在于怎么找到起始值?
这里我想了很多,后来觉得是多虑了!只需要抓住一个点就行:
当之前的子序列不可能是最大子序列的时候,更新起始值的下标(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的更多相关文章
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 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 ...
- 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 ...
- PAT 甲级 1007 Maximum Subsequence Sum
https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168 Given a sequence of K ...
- PAT 甲级 1007. Maximum Subsequence Sum (25) 【最大子串和】
题目链接 https://www.patest.cn/contests/pat-a-practise/1007 思路 最大子列和 就是 一直往后加 如果 sum < 0 就重置为 0 然后每次 ...
- python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)
python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 1007 Maximum Subsequence Sum (PAT(Advance))
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
随机推荐
- 多任务-python实现-Thread的基本使用(2.1.1)
@ 目录 1.多任务的概念 2.线程 1.多任务的概念 "多任务工作"指的是当前很普遍的工作状态,一个人同时处理多件事情,比如以下这个常见的画面:写一会报告,刷一下网页,查一下资料 ...
- Mysql技术内幕之InnoDB锁探究
自7月份换工作以来,期间一直在学习MySQL的相关知识,听了一些视频课,但是一直好奇那些讲师的知识是从哪里学习的.于是想着从书籍中找答案.毕竟一直 看视频也不是办法,不能形成自己的知识.于是想着看书汲 ...
- Java 面试知识点【背诵版 240题 约7w字】
-- 转载自牛客网 是瑶瑶公主吖 Java 基础 40 语言特性 12 Q1:Java 语言的优点? ① 平台无关性,摆脱硬件束缚,"一次编写,到处运行". ② 相对安全的内存管理 ...
- Docker实例开机启动
部署项目服务器时,为了应对停电等情况影响正常web项目的访问,会把Docker容器设置为开机自动启动. 在使用docker run启动容器时,使用--restart参数来设置: docker run ...
- 自动测试LeetCode用例方法
自动合并测试LeetCode解题方法 在leetcode.com上答题,Run Code或者Sumbmit通常要Spending一会,如果提交一次就Accepted那还好,如果反复Wrong Answ ...
- python简单爬去前程无忧信息招聘
import sys reload(sys) sys.setdefaultencoding('utf-8') import requests import csv from BeautifulSoup ...
- PSO 粒子群算法
注:本人参考http://www.cnblogs.com/tiandsp/category/348031.html来实现的 算法步骤: 1.首先确定粒子个数与迭代次数. 2.对每个粒子随机初始化位置与 ...
- NET 5 Execl导入数据处理(EppLus、NPOI)
先来简单介绍下市面上最广泛常见的三种操作excel库的优缺点1.NPOI 优点:免费开源,无需装Office即可操作excel, 支持处理的文件格式包括xls, xlsx, docx.格式 缺点:不支 ...
- stm32之定时器彻底研究
分类: C/C++ 这里介绍两种方式使用stm32的定时器:直接操作寄存器和使用st的官方的库文件. 相比较而言,直接操作定时器比较简洁,对着寄存器看十分明了.而使用库文件有一点晕头转向. (个人观点 ...
- hadoop伪分布式平台组件搭建
第一部分:系统基础配置 系统基础配置中主完成了安装大数据环境之前的基础配置,如防火墙配置和安装MySQL.JDK安装等 第一步:关闭防火墙 Hadoop与其他组件的服务需要通过端口进行通信,防火墙的存 ...