-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreversort_engineering.py
59 lines (38 loc) · 1.36 KB
/
reversort_engineering.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
def solve(line):
numOfElements = line[0]
cost = line[1]
low = numOfElements - 1
high = int(((numOfElements * (numOfElements + 1)) / 2) - 1)
if cost < low or cost > high:
result = 'IMPOSSIBLE'
else:
ans = [None] * numOfElements
iteration = numOfElements - 1
low = 1
high = numOfElements
reverse = []
for i in range(numOfElements):
if (cost - numOfElements) >= iteration - 1:
cost = cost - numOfElements
numOfElements = numOfElements - 1
ans[high - 1] = low
ans[low - 1:high] = ans[low - 1:high][::-1]
reverse.append([low - 1, high])
low = low + 1
iteration = iteration - 1
else:
cost = cost - 1
numOfElements = numOfElements - 1
ans[low - 1] = low
low = low + 1
iteration = iteration - 1
for i in reverse[::-1]:
ans[i[0]:i[1]] = ans[i[0]:i[1]][::-1]
result = ' '.join(str(e) for e in ans)
return result
if __name__ == '__main__':
t = int(input())
for case in range(1, t+1):
line = list(map(int, input().split()))
result = solve(line)
print('Case #{}: {}'.format(case, result))