1007 Maximum Subsequence Sum (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. 题意:
      求连续区间最大和,并输出这个区间左右端点的值
  3. 题解:
      1、模拟,求出每一个正数的连续区间和,更新最大区间值并记录左右端点的值
  1.   2dp[i]表示区间以a[i]结尾的区间和,转移方程为dp[i]=max(dp[i],dp[i-1]+a[i]),并更新区间下标
  1. 注意:
      1、当a[i]全为负值的时候,输出0 a[0] a[n-1]
      2
      当输入为:
      5
      -1 -1 0 -1 -1
      输出为:0 0 0
     
    代码一(模拟)
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<vector>
  4. #include<cstring>
  5. #include<stack>
  6. #include<algorithm>
  7. #include<map>
  8. #define MAX 1000000
  9. #define ll long long
  10. using namespace std;
  11. int a[];
  12. int main()
  13. {
  14. int n;
  15. cin>>n;
  16. for(int i=;i<n;i++)
  17. cin>>a[i];
  18. int ans=-,l=,r=n-;
  19. int temp=,first=;
  20. for(int i=;i<n;i++)
  21. {
  22. temp=temp+a[i];
  23. if(temp<)
  24. {
  25. temp=;
  26. first=i+;
  27. }
  28. else if(temp>ans)
  29. {
  30. ans=temp;
  31. l=first;
  32. r=i;
  33. }
  34. }
  35. if(ans<)
  36. ans=;
  37. cout<<ans<<' '<<a[l]<<' '<<a[r]<<endl;
  38. return ;
  39. }

  1. 代码二(dp
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<vector>
  4. #include<cstring>
  5. #include<stack>
  6. #include<algorithm>
  7. #include<map>
  8. #define MAX 1000000
  9. #define ll long long
  10. using namespace std;
  11. int a[],l[],r[];
  12. int dp[];//以a[i]结尾的最大连续区间和
  13. int main()
  14. {
  15. int n;
  16. cin>>n;
  17. for(int i=;i<n;i++)
  18. {
  19. cin>>a[i];
  20. dp[i]=a[i];//初始化
  21. }
  22.  
  23. l[]=r[]=;
  24.  
  25. for(int i=;i<n;i++)
  26. {
  27. if(dp[i-]+a[i]>=dp[i])//如果a[i]>=0
  28. {
  29. dp[i]=dp[i-]+a[i];
  30. l[i]=l[i-];//区间左端点不变,右端点更新
  31. r[i]=i;
  32. }
  33. else//如果a[i]是负数,新建一个区间
  34. {
  35. l[i]=i;
  36. r[i]=i;
  37. }
  38. }
  39. int ans=-,pos=,flag=;
  40. for(int i=;i<n;i++)
  41. {
  42. if(dp[i]>ans)
  43. {
  44. flag=;
  45. ans=dp[i];
  46. pos=i;
  47. }
  48. }
  49. //cout<<flag<<endl;
  50. if(flag==)
  51. cout<<<<' '<<a[]<<' '<<a[n-]<<endl;
  52. else
  53. cout<<ans<<' '<<a[l[pos]]<<' '<<a[r[pos]]<<endl;
  54. return ;
  55. }
  1.  

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

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

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

  4. PAT 1007 Maximum Subsequence Sum (25分)

    题目 Given a sequence of K integers { N​1​​ , N​2​​ , ..., N​K​​ }. A continuous subsequence is define ...

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

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

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

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

  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. PTA 01-复杂度2 Maximum Subsequence Sum (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum   (25分) Given ...

随机推荐

  1. Java - Java IO 示例

    1. 概述 面试偶尔会问到让你写 文件读写 的面试官 我搞不懂为啥必须会这玩意 面试官的意思是, 这玩意只要是个开发就得会 当然我现在又不是开发 一些概念 读与写 参照 以 进行读写行为的程序 作为参 ...

  2. Python - CentOS 下 yum 安装 python3

    1. 概述 CentOS 7 自带 python2(python 以下正文简写为 py, 命令行中依然是 python) 尝试用 yum 安装 py3 2. 环境 os centos7 3. 步骤 1 ...

  3. python操作oracle完整教程

    1.    连接对象 操作数据库之前,首先要建立数据库连接.有下面几个方法进行连接. >>>import cx_Oracle>>>db = cx_Oracle.co ...

  4. css 溢出滚动条显示,修改滚动条样式

    文本或者内容溢出滚动条显示: a/横纵都出滚动条:css添加属性{overflow:auto;} b/横向滚动条:css添加属性{overflow-x:auto;} c/纵向滚动条:css添加属性{o ...

  5. Steam游戏《Nine Parchments(九张羊皮纸)》修改器制作-[先使用CE写,之后有时间的话改用C#](2020年寒假小目标02)

    日期:2020.01.09 博客期:122 星期四 [温馨提示]: 只是想要修改器的网友,可以直接点击此链接下载: 只是想要部分CT文件的网友,可以直接点击此链接下载: 没有博客园账号的网友,可以将页 ...

  6. 2020 i春秋新春战疫公益赛 misc

    0x01 code_in_morse morse decode后得到: RFIE4RYNBINAUAAAAAGUSSCEKIAAAAEUAAAAA7AIAYAAAAEPFOMTWAAABANUSRCB ...

  7. Plastic Bottle Manufacturer Profile: Plastic Bottle Forming Process

    Plastic bottle molding refers to the process of making a final plastic product from a polymer produc ...

  8. 循环select和取赋值

    bootstrap: <!--列表容器--> <div class="panel-body" style="position:relative;&quo ...

  9. 吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:LSTM网络层详解及其应用

    from keras.layers import LSTM model = Sequential() model.add(embedding_layer) model.add(LSTM(32)) #当 ...

  10. static静态变量使用@Value注入方式

    @Componentpublic class MyConfig { private static String env; public static String getEnv() { return ...