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 ...
随机推荐
- 实例化Flask的参数 及 对app的配置
Flask 是一个非常灵活且短小精干的web框架 , 那么灵活性从什么地方体现呢? 有一个神奇的东西叫 Flask配置 , 这个东西怎么用呢? 它能给我们带来怎么样的方便呢? 首先展示一下: from ...
- 一个挺好用的生成GIF格式图片的小程序
import os import re import imageio pic_list = os.listdir() pic_type = ['png', 'jpg', 'jpeg', 'bmp'] ...
- js-权威指南学习笔记21
第二十一章 多媒体和图形编程 1.为了强制让图片缓存起来,首先利用Image()构造函数来创建一个屏幕外的图片对象,之后将该对象的src属性设置成期望的URL. 2.由于各家浏览器制造商未能在对标准音 ...
- 浅谈 Event loop (事件循环)
从Event Loop谈JS的运行机制 先来理解一个概念: JS分为同步任务和异步任务 同步任务都在主线程上执行,形成一个执行栈 Execute Content Stack 主线程之外,事件触发线程管 ...
- Visual Studio Code必备插件
HTML Snippets: 超级实用且初级的 H5代码片段以及提示 HTMLHint: html代码检测 HTML CSS Support : 让 html 标签上写class 智能提示当前项目所支 ...
- windows 远程连接
* 方法1:windows自带的远程工具 缺点:如果操作系统是家庭版,会一致连接不上:尽管想办法把这个功能打开: 步骤: * 打开允许远程连接: 点进去自己设置就行,没有什么好说的 设置完之后,需要允 ...
- Web服务架构风格之REST
REST(Representational State Transfer)是一种Web服务的架构,其目的是创建具有良好扩展性的分布式系统.它的约束包含: 使用C/S模型.client和server之间 ...
- Problem5-Project Euler
Smallest multiple 2520 is the smallest number that can be divided by each of the numbers from 1 to ...
- She Left Her Shoes
She left her shoes, she took everything else, her toothbrush, her clothes, and even that stupid litt ...
- SQL Server 全文索引的硬伤(转载)
本文关键字:SQL Server全文索引.CONTAINS.FREETEXT.CONTAINSTABLE.FREETEXTTABLE等谓词. 想象这样一个场景:在DataBase_name.dbo.T ...