CF1741B.Funny Permutation

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A sequence of nn numbers is called permutation if it contains all numbers from 11 to nn exactly once. For example, the sequences [3,1,4,2][3, 1, 4, 2], [11] and [2,1][2,1] are permutations, but [1,2,1][1,2,1], [0,1][0,1] and [1,3,4][1,3,4] are not.

For a given number nn you need to make a permutation pp such that two requirements are satisfied at the same time:

  • For each element pip_i, at least one of its neighbors has a value that differs from the value of pip_i by one. That is, for each element pip_i (1≤i≤n1 \le i \le n), at least one of its neighboring elements (standing to the left or right of pip_i) must be pi+1p_i + 1, or pi−1p_i - 1.
  • the permutation must have no fixed points. That is, for every ii (1≤i≤n1 \le i \le n), pi≠ip_i \neq i must be satisfied.

Let's call the permutation that satisfies these requirements funny.

For example, let n=4n = 4. Then [4,3,1,24, 3, 1, 2] is a funny permutation, since:

  • to the right of p1=4p_1=4 is p2=p1−1=4−1=3p_2=p_1-1=4-1=3;
  • to the left of p2=3p_2=3 is p1=p2+1=3+1=4p_1=p_2+1=3+1=4;
  • to the right of p3=1p_3=1 is p4=p3+1=1+1=2p_4=p_3+1=1+1=2;
  • to the left of p4=2p_4=2 is p3=p4−1=2−1=1p_3=p_4-1=2-1=1.
  • for all ii is pi≠ip_i \ne i.

For a given positive integer nn, output any funny permutation of length nn, or output -1 if funny permutation of length nn does not exist.

一个包含 nn 个数的序列被称为排列,当且仅当它恰好包含从 11 到 nn 的所有整数各一次。例如,序列 [3,1,4,2][3, 1, 4, 2]、[11] 和 [2,1][2,1] 是排列,但 [1,2,1][1,2,1]、[0,1][0,1] 和 [1,3,4][1,3,4] 不是。

给定正整数 nn,你需要构造一个排列 pp,使其同时满足以下两个条件:

  • 对于每个元素 pip_i,其至少一个相邻元素(即位于 pip_i 左侧或右侧的元素)的值与 pip_i 相差恰好为 11。即:对每个 pip_i(其中 1≤i≤n1 \le i \le n),其左侧或右侧的某个邻接元素必须等于 pi+1p_i + 1 或 pi−1p_i - 1。
  • 该排列不能有不动点(fixed point),即对每个 ii(其中 1≤i≤n1 \le i \le n),都必须满足 pi≠ip_i \neq i。

我们称满足上述两个条件的排列为有趣的排列(funny permutation)。

例如,当 n=4n = 4 时,[4,3,1,24, 3, 1, 2] 是一个有趣的排列,因为:

  • p1=4p_1=4 的右侧邻居是 p2=p1−1=4−1=3p_2=p_1-1=4-1=3;
  • p2=3p_2=3 的左侧邻居是 p1=p2+1=3+1=4p_1=p_2+1=3+1=4;
  • p3=1p_3=1 的右侧邻居是 p4=p3+1=1+1=2p_4=p_3+1=1+1=2;
  • p4=2p_4=2 的左侧邻居是 p3=p4−1=2−1=1p_3=p_4-1=2-1=1;
  • 对所有 ii,均有 pi≠ip_i \ne i。

对于给定的正整数 nn,请输出任意一个长度为 nn 的有趣的排列;若不存在这样的排列,则输出 −1-1。

输入格式

The first line of input data contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The description of the test cases follows.

Each test case consists of f single line containing one integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5).

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入数据的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

接下来是各测试用例的描述。

每个测试用例由一行组成,该行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print on a separate line:

  • any funny permutation pp of length nn;
  • or the number -1 if the permutation you are looking for does not exist.

对于每个测试用例,在单独一行中输出:

  • 任意一个长度为 nn 的有趣排列 pp;
  • 或者如果所求的排列不存在,则输出数字 −1-1。

输入输出样例

  • 输入#1

    5
    4
    3
    7
    5
    2

    输出#1

    3 4 2 1
    -1
    6 7 4 5 3 2 1
    5 4 1 2 3
    2 1

说明/提示

The first test case is explained in the problem statement.

In the second test case, it is not possible to make the required permutation: permutations [1,2,3][1, 2, 3], [1,3,2][1, 3, 2], [2,1,3][2, 1, 3], [3,2,1][3, 2, 1] have fixed points, and in [2,3,1][2, 3, 1] and [3,1,2][3, 1, 2] the first condition is met not for all positions.

第一个测试用例已在题目描述中说明。

在第二个测试用例中,无法构造出满足要求的排列:排列 [1,2,3][1, 2, 3]、[1,3,2][1, 3, 2]、[2,1,3][2, 1, 3]、[3,2,1][3, 2, 1] 均存在不动点;而在 [2,3,1][2, 3, 1] 和 [3,1,2][3, 1, 2] 中,第一个条件并非对所有位置都成立。

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

首页