-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlena_sort.py
More file actions
49 lines (41 loc) · 1.26 KB
/
lena_sort.py
File metadata and controls
49 lines (41 loc) · 1.26 KB
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
import sys
sys.setrecursionlimit(100000)
def main(length, comp, offset, min_vals, max_vals, ans):
if length == 0:
return
if length == 1:
ans.append(offset)
return
pivot = 0
comp -= (length - 1)
while True:
if min_vals[pivot] + min_vals[length - pivot - 1] <= comp <= max_vals[pivot] + max_vals[length - pivot - 1]:
break
pivot += 1
left = min_vals[pivot]
right = comp - left
while True:
if min_vals[length - pivot - 1] <= right <= max_vals[length - pivot - 1]:
break
left += 1
right -= 1
ans.append(pivot + offset)
main(pivot, left, offset, min_vals, max_vals, ans)
main(length - pivot - 1, comp - left, offset + pivot + 1, min_vals, max_vals, ans)
if __name__ == "__main__":
q = int(input())
for q_itr in range(q):
inp = input().split()
l = int(inp[0])
c = int(inp[1])
maxs = [0,0]
mins = [0,0]
for i in range(1, l):
maxs.append(maxs[i] + i)
mins.append(i + mins[i // 2] + mins[i - i // 2])
if c < mins[-1] or c > maxs[-1]:
print(-1)
else:
result = []
main(l, c, 1, mins, maxs, result)
print(*result)