def determine_pairs(basis):
basis = [x-1 for x in basis]
position = [0]*len(basis)
for i,x in enumerate(basis):
position[x] = i
pairs = []
swapped = [False]*len(basis)
for i in range(len(basis)):
breaker = False
if not swapped[i] and i != position[i]:
x = i
while True:
if basis[x] == position[x]:
alpha = x
beta = position[x]
breaker = True
else:
alpha = basis[x]
beta = position[x]
if swapped[alpha] or swapped[beta]:
break
pairs.append((position[alpha], position[beta]))
swapped[alpha] = True
swapped[beta] = True
basis[position[alpha]], basis[position[beta]] = basis[position[beta]], basis[position[alpha]]
position[alpha], position[beta] = position[beta], position[alpha]
if breaker == True:
break
x = alpha
return [(x, y) for x,y in pairs]
def pairs_to_mapping(pairs):
mapping = []
for x, y in pairs:
mapping.append(x)
for x, y in reversed(pairs):
mapping.append(y)
return mapping
def transform(basis, seq):
basis = [x-1 for x in basis]
people = [basis[x] for x in seq]
result = [x for x in basis]
for i, x in enumerate(seq):
result[x] = people[len(seq)-1-i]
return [x+1 for x in result]
def outer_pipeline(basis):
if basis == list(range(1, len(basis)+1)):
return []
else:
pairs = determine_pairs(basis)
mapping = pairs_to_mapping(pairs)
res = [mapping]
next_system = transform(basis, mapping)
if next_system != list(range(1, len(basis)+1)):
pairs = determine_pairs(next_system)
mapping = pairs_to_mapping(pairs)
next_system = transform(next_system, mapping)
res.append(mapping)
return res
n = int(input())
parts = []
for i in range(n):
part = int(input())
parts.append((i, part))
parts = sorted(parts, key = lambda x: x[1])
basis = [0]*len(parts)
for i,x in enumerate(parts):
basis[x[0]] = i+1
res = outer_pipeline(basis)
print(len(res))
_ = [[print(len(some_map))] and [print(x+1, end = ' ') for x in some_map] and print() for some_map in res]
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | def determine_pairs(basis): basis = [x-1 for x in basis] position = [0]*len(basis) for i,x in enumerate(basis): position[x] = i pairs = [] swapped = [False]*len(basis) for i in range(len(basis)): breaker = False if not swapped[i] and i != position[i]: x = i while True: if basis[x] == position[x]: alpha = x beta = position[x] breaker = True else: alpha = basis[x] beta = position[x] if swapped[alpha] or swapped[beta]: break pairs.append((position[alpha], position[beta])) swapped[alpha] = True swapped[beta] = True basis[position[alpha]], basis[position[beta]] = basis[position[beta]], basis[position[alpha]] position[alpha], position[beta] = position[beta], position[alpha] if breaker == True: break x = alpha return [(x, y) for x,y in pairs] def pairs_to_mapping(pairs): mapping = [] for x, y in pairs: mapping.append(x) for x, y in reversed(pairs): mapping.append(y) return mapping def transform(basis, seq): basis = [x-1 for x in basis] people = [basis[x] for x in seq] result = [x for x in basis] for i, x in enumerate(seq): result[x] = people[len(seq)-1-i] return [x+1 for x in result] def outer_pipeline(basis): if basis == list(range(1, len(basis)+1)): return [] else: pairs = determine_pairs(basis) mapping = pairs_to_mapping(pairs) res = [mapping] next_system = transform(basis, mapping) if next_system != list(range(1, len(basis)+1)): pairs = determine_pairs(next_system) mapping = pairs_to_mapping(pairs) next_system = transform(next_system, mapping) res.append(mapping) return res n = int(input()) parts = [] for i in range(n): part = int(input()) parts.append((i, part)) parts = sorted(parts, key = lambda x: x[1]) basis = [0]*len(parts) for i,x in enumerate(parts): basis[x[0]] = i+1 res = outer_pipeline(basis) print(len(res)) _ = [[print(len(some_map))] and [print(x+1, end = ' ') for x in some_map] and print() for some_map in res] |
English