-
Notifications
You must be signed in to change notification settings - Fork 0
/
atcoder_interactive_sorting.py
102 lines (78 loc) · 2.06 KB
/
atcoder_interactive_sorting.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from string import ascii_uppercase
def comp(a, b):
print("? "+a, b)
return input()
def sort5(l):
a, b, c, d, e = l
if comp(a, b) == ">":
a, b = b, a
if comp(c, d)== ">":
c, d = d, c
if comp(a, c) == ">":
a, b, c, d = c, d, a, b
if comp(c, e) == "<":
if comp(d, e) == ">":
d, e = e, d
if comp(b, d) == "<":
if comp(b, c) == "<":
res = [a, b, c, d, e]
else:
res = [a, c, b, d, e]
else:
if comp(b, e) == "<":
res = [a, c, d, b, e]
else:
res = [a, c, d, e, b]
else:
if comp(a, e) == "<":
if comp(b, c) == "<":
if comp(b, e) == "<":
res = [a, b, e, c, d]
else:
res = [a, e, b, c, d]
else:
if comp(b, d) == "<":
res = [a, e, c, b, d]
else:
res = [a, e, c, d, b]
else:
if comp(b, c) == "<":
res = [e, a, b, c, d]
else:
if comp(b, d) == "<":
res = [e, a, c, b, d]
else:
res = [e, a, c, d, b]
return res
def merge_sort(l):
if len(l)<=1:
return l
mid = len(l)//2
left = l[:mid]
right = l[mid:]
left = merge_sort(left)
right = merge_sort(right)
return merge(left, right)
def merge(left, right):
merged = []
lp, rp = 0, 0
while lp < len(left) and rp < len(right):
print("? "+left[lp], right[rp])
if input() == "<":
merged.append(left[lp])
lp = lp + 1
else:
merged.append(right[rp])
rp = rp + 1
if lp < len(left):
merged.extend(left[lp:])
if rp < len(right):
merged.extend(right[rp:])
return merged
n, q = map(int, input().split())
ans = list(ascii_uppercase[:n])
if n > 5:
ans = merge_sort(ans)
else:
ans = sort5(ans)
print("! "+"".join(ans), flush=True)