PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum
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
题目解析
本题第一行给出数组长度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会错误。
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e4+;
int arr[MAX];
int dp[MAX], bg[MAX], ed[MAX];
int main()
{
int k;
scanf("%d", &k); //输入数组长度k
bool flag = false; //flag记录数组是否全为负数
for(int i = ; i < k; i++){ //输入数组
scanf("%d", &arr[i]);
if(arr[i] >= ) //只要存在正数或0则将flag标记位true
flag = true;
}
dp[] = arr[]; //以首位为结尾的最大连续连续子串和为首位arr[i]的值
int maxSum = arr[], index = ;
//maxSum记录最大连续子串和
//index记录最大连续子串的末位下标
for(int i = ; i < k; i++){
if(dp[i - ] + arr[i] >= arr[i]){ //情况2
dp[i] = dp[i - ] + arr[i];
bg[i] = bg[i - ]; //记录当前最大连续子串首位下标
ed[i] = i; //记录最大连续子串末位下标
}else{ //情况1
dp[i] = arr[i];
bg[i] = ed[i] = i;
//最大连续子串首尾下标都为i
}
if(dp[i] > maxSum){
maxSum = dp[i];
index = i;
}
}
if(flag)
printf("%d %d %d\n", maxSum, arr[bg[index]], arr[ed[index]]);
else
printf("0 %d %d\n", arr[], arr[k - ]);
return ;
}
当然本题数据量最大只有1e4,暴力的O(n^2)并不会超时,所以本题暴力依然没有问题。
暴力过一切:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e4+;
int arr[MAX], sum[MAX];
int main(){
int k;
scanf("%d", &k);
int temp = ;
for(int i = ; i < k; i++){
scanf("%d", &arr[i]);
temp += arr[i];
sum[i] = temp;
}
int maxSum = -, bg = , ed = k - ;
for(int i = ; i < k; i++){
for(int j = i; j < k; j++){
if(sum[j] - sum[i] + arr[i] > maxSum){
maxSum = sum[j] - sum[i] + arr[i];
bg = i;
ed = j;
}
}
}
if(maxSum < )
maxSum = ;
printf("%d %d %d\n", maxSum, arr[bg], arr[ed]);
return ;
}
PTA (Advanced Level) 1007 Maximum Subsequence Sum的更多相关文章
- 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, ...
- 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 ...
- PAT Advanced 1007 Maximum Subsequence Sum
题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...
- 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 ...
- 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 ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 1007 Maximum Subsequence Sum (PAT(Advance))
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 ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
随机推荐
- noip第4课作业
1. 计算邮资 [问题描述] 根据邮件的重量和用户是否选择加急计算邮费.计算规则:重量在1000克以内 (包含1000克),基本费8元.超过1000克的部分,每500克加收超重费4元,不足500 ...
- vc6中向vs2010迁移的几个问题
vc6版本支持的库编译:CJ60lib 1. 用vs2010打开CJ60库的源码的dsw,强制打开 (1)设置项目属性的语言 因为,如果代码字符的编码集不一样,则会出现函数冲定义,参数冲突等问题,这可 ...
- Lambda架构
转载:https://blog.csdn.net/brucesea/article/details/45937875 1.Lambda架构背景介绍 Lambda架构是由Storm的作者Nathan M ...
- 《mysql必知必会》学习_第八章_20180730_欢
第八章学习LIKE操作符,百分百(%)通配符,下划线(_)通配符 P47 select prod_id,prod_name from products where prod_name LIKE 'je ...
- Ubuntu12.04 root用户登录设置
ubuntu12.04默认是不允许root登录的,在登录窗口只能看到普通用户和访客登录.以普通身份登录Ubuntu后我们需要做一些修改. 1.普通用户登录后,修改系统配置文件需要切换到超级用户模式,在 ...
- postgresql 主从 patroni
1 安装基础包 1.1 postgres yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_ ...
- NODE-WEBKIT教程(6)NATIVE UI API 之MENU(菜单)
node-webkit教程(6)Native UI API 之Menu(菜单) 文/玄魂 目录 node-webkit教程(6)Native UI API 之Menu(菜单) 前言 6.1 Menu ...
- Flash插件flashplugin-nonfree的手动更新
Debian,nonfree库里有flashplugin-nonfree,这个就是网页浏览器的Flash插件了.不过它好像不会自动更新,每次更新网页浏览器后都会提示flashplugin版本过低.但f ...
- Asp .Net core 2 学习笔记(3) —— 静态文件
这个系列的初衷是便于自己总结与回顾,把笔记本上面的东西转移到这里,态度不由得谨慎许多,下面是我参考的资源: ASP.NET Core 中文文档目录 官方文档 记在这里的东西我会不断的完善丰满,对于文章 ...
- 19_python_反射
一.内置函数(补充) 1.issubclass() -- 方法用于判断参数 class 是否是类型参数 classinfo 的子类. 语法格式:issubclass(class, ...