n = int(input()) b = [(int(input()),i) for i in range(n)] #print(b) b.sort() #print(b) u = [False]*n k = 0 p = 0 cycles = [] m = 0 while k<n: if not u[p]: q = p c = [] u[q] = True while True: q = b[q][1] if u[q]: c = [p]+c[::-1] if len(c)>1: cycles.append(c) if len(c)>m: m = len(c) break c.append(q) u[q] = True k += len(c) p += 1 #print(cycles,m) ans = [] for c in cycles: r = 0 while len(c)>1: if len(ans)<r+1: ans.append([]) for i in range(1,len(c),2): ans[r].append((c[i-1],c[i])) c = c[::2] r += 1 #print(ans) print(len(ans)) for r in ans: print(len(r)*2) left = [] right = [] for c in r: left.append(c[0]+1) right.append(c[1]+1) print(*(left+right[::-1]))
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 | n = int(input()) b = [(int(input()),i) for i in range(n)] #print(b) b.sort() #print(b) u = [False]*n k = 0 p = 0 cycles = [] m = 0 while k<n: if not u[p]: q = p c = [] u[q] = True while True: q = b[q][1] if u[q]: c = [p]+c[::-1] if len(c)>1: cycles.append(c) if len(c)>m: m = len(c) break c.append(q) u[q] = True k += len(c) p += 1 #print(cycles,m) ans = [] for c in cycles: r = 0 while len(c)>1: if len(ans)<r+1: ans.append([]) for i in range(1,len(c),2): ans[r].append((c[i-1],c[i])) c = c[::2] r += 1 #print(ans) print(len(ans)) for r in ans: print(len(r)*2) left = [] right = [] for c in r: left.append(c[0]+1) right.append(c[1]+1) print(*(left+right[::-1])) |