n = int(input()) heights = [int(input()) for x in range(0, n)] correct_heights = sorted(heights) def get_cycle(l, pos): cycle = [] while l[pos] != -1: if l[pos] == pos: break cycle.append(l[pos]) v = l[pos] l[pos] = -1 pos = v return cycle def get_cycles(): cycles = [] correct_pos = [int(correct_heights.index(heights[x])) for x in range(0, n)] for i in range(0, n): cycle = get_cycle(correct_pos, i) if len(cycle) > 0: cycles.append(cycle) return cycles def get_swaps(c): swaps = [] while len(c) > 1: swaps.append([c[0], c[len(c)-1]]) c = c[1:len(c)-1] return swaps def merge_swaps(c): merged = [] for i in c: merged.extend(get_swaps(i)) return merged def prepare_round(m): l = [] r = [] for i in m: l.append(i[0]) r.append(i[1]) r = r[::-1] return l + r def swap_places(merged): for i in merged: tmp = heights[i[0]] heights[i[0]] = heights[i[1]] heights[i[1]] = tmp round_list = [] while heights != correct_heights: cycles = get_cycles() merged_swaps = merge_swaps(cycles) r = prepare_round(merged_swaps) round_list.append(r) swap_places(merged_swaps) print(len(round_list)) for i in round_list: print(len(i)) r = [str(x+1) for x in i] print(" ".join(r))
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 | n = int(input()) heights = [int(input()) for x in range(0, n)] correct_heights = sorted(heights) def get_cycle(l, pos): cycle = [] while l[pos] != -1: if l[pos] == pos: break cycle.append(l[pos]) v = l[pos] l[pos] = -1 pos = v return cycle def get_cycles(): cycles = [] correct_pos = [int(correct_heights.index(heights[x])) for x in range(0, n)] for i in range(0, n): cycle = get_cycle(correct_pos, i) if len(cycle) > 0: cycles.append(cycle) return cycles def get_swaps(c): swaps = [] while len(c) > 1: swaps.append([c[0], c[len(c)-1]]) c = c[1:len(c)-1] return swaps def merge_swaps(c): merged = [] for i in c: merged.extend(get_swaps(i)) return merged def prepare_round(m): l = [] r = [] for i in m: l.append(i[0]) r.append(i[1]) r = r[::-1] return l + r def swap_places(merged): for i in merged: tmp = heights[i[0]] heights[i[0]] = heights[i[1]] heights[i[1]] = tmp round_list = [] while heights != correct_heights: cycles = get_cycles() merged_swaps = merge_swaps(cycles) r = prepare_round(merged_swaps) round_list.append(r) swap_places(merged_swaps) print(len(round_list)) for i in round_list: print(len(i)) r = [str(x+1) for x in i] print(" ".join(r)) |