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 ...
随机推荐
- 如何在SpringMVC中使用REST风格的url
如何在SpringMVC中使用REST风格的url 1.url写法: get:/restUrl/{id} post:/restUrl delete:/restUrl/{id} put:/restUrl ...
- 【学习笔记】---老男孩学Python,day1
老早同学就推荐自己学编程了,因为各种事耽误了几年的时间,也可以说自己没有居安思危的意识吧… 直到今年2月份决定掏钱学线上课,但是又被兼职打断了,公司忙,兼职事多,拖来拖去只能把课程延期.这一拖就到了五 ...
- 集合框架四(Map)
Map的主要实现类: --HashMap:Map的主要实现类(掌握) --LinkedHashMap:使用链表维护添加进Map中的顺序,遍历时按添加时的顺序遍历 --TreeMap:按照添加进Map中 ...
- 【代码笔记】iOS-对数组进行排序
一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ...
- 有关正则表达式的Js方法(replace)
整理一下最近常用的几个有关正则的js方法 replace 语法:stringObject.replace(regexp/substr,replacement) 用法1 const str='abc13 ...
- Windows Win7建立wifi热点,手机共享WIFI上网
Win7建立wifi热点,手机共享wifi上网 by:授客 QQ:1033553122 1.以管理员身份运行命令提示符:快捷键win+R→输入cmd→回车 2.启用并设定虚拟WiFi网卡:运行命令:n ...
- 【python】python之tuple元组
tuple特性 python的tuple与列表类似,不同之处在于tuple的元素不能修改. tuple使用小括号,列表使用方括号. tuple创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. ...
- webstorm使用过程中的一些问题与技巧
这一篇会随着使用逐渐更新: 1. 问题:string templates are not supported by current javascript version 解决 : setting &g ...
- SqlServer 一个查询语句导致tempdb增大55G(转载)
SqlServer 一个查询语句导致tempdb增大55G 今天操作着服务器,突然右下角提示“C盘空间不足”! 吓一跳!~ 看看C盘,还有7M!!!这么大的C盘空间怎么会没了呢?搞不好等下服务器会动不 ...
- 为MySQ启用HugePage
8.12.4.2 Enabling Large Page Support Some hardware/operating system architectures support memory pag ...