fork download
  1. # Credits to Sergii Olshanetskyi
  2. # https://groups.google.com/g/google-code/c/nF6-5NEwLqE
  3.  
  4. def get_boundaries(size):
  5. return size - 1, (size + 2) * (size - 1) // 2
  6.  
  7. def swap(arr, s, e):
  8. while s < e:
  9. arr[s], arr[e] = arr[e], arr[s]
  10. s += 1
  11. e -= 1
  12.  
  13. number_of_cases = input()
  14.  
  15. for test_case_number in range(int(number_of_cases)):
  16. input_params = input().split(" ")
  17. N = int(input_params[0])
  18. C = int(input_params[1])
  19.  
  20. min_value, max_value = get_boundaries(N)
  21.  
  22. if C < min_value or C > max_value:
  23. print("Case #{}: {}".format(test_case_number + 1, "IMPOSSIBLE"))
  24. continue
  25.  
  26. current_C = C
  27.  
  28. array = [-1] * N
  29.  
  30. start_index = 0
  31. last_index = len(array) - 1
  32.  
  33. current_n = start_index + 1
  34.  
  35. while start_index < last_index:
  36. min_v, max_v = get_boundaries(N - current_n)
  37.  
  38. if current_C - 1 < max_v:
  39. array[start_index] = current_n
  40. current_n += 1
  41. start_index += 1
  42. current_C -= 1
  43. continue
  44.  
  45. spooky_start = start_index
  46. start = True
  47.  
  48. while current_n <= N:
  49. if start:
  50. array[start_index] = current_n
  51. start_index += 1
  52. else:
  53. array[last_index] = current_n
  54. last_index -= 1
  55.  
  56. current_n += 1
  57. start = not start
  58.  
  59. next_min_index = current_C - max_v
  60.  
  61. swap(array, spooky_start, spooky_start + next_min_index - 1)
  62.  
  63. result = ' '.join(str(i) for i in array)
  64.  
  65. print("Case #{}: {}".format(test_case_number + 1, result))
Success #stdin #stdout 0.02s 9420KB
stdin
5
4 6
2 1
7 12
7 2
2 1000
stdout
Case #1: 1 3 4 2
Case #2: 1 2
Case #3: 1 2 3 5 7 6 4
Case #4: IMPOSSIBLE
Case #5: IMPOSSIBLE