-
Notifications
You must be signed in to change notification settings - Fork 14
/
481 - What Goes Up.py
54 lines (49 loc) · 1.24 KB
/
481 - What Goes Up.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def LIS(d):
l = []
for i in range(len(d)):
l.append(max([l[j] for j in range(i) if l[j][-1] < d[i]] or [[]],key=len) + [d[i]])
return max(l,key=len)
def LIS1(array):
memo = [0]
prev = [0]*len(array)
for i in range(1,len(array)):
if array[memo[-1]] < array[i]:
prev[i] = memo[-1]
memo.append(i)
continue
low = 0
high = len(memo) -1
while low < high:
mid = (high + low)//2
if array[memo[mid]] < array[i]:
low = mid + 1
else:
high = mid
if array[i] < array[memo[low]]:
if low > 0:
prev[i] = memo[low - 1]
memo[low] = i
path = []
p = memo[-1]
N = len(memo)
for i in range(N):
path.append(array[p])
p = prev[p]
path.reverse();
return path
if __name__ == '__main__':
# array = [-7,10,9,2,3,8,8,6]
array = []
# print(LIS(array))
while True:
try:
array.append(int(input()))
except:
break
A = LIS1(array)
l = len(A)
print(l)
print("-")
# print(*A,end='\n')
for itm in A:
print(itm)