n = int(input()) heights = [int(input()) for _ in range(n)] sorted_heights = sorted(heights) sorted_indices = {h: i for i, h in enumerate(sorted_heights)} norm_heights = [0] + [sorted_indices[h] + 1 for h in heights] k = 0 rounds = [[], []] visited = set() i = 1 while i <= n: if i in visited: i += 1 continue cycle = [] j = i while j not in visited: cycle.append(j) visited.add(j) j = norm_heights[j] l = 1 r = len(cycle) - 1 while l < r: rounds[k].append((cycle[l], cycle[r])) norm_heights[cycle[l]], norm_heights[cycle[r]] = norm_heights[cycle[r]], norm_heights[cycle[l]] l += 1 r -= 1 i += 1 if len(rounds[k]) > 0: k += 1 i = 1 while i <= n: if norm_heights[i] != i and norm_heights[i] < i: rounds[k].append((i, norm_heights[i])) i += 1 if len(rounds[k]) > 0: k += 1 print(k) for r in rounds: if r: swaps = [x[0] for x in r] + list(reversed([x[1] for x in r])) print(len(swaps)) for swap in swaps: print(swap, end=' ') print()
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 | n = int(input()) heights = [int(input()) for _ in range(n)] sorted_heights = sorted(heights) sorted_indices = {h: i for i, h in enumerate(sorted_heights)} norm_heights = [0] + [sorted_indices[h] + 1 for h in heights] k = 0 rounds = [[], []] visited = set() i = 1 while i <= n: if i in visited: i += 1 continue cycle = [] j = i while j not in visited: cycle.append(j) visited.add(j) j = norm_heights[j] l = 1 r = len(cycle) - 1 while l < r: rounds[k].append((cycle[l], cycle[r])) norm_heights[cycle[l]], norm_heights[cycle[r]] = norm_heights[cycle[r]], norm_heights[cycle[l]] l += 1 r -= 1 i += 1 if len(rounds[k]) > 0: k += 1 i = 1 while i <= n: if norm_heights[i] != i and norm_heights[i] < i: rounds[k].append((i, norm_heights[i])) i += 1 if len(rounds[k]) > 0: k += 1 print(k) for r in rounds: if r: swaps = [x[0] for x in r] + list(reversed([x[1] for x in r])) print(len(swaps)) for swap in swaps: print(swap, end=' ') print() |