CF1716B.Permutation Chain

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A permutation of length nn is a sequence of integers from 11 to nn such that each integer appears in it exactly once.

Let the fixedness of a permutation pp be the number of fixed points in it — the number of positions jj such that pj=jp_j = j, where pjp_j is the jj-th element of the permutation pp.

You are asked to build a sequence of permutations a1,a2,…a_1, a_2, \dots, starting from the identity permutation (permutation a1=[1,2,…,n]a_1 = [1, 2, \dots, n]). Let's call it a permutation chain. Thus, aia_i is the ii-th permutation of length nn.

For every ii from 22 onwards, the permutation aia_i should be obtained from the permutation ai−1a_{i-1} by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation aia_i should be strictly lower than the fixedness of the permutation ai−1a_{i-1}.

Consider some chains for n=3n = 3:

  • a1=[1,2,3]a_1 = [1, 2, 3], a2=[1,3,2]a_2 = [1, 3, 2] — that is a valid chain of length 22. From a1a_1 to a2a_2, the elements on positions 22 and 33 get swapped, the fixedness decrease from 33 to 11.
  • a1=[2,1,3]a_1 = [2, 1, 3], a2=[3,1,2]a_2 = [3, 1, 2] — that is not a valid chain. The first permutation should always be [1,2,3][1, 2, 3] for n=3n = 3.
  • a1=[1,2,3]a_1 = [1, 2, 3], a2=[1,3,2]a_2 = [1, 3, 2], a3=[1,2,3]a_3 = [1, 2, 3] — that is not a valid chain. From a2a_2 to a3a_3, the elements on positions 22 and 33 get swapped but the fixedness increase from 11 to 33.
  • a1=[1,2,3]a_1 = [1, 2, 3], a2=[3,2,1]a_2 = [3, 2, 1], a3=[3,1,2]a_3 = [3, 1, 2] — that is a valid chain of length 33. From a1a_1 to a2a_2, the elements on positions 11 and 33 get swapped, the fixedness decrease from 33 to 11. From a2a_2 to a3a_3, the elements on positions 22 and 33 get swapped, the fixedness decrease from 11 to 00.

Find the longest permutation chain. If there are multiple longest answers, print any of them.

长度为 nn 的一个排列是指由 11 到 nn 的整数组成的序列,其中每个整数恰好出现一次。

定义一个排列 pp 的**不动点数(fixedness)**为该排列中不动点的个数——即满足 pj=jp_j = j 的位置 jj 的个数,其中 pjp_j 表示排列 pp 的第 jj 个元素。

你需要构造一个排列序列 a1,a2,…a_1, a_2, \dots,起始于恒等排列(即 a1=[1,2,…,n]a_1 = [1, 2, \dots, n])。我们称其为一个排列链(permutation chain)。因此,aia_i 是长度为 nn 的第 ii 个排列。

对每个 i≥2i \geq 2,排列 aia_i 必须由排列 ai−1a_{i-1} 通过交换其中任意两个元素(不一定是相邻元素)得到;且 aia_i 的不动点数必须严格小于 ai−1a_{i-1} 的不动点数。

考虑 n=3n = 3 的一些例子:

  • a1=[1,2,3]a_1 = [1, 2, 3],a2=[1,3,2]a_2 = [1, 3, 2] —— 这是一个长度为 22 的合法链。从 a1a_1 到 a2a_2,位置 22 和 33 上的元素被交换,不动点数从 33 减少到 11。
  • a1=[2,1,3]a_1 = [2, 1, 3],a2=[3,1,2]a_2 = [3, 1, 2] —— 这不是一个合法链。对于 n=3n = 3,第一个排列必须始终是 [1,2,3][1, 2, 3]。
  • a1=[1,2,3]a_1 = [1, 2, 3],a2=[1,3,2]a_2 = [1, 3, 2],a3=[1,2,3]a_3 = [1, 2, 3] —— 这不是一个合法链。从 a2a_2 到 a3a_3,位置 22 和 33 上的元素被交换,但不动点数从 11 增加到了 33。
  • a1=[1,2,3]a_1 = [1, 2, 3],a2=[3,2,1]a_2 = [3, 2, 1],a3=[3,1,2]a_3 = [3, 1, 2] —— 这是一个长度为 33 的合法链。从 a1a_1 到 a2a_2,位置 11 和 33 上的元素被交换,不动点数从 33 减少到 11;从 a2a_2 到 a3a_3,位置 22 和 33 上的元素被交换,不动点数从 11 减少到 00。

请找出最长的排列链。如果存在多个最长的链,输出任意一个即可。

输入格式

The first line contains a single integer tt (1≤t≤991 \le t \le 99) — the number of testcases.

The only line of each testcase contains a single integer nn (2≤n≤1002 \le n \le 100) — the required length of permutations in the chain.

第一行包含一个整数 tt(1≤t≤991 \le t \le 99)—— 测试用例的数量。

每个测试用例仅有一行,包含一个整数 nn(2≤n≤1002 \le n \le 100)—— 所需链中排列的长度。

输出格式

For each testcase, first, print the length of a permutation chain kk.

Then print kk permutations a1,a2,…,aka_1, a_2, \dots, a_k. a1a_1 should be an identity permutation of length nn ([1,2,…,n][1, 2, \dots, n]). For each ii from 22 to kk, aia_i should be obtained by swapping two elements in ai−1a_{i-1}. It should also have a strictly lower fixedness than ai−1a_{i-1}.

对于每个测试用例,首先输出一个排列链的长度 kk。

然后输出 kk 个排列 a1,a2,…,aka_1, a_2, \dots, a_k。其中 a1a_1 应为长度为 nn 的单位排列(即 [1,2,…,n][1, 2, \dots, n])。对每个从 22 到 kk 的 ii,aia_i 应通过交换 ai−1a_{i-1} 中的两个元素得到,且其固定点数(fixedness)必须严格小于 ai−1a_{i-1} 的固定点数。

输入输出样例

  • 输入#1

    2
    2
    3

    输出#1

    2
    1 2
    2 1
    3
    1 2 3
    3 2 1
    3 1 2

输入解题思路,AI测评打分。不知道怎么写?

首页