-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2550.py
38 lines (32 loc) · 785 Bytes
/
2550.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
# 전구
import sys
from collections import deque
from bisect import bisect_left
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
B_pos = [0 for _ in range(N + 1)]
for i, n in enumerate(B):
B_pos[n] = i + 1
cnt = [B_pos[A[0]]]
idx = [[]]
for i, n in enumerate(A):
match = B_pos[n]
if cnt[-1] < match:
cnt.append(match)
idx.append([i])
else:
search = bisect_left(cnt, match)
cnt[search] = match
idx[search].append(i)
print(len(cnt))
suyeol = deque([])
suyeol.appendleft(idx.pop().pop())
while idx:
choose = idx.pop()
i = choose.pop()
while i >= suyeol[0]:
i = choose.pop()
suyeol.appendleft(i)
print(*sorted([A[i] for i in suyeol]))