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
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int maxn = ;
int s[maxn] = { };
int res[maxn] = { };
int main(){
int n;
scanf("%d", &n);
for (int i = ; i<n; i++){
scanf("%d", &s[i]);
}
res[] = s[];
for (int i = ; i<n; i++){
res[i] = max(s[i], res[i - ] + s[i]);
}
/*for (int i = 1; i < n; i++){
if (res[i - 1] + s[i] < 0)res[i] = s[i];
else res[i] = res[i - 1] + s[i];
}*/
int maxi = ;
for (int i = ; i<n; i++){
if (res[i]>res[maxi]){
maxi = i;
}
}
if (maxi == && s[]<)printf("0 %d %d", s[], s[n - ]);
else{
printf("%d ", res[maxi]);
int mini = maxi;
int sum = ;
do{
sum += s[mini--]; } while (sum != res[maxi]);
mini++;
printf("%d %d", s[mini], s[maxi]);
}
system("pause");
}

注意点:又是最大子列和问题,不仅要最大子列和答案,还要输出子列的首尾数字。知道可以用动态规划做,做了半天答案一直错误,发现是动态规划想错了,不是一小于0就把值赋给自己,而是要取(自己)和(自己加上前面的最大子列和)的最大值。后面找索引时要用do...while,否则最大子列和只有自身一个数的时候会出错。

PAT A1007 Maximum Subsequence Sum (25 分)——最大子列和,动态规划的更多相关文章

  1. PAT 1007 Maximum Subsequence Sum (25分)

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

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

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

  4. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

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

  5. 1007 Maximum Subsequence Sum (25 分)

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

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

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

  7. PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)

    1​​, N2N_2N​2​​, ..., NKN_KN​K​​ }. A continuous subsequence is defined to be { NiN_iN​i​​, Ni+1N_{i ...

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

  9. 01-复杂度2 Maximum Subsequence Sum (25 分)

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

随机推荐

  1. ORACLE查看数据库已安装补丁

    cd $ORACLE_HOME ./opatch lsinventory :}

  2. Python 进阶必备函数

    1. lambda 表达式 匿名函数(英语:anonymous function)是指一类无需定义标识符(函数名)的函数.通俗来说呢,就是它可以让我们的函数,可以不需要函数名. 正常情况下,我们定义一 ...

  3. js之展开收缩菜单,用到window.onload ,onclick,

    目标效果:点击标签1,如果列表标签的style的display是block,改成none,否则改成block,来达到展开收缩菜单效果 一.准备阶段 html文件 <!DOCTYPE html&g ...

  4. js-权威指南学习笔记20

    第二十章 客户端存储 1.客户端存储有一下几种形式:Web存储.cookie.IE userData.离线Web应用.Web数据库.文件系统API. 2.Web存储标准所描述的API包含localSt ...

  5. JetBrains PhpStorm 2017.2 x64 激活

    使用方法:激活时选择License server 填入http://idea.imsxm.com 点击Active即可

  6. AWS CSAA -- 04 AWS Object Storage and CDN - S3 Glacier and CloudFront(四)

    026 S3 Summary

  7. ABP(ASP.NET Boilerplate Project)框架探讨

    从官网上下载下来带Module-Zero的abp框架. vs2015打开解决方案. 首先让系统run起来.把webconfig数据库连接改一下.启动程序. 发现报错:“本地语言指定”的错误,之后运行n ...

  8. SDN 第四次作业

    阅读 了解SDN控制器的发展 http://www.sdnlab.com/13306.html http://www.docin.com/p-1536626509.html 了解ryu控制器 http ...

  9. 团队作业——Alpha冲刺 12/12

    团队作业--Alpha冲刺 冲刺任务安排 杨光海天 今日任务:自定义保存界面布局以及交互接口函数的实现 明日任务:总结项目中的问题,为什么没能按照预期推进项目 郭剑南 今日任务:继续解决Python编 ...

  10. jdk1.7环境配置

    JDK1.7的环境配置(我的是jdk1.7,文件名写快了,忽略忽略) 官网下载自己需要的版本(ps:我这是朋友发给我的就不提供官网地址,去百度搜jdk就可以了) 下载下来除了改存放路径还有记得再jdk ...