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]选择的是 ...
随机推荐
- hdoj 3376,2686 Matrix Again 【最小费用最大流】
题目:hdoj 3376 Matrix Again 题意:给出一个m*n的矩阵,然后从左上角到右下角走两次,每次仅仅能向右或者向下,出了末尾点其它仅仅能走一次,不能交叉,每次走到一个格子拿走这个格子中 ...
- SVN版本号管理工具使用中常见的代码提交冲突问题的解决方法
相信刚開始学习使用SVN的小伙伴在项目合作开发的过程中一定常常遇到一些影响到自己编写的代码的苦恼.我这里列举了几种常见的问题以及问题的解决方法: 1.误删除和误操作的问题 问题1:有A和B两个人一块合 ...
- python批量下载色影无忌和蜂鸟的图片 爬虫小应用
有些冗余信息.由于之前測试正則表達式.所以没有把它们给移走.只是不影响使用. # -*- coding:utf-8 -*- import re,urllib,sys,os,time def getAl ...
- Android开发之EditText 详解(addTextChangedListener监听用户输入状态)
为了实现像qq或者微信输入框的效果,当在 EditText输入字符串时发送按钮显示,当输入框字符消除掉时按钮改变.所以这时候我就要用到addTextChangedListener 用它来监听用户输入状 ...
- c# 静态成员和实例成员的区别
静态成员也称为共享成员,例如静态属性 静态字段 静态方法:静态成员可以在类的实例之间共享. 静态类中只能有静态成员,不能有实例成员,因为静态类不能进行实例化: 在非静态类中 即可以有静态成员 也可以有 ...
- POJ-1182 食物链 并查集(互相关联的并查集写法)
题目链接:https://cn.vjudge.net/problem/POJ-1182 题意 中文题目,就不写了哈哈 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃 ...
- python 任何基础问题,包括语法等
*)copy()和deep copy() 参考链接:https://blog.csdn.net/qq_32907349/article/details/52190796 *)OPP面向对象编程 *)接 ...
- 如何创建一个可以使用try.....catch.......捕获的异常
代码很简单,大家一看基本上就能明白(有一定的java基础,熟悉try......catch.....finally的使用方法) package com.nokia.test1; public clas ...
- Map和Collection详解
Collection -----List -----LinkedList 非同步 ----ArrayList 非同 ...
- ZooKeeper 特性
ZooKeeper 拥有一个层次的命名空间.(like distributed) 注意:ZooKeeper 中不许使用相对路径. 一 ZooKeeper 数据模型 ...