Maximum Subsequence Sum

  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. 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 (≤). 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

题目解析

  本题第一行给出数组长度k,下一行给出k个整数,要求计算出最大连续子串和,并输出最大连续字串的首位与末位,如果整个字符串全为负数,则输出0并输出整个字符串的首位与末位。

  用数组arr记录给出的数组,最大连续子串和,果断dp。用数组dp[i]记录当字符串以第i位结尾时的最大连续子串和。当i = 0时以首位为结尾的最大连续子串和为首位arr[ i ]的值。

之后的值只会有两种情况:

  1)以当前位i为结尾时最大连续子串只有其本身arr[ i ]一个元素;

  2)以当前位i为结尾时最大连续子串有多个元素,即dp[ i - 1] + arr[ i ];

  可以得到状态转移方程dp[ i ] = max(arr[ i ], dp[ i - 1] + arr[ i ]);

  至于最大连续子串的首尾两位我们可以在计算dp数组时一并计算,以bg[ i ]记录dp[ i ]对应的最大连续子串的首位,以ed[ i ]记录dp[ i ]对应的最大连续子串的末尾。

注意:

若不对整个字符串全为负数的情况做出处理, 测试点4会错误;

  若对整个字符串全为负数的情况做出处理了,但将字符串存在0的情况也视为全为负数处理,测试点5会错误。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAX = 1e4+;
  4. int arr[MAX];
  5. int dp[MAX], bg[MAX], ed[MAX];
  6. int main()
  7. {
  8. int k;
  9. scanf("%d", &k); //输入数组长度k
  10. bool flag = false; //flag记录数组是否全为负数
  11. for(int i = ; i < k; i++){ //输入数组
  12. scanf("%d", &arr[i]);
  13. if(arr[i] >= ) //只要存在正数或0则将flag标记位true
  14. flag = true;
  15. }
  16. dp[] = arr[]; //以首位为结尾的最大连续连续子串和为首位arr[i]的值
  17. int maxSum = arr[], index = ;
  18. //maxSum记录最大连续子串和
  19. //index记录最大连续子串的末位下标
  20. for(int i = ; i < k; i++){
  21. if(dp[i - ] + arr[i] >= arr[i]){ //情况2
  22. dp[i] = dp[i - ] + arr[i];
  23. bg[i] = bg[i - ]; //记录当前最大连续子串首位下标
  24. ed[i] = i; //记录最大连续子串末位下标
  25. }else{ //情况1
  26. dp[i] = arr[i];
  27. bg[i] = ed[i] = i;
  28. //最大连续子串首尾下标都为i
  29. }
  30. if(dp[i] > maxSum){
  31. maxSum = dp[i];
  32. index = i;
  33. }
  34. }
  35. if(flag)
  36. printf("%d %d %d\n", maxSum, arr[bg[index]], arr[ed[index]]);
  37. else
  38. printf("0 %d %d\n", arr[], arr[k - ]);
  39. return ;
  40. }

  当然本题数据量最大只有1e4,暴力的O(n^2)并不会超时,所以本题暴力依然没有问题。

暴力过一切:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAX = 1e4+;
  4. int arr[MAX], sum[MAX];
  5. int main(){
  6. int k;
  7. scanf("%d", &k);
  8. int temp = ;
  9. for(int i = ; i < k; i++){
  10. scanf("%d", &arr[i]);
  11. temp += arr[i];
  12. sum[i] = temp;
  13. }
  14. int maxSum = -, bg = , ed = k - ;
  15. for(int i = ; i < k; i++){
  16. for(int j = i; j < k; j++){
  17. if(sum[j] - sum[i] + arr[i] > maxSum){
  18. maxSum = sum[j] - sum[i] + arr[i];
  19. bg = i;
  20. ed = j;
  21. }
  22. }
  23. }
  24. if(maxSum < )
  25. maxSum = ;
  26. printf("%d %d %d\n", maxSum, arr[bg], arr[ed]);
  27. return ;
  28. }

PTA (Advanced Level) 1007 Maximum Subsequence Sum的更多相关文章

  1. PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 经典题

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  2. PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)

    简单DP. 注意:If all the K numbers are negative, then its maximum sum is defined to be 0, and you are sup ...

  3. PAT Advanced 1007 Maximum Subsequence Sum

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

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

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

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

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

  7. 1007 Maximum Subsequence Sum (PAT(Advance))

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

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

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

  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. JVM虚拟机---本地接口(我还不太会)

    转载http://www.newhua.com/2008/0328/33542_2.shtml Java本地接口(Java Native Interface (JNI))允许运行在Java虚拟机(Ja ...

  2. 冲刺博客NO.5

    今天做了什么:布局UI和效果图,学会了监听事件并销毁监听接口 SMSSDK.unregisterAllEventHandler(); 今天做的东西不多,没有遇到什么苦难

  3. [Phalcon-framework]2016-04-13_安装使用 Phalcon 框架

    1. 获取你的 PHP Version,操作系统是 x86 还是 64bit的,以及 Compiler 是什么 VC, 你可以直接同时 phpinfo() 函数获取到,如下截图: 2.  下载对应的 ...

  4. 分形之科赫(Koch)雪花

    科赫曲线是一种分形.其形态似雪花,又称科赫雪花.雪花曲线.瑞典人科赫于1904年提出了著名的“雪花”曲线,这种曲线的作法是,从一个正三角形开始,把每条边分成三等份,然后以各边的中间长度为底边.分别向外 ...

  5. SQL SERVER的锁机制(三)——概述(锁与事务隔离级别)

    五.锁与事务隔离级别 事务隔离级别简单的说,就是当激活事务时,控制事务内因SQL语句产生的锁定需要保留多入,影响范围多大,以防止多人访问时,在事务内发生数据查询的错误.设置事务隔离级别将影响整条连接. ...

  6. [C#]Dapper学习笔记

    1.安装,直接用nuget搜索Dapper就行,不过只支持框架4.5.1 2.数据库测试表 CREATE TABLE [dbo].[Student]( [ID] [bigint] NULL, ) NU ...

  7. Python3.5 学习二十四

    本节课程大纲: -------------------------------------------------------------------------------------------- ...

  8. MariaDB 单表查询与聚合(5)

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...

  9. CodeForces - 940C + CodeForces - 932B (两道比较好的模拟题)

    940C链接:http://codeforces.com/problemset/problem/940/C C. Phone Numbers time limit per test 2 seconds ...

  10. python中的三次握手以及四次挥手

    三次握手1.客户端对服务端说:我的序号是x,我要向你请求连接:(第一次握手,发送SYN包,然后进入SYN-SEND状态)2.服务端听到之后对客户端说:我的序号是y,期待你下一句序号是x+1的话(意思就 ...