n = int(input()) a = [int(input()) for _ in range(n)] a_sorted = list(sorted(a)) scale_map = {a_sorted[i]: i for i in range(n)} a = [scale_map[v] for v in a] ans_pref = [] ans_suf = [] def check(): if all(map(lambda x: x[0] < x[1], zip(a,a[1:]))): print(len(ans_pref)) for (b1, b2) in zip(ans_pref, ans_suf): x = b1+b2[::-1] print(len(x)) for y in x: print(y+1, end=' ') print() return True return False while not check(): visited = [False]*n ans_pref.append([]) ans_suf.append([]) for i in range(n): idxs = [] values = [] while not visited[i]: visited[i] = True idxs.append(i) values.append(a[i]) i = a[i] ans_pref[-1] += idxs[:len(idxs)//2] ans_suf[-1] += list(reversed(idxs[(len(idxs)+1)//2:])) for (i,v) in zip(idxs, reversed(values)): a[i] = v
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 | n = int(input()) a = [int(input()) for _ in range(n)] a_sorted = list(sorted(a)) scale_map = {a_sorted[i]: i for i in range(n)} a = [scale_map[v] for v in a] ans_pref = [] ans_suf = [] def check(): if all(map(lambda x: x[0] < x[1], zip(a,a[1:]))): print(len(ans_pref)) for (b1, b2) in zip(ans_pref, ans_suf): x = b1+b2[::-1] print(len(x)) for y in x: print(y+1, end=' ') print() return True return False while not check(): visited = [False]*n ans_pref.append([]) ans_suf.append([]) for i in range(n): idxs = [] values = [] while not visited[i]: visited[i] = True idxs.append(i) values.append(a[i]) i = a[i] ans_pref[-1] += idxs[:len(idxs)//2] ans_suf[-1] += list(reversed(idxs[(len(idxs)+1)//2:])) for (i,v) in zip(idxs, reversed(values)): a[i] = v |