[Solution] Permutation Chain Codeforces Solution
A permutation of length is a sequence of integers from to such that each integer appears in it exactly once.
Let the fixedness of a permutation be the number of fixed points in it — the number of positions such that , where is the -th element of the permutation .
You are asked to build a sequence of permutations , starting from the identity permutation (permutation ). Let's call it a permutation chain. Thus, is the -th permutation of length .
occupied
For every from onwards, the permutation should be obtained from the permutation by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation should be strictly lower than the fixedness of the permutation .
Consider some chains for :
- , — that is a valid chain of length . From to , the elements on positions and get swapped, the fixedness decrease from to .
- , — that is not a valid chain. The first permutation should always be for .
- , , — that is not a valid chain. From to , the elements on positions and get swapped but the fixedness increase from to .
- , , — that is a valid chain of length . From to , the elements on positions and get swapped, the fixedness decrease from to . From to , the elements on positions and get swapped, the fixedness decrease from to .
Find the longest permutation chain. If there are multiple longest answers, print any of them.
The first line contains a single integer () — the number of testcases.
The only line of each testcase contains a single integer () — the required length of permutations in the chain.
For each testcase, first, print the length of a permutation chain .
Then print permutations . should be an identity permutation of length (). For each from to , should be obtained by swapping two elements in . It should also have a strictly lower fixedness than .
No comments:
Post a Comment