PAT A1007 Maximum Subsequence Sum (25 分)——最大子列和,动态规划
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } 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 分)——最大子列和,动态规划的更多相关文章
- PAT 1007 Maximum Subsequence Sum (25分)
题目 Given a sequence of K integers { N1 , N2 , ..., NK }. A continuous subsequence is define ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- 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 ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)
python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...
- PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)
1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 01-复杂度2 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
随机推荐
- 在Ubuntu 13.10 中安装配置 Hadoop 2.2.0
预备条件: 1. 已安装JDK Add Hadoop Group and User $ sudo addgroup hadoop$ sudo adduser --ingroup hadoop hdus ...
- PHP 协程最简洁的讲解
协程,又称微线程,纤程.英文名Coroutine.协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B,B在执 ...
- @Schedul 中cron的命名规则
@Schedul注解的定时任务详解 1.springboot集成schedule由于Spring Schedule包含在spring-boot-starter基础模块中了,所有不需要增加额外的依赖. ...
- PeopleSoft面试题...
Q1:PS发出的邮件附件名字中中文字符乱码在哪设置? A1: 分为APP和PROCESS两个配置文件,分别在psprcs.cfg 和 psappsrv.cfg 中 SMTP Settings设置. 评 ...
- C#与java中的AES加解密互解算法
一.C#版AES加解密算法 public class AESCode { public string Key { get; set; } public string Encrypt(string va ...
- jQuery首页更换背景皮肤
昨天做了一个jQuery首页更换背景皮肤,感觉还是挺不错的,一共需要两个文件,一个是我们写的HTML文件,我们起名叫做index.html,一个是我们引入的jQuery文件,我们起名叫做jQuery. ...
- Linux CentOS 6.5 下 vsftpd ftp服务器搭建
Linux CentOS 6.5 下 vsftpd ftp服务器搭建 by:授客 QQ:1033553122 操作系统环境:CentOS 6.5-x86_64 下载地址:http://www.ce ...
- Loadrunner打开VU时候报错Critical error(cannot use Exceptiondialog)
打开Loadrunner打开VU时候报错Critical error(cannot use Exceptiondialog) 卸载后,删掉注册表,重新安装,打开还是这样 怎么办呢 我男票告诉我,从开始 ...
- 高通 display 驱动【转】
高通display驱动 0. 关键字 MDSS : 高通平台lcd multimedia Display sub system DSI: Display Serial Interface qcom,m ...
- python web编程CGI
CGI(通用网关接口),CGI 是Web 服务器运行时外部程序的规范,按CGI 编写的程序可以扩展服务器功能. CGI 应用程序能与浏览器进行交互,还可通过数据库API 与数据库服务器等外部数据源进行 ...