以下为知识点:
1、index倒序切片:
temp=range(10)
temp[:ind:-1]
2、输出121队形的计算方法和实现步骤
3、bisect模块的使用。(二分法)
![]()
1 #计算方法为,计算出以每个元素为最高点的最长121队列,再比较队列长度
2 #实现步骤:
3 #分别计算从左往右和从右往左的递增数列,并将两者相加,减去重复计算的中间数,则为最长数列长度
4 #抓住了中间数的index,通过计算方法求出最长队列的每个队员身高。
5
6 import bisect
7 def mid_incre(l):
8 arr=[l[0]]
9 dp=[1 for i in range(a)]
10 for i in range(1,a):
11 if l[i]>arr[-1]:
12 arr.append(l[i])
13 dp[i]=len(arr)
14 else:
15 pos=bisect.bisect_left(arr,l[i])#获取插入序列左侧index
16 dp[i]=pos+1
17 arr[pos]=l[i]#这个置换不能少。
18 return dp
19 #a = 8
20 #l = [186, 186, 140, 200, 150, 400, 197, 190]
21 a=int(input().strip())
22 l = list(map(int,input().strip().split()))
23 #记录递增序列长度
24 left_incre=mid_incre(l)
25 right_incre=mid_incre(l[::-1])[::-1]
26 n_arr=[left_incre[i]+right_incre[i]-1 for i in range(len(left_incre))]
27 print(a-max(n_arr))
28 #打印其中一列身高队列
29 for i in n_arr:
30 if i==max(n_arr):
31 ind=n_arr.index(i)
32 break
33 pq=[]
34 for i in range(ind):
35 if l[i]<l[ind] and pq==[]:
36 pq.append(l[i])
37 elif pq[-1]>l[i]:
38 pos=bisect.bisect_left(pq,l[i])
39 pq[pos]=l[i]
40 elif pq[-1]<l[i]:
41 pq.append(l[i])
42 #print(pq+[l[ind]])
43 pq1=[]
44 temp=range(a)#print(temp[:ind+1:-1])
45 for i in temp[:ind:-1]:
46 if l[i]<l[ind] and pq1==[]:
47 pq1.append(l[i])
48 elif pq1[-1]>l[i]:
49 pos=bisect.bisect_left(pq1,l[i])
50 #print(pos)
51 pq1[pos]=l[i]
52 elif pq1[-1]<l[i]:
53 pq1.append(l[i])
54 one_queue=pq+[l[ind]]+pq1[::-1]
55 print(one_queue)