题目

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. 闭关修炼180天--手写持久层框架(mybatis简易版)

    闭关修炼180天--手写持久层框架(mybatis简易版) 抛砖引玉 首先先看一段传统的JDBC编码的代码实现: //传统的JDBC实现 public static void main(String[ ...

  2. C# AutoMapper:流行的对象映射框架,可减少大量硬编码,很小巧灵活,性能表现也可接受。

    AutoMapper 是一个对象-对象映射器,可以将一个对象映射到另一个对象. 官网地址:http://automapper.org/ 官方文档:https://docs.automapper.org ...

  3. jquery ajax return不起作用

    jquery的ajax方法:在success中使用return:来结束程序的时候,结束的只是success这个方法,也就是说success中的return的作用范围只是success: 如果要想在su ...

  4. [.NET] - OleDb读取CSV文件:使用指定的分隔符号

    今天在用OleDb方式读取一个CSV文件的时候,发现得到的文本不是通常用逗号隔开的.而是用Tab制表符来隔开的. OrderID OrderName 1 1 2 2 3 3 然后去MSND查询了了下发 ...

  5. 建议收藏!利用Spring解决循环依赖,深入源码给你讲明白!

    前置知识 只有单例模式下的bean会通过三级缓存提前暴露来解决循环依赖的问题.而非单例的bean每次获取都会重新创建,并不会放入三级缓存,所以多实例的bean循环依赖问题不能解决. 首先需要明白处于各 ...

  6. Mac Arduino ESP8266 ESP32 搭建开发环境

    目录 1.安装Arduino 2.搭建开发板管理器 3.可能出现的错误 1.安装Arduino Arduino下载. 官方下载地址:Arduino官方网站 Arduino中文社区:下载地址 安装方式: ...

  7. 出现org.apache.ibatis.binding.BindingException异常

    出现绑定式异常 查看target文件夹里面再mapper中,发现运行时缺少xml文件 解决办法 1.将xml文件复制到target中Mapper文件夹下面. 2.将xml放到resource目录下 3 ...

  8. 2020安徽程序设计省赛 G序列游戏

    2020安徽程序设计省赛 G序列游戏 有一个序列w,初始为空.再给出一个长度为m 单调递增的序列a.你需要对序列w 作如下n 次操作: (1)操作0,在序列尾部添加数字0. (2)操作1,在序列尾部添 ...

  9. WPF 关于拖拽打开文件的注意事项

    由于开发需求,需要开发一个类似Win图片浏览的工具 当然也涉及到了拖拽打开的需求 按照固有思路: <Grid x:Name="grid1" AllowDrop="T ...

  10. Mysql-Incorrect string value

    [问题描述] com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect string value: '\xF0\x9F\x8E\x8 ...