-
Notifications
You must be signed in to change notification settings - Fork 0
/
10775.py
44 lines (35 loc) · 1.17 KB
/
10775.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
from sys import stdin
input = stdin.readline
def union_find(disjoint_set, gate):
if disjoint_set[gate] != -1:
disjoint_set[gate] = union_find(disjoint_set, disjoint_set[gate])
return disjoint_set[gate]
else:
return gate
def solve():
G = int(input())
P = int(input())
ans = 0
# i번 게이트에 처음 넣으면 disjoint_set[i] = i-1
disjoint_set = [-1] * (G + 1)
arr = [int(input()) for _ in range(P)]
for plane in range(P):
gate = arr[plane]
# 이미 들어가 있으면
if disjoint_set[gate] != -1:
# 3, 4번 들어가 있는 상태에서 gate = 4 -> next_gate = 2
next_gate = union_find(disjoint_set, gate)
if next_gate:
# disjoin_set[2] = 1
disjoint_set[next_gate] = union_find(disjoint_set, next_gate-1)
# disjoint_set[4] = 1
disjoint_set[gate] = disjoint_set[next_gate]
else:
break
# 처음 넣는 경우
else:
disjoint_set[gate] = union_find(disjoint_set, gate - 1)
ans += 1
return ans
if __name__ == '__main__':
print(solve())