Redraiment是走梅花桩的高手。Redraiment可以选择任意一个起点,从前到后,但只能从低处往高处的桩子走。他希望走的步数最多,你能替Redraiment研究他最多走的步数吗?
数据范围:每组数据长度满足 , 数据大小满足
数据共2行,第1行先输入数组的个数,第2行再输入梅花桩的高度
输出一个结果
6 2 5 1 5 4 5
3
6个点的高度各为 2 5 1 5 4 5 如从第1格开始走,最多为3步, 2 4 5 ,下标分别是 1 5 6 从第2格开始走,最多只有1步,5 而从第3格开始走最多有3步,1 4 5, 下标分别是 3 5 6 从第5格开始走最多有2步,4 5, 下标分别是 5 6 所以这个结果是3。
def count(n,nums,res): for i in range(2,n+1): for j in range(1,i): if nums[-i]<nums[-j] and res[-i]<res[-j]+1: res[-i]=res[-j]+1 return max(res) while True: try: n=int(input()) nums = [int(x) for x in input().split()] except: break res=[1]*n print(count(n,nums,res))没引入库,用动态规划解决了
while True: try: n=int(input()) input_list=input().strip().split() steps=[1 for i in range(n)] for i in range(n-2,-1,-1): for j in range(i+1, n): if input_list[j]>input_list[i] and steps[j]+1>steps[i]: steps[i]=steps[j]+1 print(max(steps)) except: break
def scan_index(input_list,index_value): result = [] for k,v in enumerate(input_list[:index_value]): if v<input_list[index_value]: result.append(k) return result while True: try: input_len = len(input()) input_value =input().split() except:break max_dict = {} input_value = list(map(int,input_value)) for k,v in enumerate(input_value): v_min_index = scan_index(input_value,k) if len(v_min_index)==0: max_dict[k]=1 else: max_dict[k] = max([max_dict[j] for j in v_min_index])+1 print(max(max_dict.values()))
dp = dict() def getMaxSub(lst, minNum): tup = tuple(lst) outer = (tup, minNum) if outer in dp: return dp[outer] #print('This is lst: {}'.format(lst)) if len(lst) == 1: if minNum == None or minNum < lst[0]: dp[outer] = 1 return 1 dp[outer] = 0 return 0 if minNum == None: num = max(1 + getMaxSub(lst[1:], lst[0]), getMaxSub(lst[1:], None)) dp[outer] = num return num else: cur = lst[0] if cur > minNum: num = max(1 + getMaxSub(lst[1:], cur), getMaxSub(lst[1:], minNum)) dp[outer] = num return num else: num = getMaxSub(lst[1:], minNum) dp[outer] = num return num lst = [] while True: try: input() string = input() string = string.split() newLst = [] for i in string: newLst.append(int(i)) lst.append(newLst) except: break for i in lst: print(getMaxSub(i, None))
给一个python的解法。本身就是一个recursion,一开始不用dp会timeout,原因是getMaxSub(sublist, None) 出现次数过多,造成重复运算。所以用dictionary做了一个简易dp
while True: try: n = int(input()) s = input().split() s = [int(i) for i in s] res = [] for i in range(len(s)): c = 1 a = s[i] if i+1 < len(s): tem = sorted(s[i+1:]) else: res.append(c) break for j in tem: if j > a: a = j c += 1 res.append(c) # print(res) print(max(res)) except: break为啥不能通过???自己列举的数据都正确
import bisect def max_order(lists): list_num = [] list_max = [] for i in lists: # bisect_left把i插入list_num,使得list_num还是升序,返回index。insort才是就地插入 local = bisect.bisect_left(list_num, i) if local == len(list_num): # 最后一个 list_num.append(i) # 最大值放最后 list_max.append(local + 1) # 记录每个元素插在哪个位置了 else: list_num[local] = i # 不是最大就替换,给后面更多机会 list_max.append(local + 1) # 记录每个元素插在哪个位置了 return list_max while True: try: people_num = int(input()) height_list = list(map(int, input().split())) result_1 = max_order(height_list) # 升序最长 print(max(result_1)) except BaseException as er: break
# 确定状态 最后一步a[j] # 1. 最优策略中的最长上升子序列就是{a[j]} 答案是 1 # 2. 子序列长度大于 1,那么最优策略中,a[j] 前一个元素是 a[i] 且 a[i] < a[j] # 因为是最优策略,那么以 a[i] 结尾的子序列一定是最长的 # 不知道 a[i] 是哪一个,所以枚举每个 i # 问题变为求 以a[i] 结尾的最长子序列 # 得出状态 f[j] = 以 a[j] 结尾的最长子序列的长度 # 转移方程 f[j] = max{1, f[i] + 1 且 i<j and a[i] < a[j]} # 计算f[0] f[1] ......f[n-1] 然后取最大值 while True: try: n, A = int(input()), list(map(int, input().split())) if n <= 0: print('0') else: res = 0 f = [1] * n for j in range(n): for i in range(j): if A[i] < A[j] and f[i] + 1 > f[j]: f[j] = f[i] + 1 res = max(res, f[j]) print(res) except: break
while True: try: a, b = int(input()), list(map(int, input().split())) size = len(b) dp = [1]*size for i in range(1, size): for j in range(i): if b[i]>b[j]: dp[i]=max(dp[i],dp[j]+1) print(max(dp)) except: break
import bisect while True: try: a, b = int(input()), map(int, input().split()) q = [] for v in b: pos = bisect.bisect_left(q, v) if pos == len(q): q.append(v) else: q[pos] = v print(len(q)) except: break # 凡哥作品 #解释: #相当于维护一个结果数组,如果当前元素比结果数组的值都大的的话,就追加在结果数组后面 #(相当于递增序列长度加了1);否则的话用当前元素覆盖掉第一个比它大的元素 #(这样做的话后续递增序列才有可能更长,即使并没有更长,这个覆盖操作也并没有副作用哈, #当然这个覆盖操作可能会让最终的结果数组值并不是最终的递增序列值,这无所谓) #https://leetcode-cn.com/problems/longest-increasing-subsequence/comments/284831
""" Create Time: 2020/6/15 10:59 Author: FengkaiXiao """ """ Create Time: 2020/6/15 10:59 Author: FengkaiXiao """ import bisect def meihuazhaung(): while True: try: a, b = int(input()), map(int, input().split()) q = [] for v in b: pos = bisect.bisect_left(q, v) if pos == len(q): q.append(v) else: q[pos] = v print(len(q)) except: break meihuazhaung()
while True: try: n = int(input()) container = [] container = input().split() container = [int(x) for x in container] dp = [0]*n dp[-1] = 1 res = [] for j in range(n-2,-1,-1): temp = [] for k in range(j,n): if container[k] > container[j]: temp.append(1+dp[k]) if len(temp) == 0: temp.append(1) dp[j] = max(temp) print(max(dp)) except: break