-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
72 lines (56 loc) · 1.33 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# BOJ 2529
def next_permutation(a: list) -> bool: #O(N)
i = len(a) - 1
while i > 0 and a[i-1] >= a[i]:
i -= 1
if i <= 0:
return False
j = len(a) - 1
while a[i-1] >= a[j]:
j -= 1
a[i-1], a[j] = a[j], a[i-1]
j = len(a) - 1
while a[i] > a[j]:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def prev_permutation(a: list) -> bool:
i = len(a) - 1
while i > 0 and a[i - 1] <= a[i]:
i -= 1
if i <= 0:
return False
j = len(a) - 1
while a[i - 1] <= a[j]:
j -= 1
a[i - 1], a[j] = a[j], a[i - 1]
j = len(a) - 1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def check(perm: list, a: list) -> bool:
for i in range(len(a)):
if a[i] == '>' and perm[i] < perm[i+1]:
return False
if a[i] == '<' and perm[i] > perm[i+1]:
return False
return True
k = int(input())
a = input().split()
small = [i for i in range(k+1)]
big = [9-i for i in range(k+1)]
while True:
if check(small, a):
break
if not next_permutation(small):
break
while True:
if check(big, a):
break
if not prev_permutation(big):
break
print(''.join(map(str,big)))
print(''.join(map(str,small)))