python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)
python编写PAT甲级 1007 Maximum Subsequence Sum
wenzongxiao1996
2019.4.3
题目
Given a sequence of K integers { N1, N2, ..., NK}. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj} where 1≤i≤j≤K. 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 (≤10000). 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
暴力解法(超时了)
def seq_sum(s):
"""求序列的所有元素之和"""
result = 0
for i in range(len(s)):
result += s[i]
return result
def main():
n = int(input())
seq = [int(i) for i in input().split()]
max = -1
pos_i = 0
pos_j = n-1
for i in range(n):
for j in range(i,n):
sum_temp = seq_sum(seq[i:j+1])
if sum_temp > max:
max = sum_temp
pos_i = i
pos_j = j
if max < 0:
print(0,seq[pos_i],seq[pos_j])
else:
print(max,seq[pos_i],seq[pos_j])
if __name__ == '__main__':
main()
分治法
def division_solution(seq,left,right):
if left == right: # 递归出口
if seq[left] >= 0:
return left,right,seq[left]
else:
return left,right,-1
center = (left+right)//2 # 地板除
# 从中间到左边的最大子串
sum_left = 0
max_sum_left = -1 # 一定要设为负数
pos_left = left # 要返回下标
for i in range(left,center+1)[::-1]: # 反向迭代
sum_left += seq[i]
if sum_left >= max_sum_left:
max_sum_left = sum_left
pos_left = i
# 从中间到右边的最大子串
sum_right = 0
max_sum_right = -1 # 一定要设为负数
pos_right = right # 要返回下标
for i in range(center+1,right+1):
sum_right += seq[i]
if sum_right > max_sum_right:
max_sum_right = sum_right
pos_right = i
# 递归求解左右两个子问题
i_left,j_left,max_left_sum = division_solution(seq,left,center)
i_right,j_right,max_right_sum = division_solution(seq,center+1,right)
if max(max_left_sum,max_right_sum,max_sum_left+max_sum_right) < 0:
return left,right,-1
else:
if max(max_left_sum,max_right_sum,max_sum_left+max_sum_right) == max_left_sum:
return i_left,j_left,max_left_sum
elif max(max_left_sum,max_right_sum,max_sum_left+max_sum_right) == max_right_sum:
return i_right,j_right,max_right_sum
else:
return pos_left,pos_right,max_sum_left+max_sum_right
def main():
n = int(input())
seq = [eval(i) for i in input().split()]
i,j,sum_max = division_solution(seq,0,n-1)
if sum_max < 0:
print(0,seq[0],seq[-1])
else:
print(sum_max,seq[i],seq[j])
if __name__ == '__main__':
main()
动态规划
def main():
n = int(input())
seq = [eval(i) for i in input().split()]
sum_max = -1
pos_i = 0
pos_i_temp = 0 # 最大子序列的左下标不能随意更改,只有找到了更大的子串才能改,用这个变量先保存着当前寻找的子串的左下标
pos_j = n-1
sum_temp = 0
for i in range(n):
sum_temp += seq[i]
if sum_temp > sum_max:
sum_max = sum_temp
pos_j = i
pos_i = pos_i_temp
elif sum_temp < 0:# 和小于0的子串不需要考虑
sum_temp = 0
pos_i_temp = i+1
if sum_max < 0:
print(0,seq[0],seq[-1])
else:
print(sum_max,seq[pos_i],seq[pos_j])
if __name__ == '__main__':
main()
谢谢观看!敬请指正!
参考博客:https://www.cnblogs.com/allzy/p/5162815.html
原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168
python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)的更多相关文章
- PAT 1007 Maximum Subsequence Sum (25分)
题目 Given a sequence of K integers { N1 , N2 , ..., NK }. A continuous subsequence is define ...
- 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 ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT 1007 Maximum Subsequence Sum 最大连续子序列和
Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PAT 1007 Maximum Subsequence Sum (最大连续子序列之和)
Given a sequence of K integers { N1, N2, ..., *N**K* }. A continuous subsequence is defined to be { ...
- 数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- [pat]1007 Maximum Subsequence Sum
经典最大连续子序列,dp[0]=a[0],状态转移dp[i]=max(dp[i-1]+a[i],a[i])找到最大的dp[i]. 难点在于记录起点,这里同样利用动态规划s[i],如果dp[i]选择的是 ...
随机推荐
- 安装xcode6 beta 后调试出现Unable to boot the iOS Simulator以及编译苹果官方Swift的demo报错failed with exit code 1的解决的方法
苹果昨天公布新语言Swift(雨燕),须要安装xcode6 以及mac os 系统为10.9以上. (xcode6 beta 可在官方下载.须要登录开发人员账号:mac os 系统直接更新就可以.在此 ...
- Linux 6.3下安装Oracle Enterprise Cloud Control 12c
Oracle enterprise cloud control 12c的安装是一个比較复杂的过程,由于他须要依赖于Oracel database以及Oracle Weblogic. 如今Oracle已 ...
- (数据结构整理)NJUPT1054
这一篇博客以一些OJ上的题目为载体,整理一下数据结构.会陆续的更新. .. 我们都知道,数据结构的灵活应用有时能让简化一些题目的解答. 一.栈的应用 1.NJUPT OJ 1054(回文串的推断) 回 ...
- Atitit.运行cmd 命令行 php
Atitit.运行cmd 命令行 php 1. 运行cmd 命令行,调用系统命令的基础 1 1.1. 实际运行模式 1 1.2. 空格的问题 1 1.3. 中文路径的问题.程序文件读取编码设置 1 1 ...
- 每一个人都懂得敏捷开发 (软件project), 为何产品开发的效率与质量还是这么的烂?
敏捷开发(软件project)是 "设计" 出来的.不是 "学" 来的-- 很多人都一直在质疑敏捷开发能否提高效率与质量? 更有不少人以嘲讽.不屑的口吻看待软件 ...
- invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
Column 'dbo.tbm_vie_View.ViewID' is invalid in the select list because it is not contained in either ...
- Android属性动画-基本用法
在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们提供了两种实现动画效果的方式,逐帧动画(frame-by-frame animation)和补间动画(twe ...
- monad-本质解释- a monad is a design pattern--monad与泛型相关
monad的特征: 类型转化+添加新的操作. monad RACStream RACSignal RACSubject monad:单一体,(不可分的)个体 以计算为中心的封装. In functi ...
- Ubuntu18.06 Mate桌面环境下VirtuslBox打开虚拟机“全局菜单”异常退出解决办法
在安装完Ubuntu18.06 Mate桌面环境后在VirtuslBox里打开虚拟机会出现“全局菜单”异常退出问题. 产生上面问题的原因是你的虚拟机可能在 显示= >屏幕= >硬件加速里勾 ...
- vue中的三级联动
1.template里面的内容 2.js里面的内容 3.函数怎么写? 这是一个省市区的三级联动,首先你要传递中国的id,这样才能获取到所有的省份,所以在vue的项目中,我需要发一次进页面就请求(来得到 ...