def main(): n = int(input()) x = [(int(input()), i+1) for i in range(n)] x1 = [i + 1 for i in range(n)] temp = [(x[i], i + 1) for i in range(n)] x2 = [el[1] for el in sorted(temp)] visited = [False for _ in range(len(x1))] i = 0 sol = [] while i < len(x1): if visited[i]: i += 1 continue j = i t = [] while not visited[j]: visited[j] = True t.append(x1[j]) j = x2[j] - 1 t.reverse() sol.append(t) i += 1 result = [] xd1 = [] xd2 = [] for x in sol: if len(x) <= 1: continue if len(x) == 2: xd1.append((x[0], x[1])) else: i = 1 j = len(x) - 1 while i < j: xd1.append((x[i], x[j])) i += 1 j -= 1 xd2.append((x[0], x[1])) i = 2 j = len(x) - 1 while i < j: xd2.append((x[i], x[j])) i += 1 j -= 1 if xd1: s = [el[0] for el in xd1] t = [el[1] for el in xd1] t.reverse() result.append(s + t) if xd2: s = [el[0] for el in xd2] t = [el[1] for el in xd2] t.reverse() result.append(s + t) print(len(result)) for el in result: print(len(el)) print(*el) if __name__ == '__main__': main()
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 | def main(): n = int(input()) x = [(int(input()), i+1) for i in range(n)] x1 = [i + 1 for i in range(n)] temp = [(x[i], i + 1) for i in range(n)] x2 = [el[1] for el in sorted(temp)] visited = [False for _ in range(len(x1))] i = 0 sol = [] while i < len(x1): if visited[i]: i += 1 continue j = i t = [] while not visited[j]: visited[j] = True t.append(x1[j]) j = x2[j] - 1 t.reverse() sol.append(t) i += 1 result = [] xd1 = [] xd2 = [] for x in sol: if len(x) <= 1: continue if len(x) == 2: xd1.append((x[0], x[1])) else: i = 1 j = len(x) - 1 while i < j: xd1.append((x[i], x[j])) i += 1 j -= 1 xd2.append((x[0], x[1])) i = 2 j = len(x) - 1 while i < j: xd2.append((x[i], x[j])) i += 1 j -= 1 if xd1: s = [el[0] for el in xd1] t = [el[1] for el in xd1] t.reverse() result.append(s + t) if xd2: s = [el[0] for el in xd2] t = [el[1] for el in xd2] t.reverse() result.append(s + t) print(len(result)) for el in result: print(len(el)) print(*el) if __name__ == '__main__': main() |