题目

Given a sequence of K integers { N​1​​ , N​2​​ , ..., 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

题目大意

给定一个整数序列,让找出其中 和 最大的 连续子序列,输出这个序列的和、起始元素、结尾元素。如果有多个和最大的连续子序列,输出其中开始元素和结束元素下标最小也就是最靠前)的那个子序列。如果所有整数都是负数,规定和为0,输出序列的首元素和尾元素。

思路分析

  • maxSum表示最大的子序列和,初始化为-1,在最后判断一下如果它为-1,说明全为负数,把它赋值为0
  • leftIndex表示最终子序列的第一个元素在序原列中的下标,初始化为0rightIndex表示最终子序列的最后一个元素在序原列中的下标,初始化为序列长度-1
  • 我们维护一个临时的连续子序列寻找局部最优解,从数组第一个元素开始,累加当前元素,每当它的和 > maxSum时,就用它取代全局最优(它的起点作为最终起点,它的终点(当前元素的位置)作为最终终点);每当它的和 < 0 时,就舍弃之前的元素,从下一个位置重新开始累加统计。为什么舍弃?因为它现在是负数,不管下一个元素是正是负,加起来只会变得更小,所以舍弃它重新开始。
  • 这样做法有个好处是,如果全是负数,临时序列的和永远不会大于maxSum(初始值是-1),所以就不会改变 leftIndex0) 和 rightIndex序列长度-1),是满足题意得,这样我们最终输出就不用特殊处理
  • 不太明白得话,看看代码,注释很详细,容易看懂。

代码

#include <iostream>
#include <algorithm>
using namespace std; int main() { // k个整数
int k;
cin >> k;
int num[k];
for (int i = 0; i < k; ++i)
cin >> num[i];
// 递增序列起始位置,结束位置
int leftIndex = 0, rightIndex = k - 1;
// 临时的连续和,临时连续和的起始位置,最大连续和
int tempSum = 0, tempIndex = 0, maxSum = -1;
for (int i = 0; i < k; ++i) {
// 累加临时的连续和
tempSum += num[i];
// 若小于0,则从下一个位置开始重新统计
if (tempSum < 0) {
tempSum = 0;
tempIndex = i + 1;
// 一旦这一个临时连续序列的和超过了之前的最大和
// 如果元素全为负数,这个条件不会成立,leftIndex=0,rightIndex=k-1不会被改变
} else if (tempSum > maxSum){
// 更新最大和
maxSum = tempSum;
// 临时序列的起始位置成了最终序列的起始位置
leftIndex = tempIndex;
// 当前位置(临时序列的结束位置)成为最终序列结束位置
rightIndex = i;
}
}
// 全为负数的情况下,返回累加和0
if (maxSum < 0) maxSum = 0;
// 输出最大和,连续序列的第一个数字(是值,不是位置),最后一个数字
cout << maxSum << " " << num[leftIndex] << " " << num[rightIndex]; return 0;
}

PAT 1007 Maximum Subsequence Sum (25分)的更多相关文章

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

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

  2. 1007 Maximum Subsequence Sum (25 分)

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

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

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

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

  5. 数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)

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

  6. 【PAT甲级】1007 Maximum Subsequence Sum (25 分)

    题意: 给出一个整数K(K<=10000),输入K个整数.输出最大区间和,空格,区间起点的数值,空格,区间终点的数值.如果有相同的最大区间和,输出靠前的.如果K个数全部为负,最大区间和输出0,区 ...

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

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

  9. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

随机推荐

  1. Java中Character类

    Character 类在对象中包装一个基本类型char的值此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等),并将字符从大写转小写,反之亦然. 构造方法: Character(char ...

  2. 解析一下阿里出品的泰山版 Java 开发手册

    说起华山,我就想起岳不群,不,令狐冲:说起泰山,我就想起司马迁,他的那句名言"人总有一死,或重于泰山,或轻于鸿毛",真的发人深省啊.这就意味着,阿里出品的泰山版 Java 开发手册 ...

  3. jeecg ant design vue 一些收藏

    1关于 进来清除上次记录 找到src/permission.js下的

  4. CSS躬行记(7)——合成

    在图形编辑软件中,可以按特定地方式处理不同图层的合成,最新的CSS规范也引入了该功能,并提供了mix-blend-mode和background-blend-mode两个属性.混合模式(blendin ...

  5. Zabbix磁盘性能监控

    iostat统计磁盘信息的时候,使用的是/proc/diskstats ,cat /proc/diskstats显示如下 ram0 ram1 ram2 ram3 ram4 ram5 ram6 ram7 ...

  6. Tomcat系列教材 (一)- 教程

    Tomcat系列教材 (一)- 教程 Tomcat是常见的免费的web服务器. Tomcat 这个名字的来历,Tomcat是一种野外的猫科动物,不依赖人类,独立生活. Tomcat的作者,取这个名字的 ...

  7. 关于用C-free进行C语言编程在电脑中生成的.exe和.o文件

    在使用C-free进行C语言编程时,程序运行后会自动在电脑文件中生成以.exe和.o为后缀名的文件: 1,生成的.exe文件为系统自动打包完成的应用程序,该程序可直接在其他无C-free环境的电脑上运 ...

  8. 《Android游戏开发详解》一1.7 控制流程第1部分——if和else语句

    本节书摘来异步社区<Android游戏开发详解>一书中的第1章,第1.7节,译者: 李强 责编: 陈冀康,更多章节内容可以访问云栖社区"异步社区"公众号查看. 1.7 ...

  9. 虚拟化VMware之虚拟机备份(1)

    之虚拟机备份() 模版:是一种开放,公用.安全的虚拟机压缩格式,通常使用的是扩展名为.ova可以在多个主流虚拟化平台下进行操作 是和 通过技术协作推出的基于磁盘的备份和恢复的新一代解决方案,可靠且易部 ...

  10. USACO Training Section 1.2 双重回文数 Dual Palindrom

    题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做"回文数".例如,12321就是一个回文数,而77778就不是.当然,回文数的首和尾都应是非零的,因此0220就 ...