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:

  1. 10
  2. -10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

  1. 10 1 4
  2.  
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include<map>
  5. #include<stack>
  6. using namespace std;
  7. int sz[];
  8. int main()
  9. {
  10.  
  11. //freopen("1.txt","r",stdin);
  12. //就是寻找最大和序列。我之前见过这种题,不过现在忘记了。
  13. //第一次提交12分。。。到底是哪里错了?
  14. //没有最小值的没有考虑到。。
  15. int n;
  16. cin>>n;
  17. for(int i=;i<n;i++){
  18. cin>>sz[i];
  19. }
  20. int maxs=-,thiss=;
  21. int f=,e=n-,temp=;
  22. for(int i=;i<n;i++){
  23. thiss+=sz[i];
  24. if(thiss>maxs){
  25. maxs=thiss;
  26. e=i;
  27. f=temp;
  28. }else if(thiss<){
  29. thiss=;
  30. temp=i+;
  31. }//这怎么标记开始和结束啊?。。。。
  32. //cout<<thiss<<" "<<sz[i]<<" "<<f<<" "<<e<<'\n';
  33. }
  34. if(maxs<)//第二次提交加上这个之后,得分还是12;使用了flag判断之后依旧是12分。。
  35. maxs=;
  36. cout<<maxs<<" "<<sz[f]<<" "<<sz[e];
  37.  
  38. return ;
  39. }

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

  1. #include <iostream>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. int a[];
  6. int main() {
  7. int n;
  8. cin>>n;
  9. for(int i=;i<n;i++){
  10. cin>>a[i];
  11. }
  12. int thisSum=,mx=-,last=;
  13. int s=,e=,first=;
  14. for(int i=;i<n;i++){
  15. thisSum+=a[i];
  16. //需要记录起始位置,和终止位置。
  17. if(thisSum>mx){
  18. mx=thisSum;
  19. s=first;
  20. e=i;
  21. }else if(thisSum<){
  22. first=i+;
  23. thisSum=;
  24. // s=i+1;//表示从下一个数开始了。
  25. }
  26. }
  27. if(mx<){//s==n
  28. cout<<<<" "<<a[]<<" "<<a[n-];
  29. }else {
  30. cout<<mx<<" "<<a[s]<<" "<<a[e];
  31. }
  32.  
  33. return ;
  34. }

//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. <转>查看linux占用内存/CPU最多的进程

    转自 http://beginman.cn/page26/ 查使用内存最多的10个进程 ps -aux | sort -k4nr | head -n 10 或者top (然后按下M,注意大写) 查使用 ...

  2. 【Mac】安装MAMP的PHPredis扩展

    1 下载phpredis扩展安装包 cd /usr/local git clone https://github.com/nicolasff/phpredis.git 2 依次执行以下操作完成安装 $ ...

  3. Disk Genius 彻底清理硬盘空闲

  4. html css float left与 float right的使用说明(转)

    点评: CSS中很多时候会用到浮动来布局,也就是经常见到的float:left或者float:right,简单点来说,前者是左浮动(往左侧向前边的非浮动元素飘,全是飘得元素的话,就按照流式来浮动从左到 ...

  5. c++ 用new创建二维数组~创建指针数组【转】

    #include <iostream> using namespace std; void main() { //用new创建一个二维数组,有两种方法,是等价的 //一: ] = ][]; ...

  6. centos 安装 Vagrant

    使用的软件: 1. CentOS:  CentOS release 6.4 (Final) 2. Vagrant: vagrant_1.2.2_i686.rpm 3. Virtualbox: Virt ...

  7. css笔记 - 张鑫旭css课程笔记之 float 篇

    https://www.imooc.com/t/197450float float的设计初衷/原本作用-是为了实现文字环绕效果如,一个图片和一段文字垂直放置,给图片加上浮动,文字就环绕图片展示了. 浮 ...

  8. bs-loading

    根据bootstrap的modal来显示loading动效. 核心内容: 1.图片转base64方法: // 图片压缩-start function run(input_file,get_data){ ...

  9. Big Spatio temporal Data(R-tree Index and NN & RNN & Skyline)

    一.简单介绍大数据技术产物 “大数据”一词首先出现在2008年9月<Nature>杂志发表的一篇名为“Big Data: Wikiomics”的文章上(Mitch,2008).“大数据科学 ...

  10. POJ 3579 Median(二分答案)

    Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11599 Accepted: 4112 Description G ...