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 ...
随机推荐
- 《Spring Boot 实战纪实》之关键点文档
目录 前言 (思维篇)人人都是产品经理 1.需求文档 1.1 需求管理 1.2 如何攥写需求文档 1.3 需求关键点文档 2 原型设计 2.1 缺失的逻辑 2.2 让想法跃然纸上 3 开发设计文档 3 ...
- 5.自定义view-评分控件RatingBar
1.效果 2.实现原理 1.根据分数分别画选中的星星.正常的星星 2.onTouchEvent 中获取点击.滑动的位置,修改分数,在通过invalidate() 去重新绘制 核心代码: @Overri ...
- 微信小程序--页面与组件之间如何进行信息传递和函数调用
微信小程序--页面与组件之间如何进行信息传递和函数调用 这篇文章我会以我自己开发经验从如下几个角度来讲解相关的内容 页面如何向组件传数据 组件如何向页面传数据 页面如何调用组件内的函数 组件如何调 ...
- Axis2开发webservice详解
Axis2开发webservice详解 标签: javawebserviceAxis2 2015-08-10 10:58 1827人阅读 评论(0) 收藏 举报 分类: JAVA(275) 服务器 ...
- 前台生成JSON
方法一 :在后台需要转换String - json let param = new URLSearchParams(); param.append('username', this.username) ...
- JavaScript内置可用类型
string,number,boolean,null和undefined,object,symbol(ES6新语法)
- offset()与position()的区别
jQuery中有两个获取元素位置的方法offset()和position(),两者的定义如下: offset(): 获取匹配元素在当前视口的相对偏移. 返回的对象包含两个整形属性:top 和 left ...
- JAVA的一些笔记
/*一般函数与构造函数的区别 构造函数:对象创建时,就会调用与之对应的构造函数,对对象进行初始化 一般函数:对象创建时,需要函数功能时才调用 构造函数:一个对象对象创建时,只调用一次 一般函数:对象创 ...
- Solon rpc 1.2.18 发布,突出Rpc特性
Solon 是一个微型的Java RPC开发框架.项目从2018年启动以来,参考过大量前人作品:历时两年,3500多次的commit:内核保持0.1m的身材,超高的跑分,良好的使用体验.支持:Rpc. ...
- 感谢 Gridea,让我有动力写作
1. 真的要感谢 Gridea,让我对写作产生热忱.一直有在各大博客平台输出的习惯,但是都没有持续更新.有的平台广告太多,写不下去.有的平台排版复杂,写文章1个小时,排版要2个小时.所以后面换成了静态 ...