1007 Maximum Subsequence Sum (25)(25 分)

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

#include <iostream>
#include <cstring>
#include <cstdio>
#include<map>
#include<stack>
using namespace std;
int sz[];
int main()
{ //freopen("1.txt","r",stdin);
//就是寻找最大和序列。我之前见过这种题,不过现在忘记了。
//第一次提交12分。。。到底是哪里错了?
//没有最小值的没有考虑到。。
int n;
cin>>n;
for(int i=;i<n;i++){
cin>>sz[i];
}
int maxs=-,thiss=;
int f=,e=n-,temp=;
for(int i=;i<n;i++){
thiss+=sz[i];
if(thiss>maxs){
maxs=thiss;
e=i;
f=temp;
}else if(thiss<){
thiss=;
temp=i+;
}//这怎么标记开始和结束啊?。。。。
//cout<<thiss<<" "<<sz[i]<<" "<<f<<" "<<e<<'\n';
}
if(maxs<)//第二次提交加上这个之后,得分还是12;使用了flag判断之后依旧是12分。。
maxs=;
cout<<maxs<<" "<<sz[f]<<" "<<sz[e]; return ;
}

//emmm,这个整了有2h.第一次提交12分,主要是不太会标记开始,结束很好标记。后来提交22分,还差3分。发现主要是对数据

2

0 -1 这样的,因为我的maxs初始化为0,那么就无法进行更新。所以初始化为了-1。

//最大子序列和问题,O(n)内的问题就可以解决,顺序相加,每次都比较是否比之前的最大和大;如果出现了负值和,那肯定就不能有贡献。初始化为0.此时也标记一个新的开始,如果本次开始和序列值出现了最大,那么就会更新开始位置序号。懂了。

//还要注意读题,最大序列和为负值时要输出0和n-1位置的数字,这是题目给的。

2018-9-24更:

在中国大学mooc上的浙大数据结构又遇见了这道题,还是有好几个样例不通过,最终参照:https://blog.csdn.net/yzh1994414/article/details/78070888

#include <iostream>
#include<vector>
using namespace std; int a[];
int main() {
int n;
cin>>n;
for(int i=;i<n;i++){
cin>>a[i];
}
int thisSum=,mx=-,last=;
int s=,e=,first=;
for(int i=;i<n;i++){
thisSum+=a[i];
//需要记录起始位置,和终止位置。
if(thisSum>mx){
mx=thisSum;
s=first;
e=i;
}else if(thisSum<){
first=i+;
thisSum=;
// s=i+1;//表示从下一个数开始了。
}
}
if(mx<){//s==n
cout<<<<" "<<a[]<<" "<<a[n-];
}else {
cout<<mx<<" "<<a[s]<<" "<<a[e];
} return ;
}

//AC了。

1.如何更新这个s是个问题,应该是再有另个中间变量来保存的,first每次将thisSum重置之后就保存,而且就算e变了,s再被first赋值,值也未必变。

2.关于mx初始化的问题,要初始化为一个负值才可以,如果初始化为0,那么在进行:

4
0 -1 -2 -3

这个样例时就会输出:0 0 -3

而正确答案应该是:0 0 0

这个就是负数和0的样例,会出错。

3.在对mx进行更新的时候,判断条件没有等号,不然如果有连续值相同的,那么就会变成最后一个序列了。

PAT Maximum Subsequence Sum[最大子序列和,简单dp]的更多相关文章

  1. Maximum Subsequence Sum 最大子序列和的进击之路

    本文解决最大子序列和问题,有两个题目组成,第二个题目比第一个要求多一些(其实就是要求输出子序列首尾元素). 01-复杂度1 最大子列和问题   (20分) 给定KK个整数组成的序列{ N1​​, N2 ...

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

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

  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. 1007 Maximum Subsequence Sum (PAT(Advance))

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

  5. PAT Advanced 1007 Maximum Subsequence Sum

    题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...

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

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

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

  8. 中国大学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​​ }. ...

  9. PAT1007:Maximum Subsequence Sum

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

随机推荐

  1. 【cs229-Lecture10】特征选择

    本节课要点: VC维: 模型选择算法 特征选择 vc维:个人还是不太理解.个人的感觉就是为核函数做理论依据,低维线性不可分时,映射到高维就可分,那么映射到多高呢?我把可分理解为“打散”. 参考的资料: ...

  2. truncate表恢复

    Fy_Recover_Data 2014.03.07 更新 -- 现在可以从离线文件中恢复被Truncated的数据了 2014新版本的恢复思路 与 2012的不同 2014年是离线恢复 http:/ ...

  3. vue Element动态设置el-menu导航当前选中项

    1,npm install vuex --save 2,在src下新建vuex文件夹,新建store.js文件: store.js import Vue from 'vue' import Vuex ...

  4. LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)

    题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description   姊妹篇:http://www. ...

  5. [Offer收割]编程练习赛15 B.分数调查[加权并查集]

    #1515 : 分数调查 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi的学校总共有N名学生,编号1-N.学校刚刚进行了一场全校的古诗文水平测验. 学校没有公布测 ...

  6. [APP] Android 开发笔记 004-Android常用基本控件使用说明

    TextView 文本框 EditText控件 Button 与 ImageButton ImageView RadioButton CheckBox复选框 TextView 文本框 ,用于显示文本的 ...

  7. iOS开发过程中使用Core Data应避免的十个错误

    原文出处: informit   译文出处:cocoachina Core Data是苹果针对Mac和iOS平台开发的一个框架,主要用来储存数据.对很多开发者来说,Core Data比较容易入手,但很 ...

  8. linux逻辑卷管理 (LVM)(转)

    1.什么是 LVM LVM 是逻辑盘卷管理(Logical Volume Manager)的简称,它是 Linux 环境下对磁盘分区进行管理的一种机制,LVM 是建立在硬盘和分区之上的一个逻辑层,来为 ...

  9. thinkphp实现采集功能的三种方法!

    最近在做一些数据分析,由于上网找数据比较麻烦,所以写了一个采集网站数据的方法.具体方法如下: 方法一:QueryList 个人感觉比较好用,采集详情比较不错的选择,但是采集复杂一点的列表,不好用.具体 ...

  10. Kibana在Linux上安装部署及使用说明

    Kibana安装及使用说明 Kibana是一个针对Elasticsearch的开源分析及可视化平台,用来搜索.查看交互存储在Elasticsearch索引中的数据. 官方地址:https://www. ...