Funny Permutation

Funny Permutation

 

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 p_ipi​, at least one of its neighbors has a value that differs from the value of p_ipi​ by one. That is, for each element p_ipi​ (1 \le i \le n1≤i≤n), at least one of its neighboring elements (standing to the left or right of p_ipi​) must be p_i + 1pi​+1, or p_i - 1pi​−1.
  • the permutation must have no fixed points. That is, for every ii (1 \le i \le n1≤i≤n), p_i \neq ipi​=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 p_1=4p1​=4 is p_2=p_1-1=4-1=3p2​=p1​−1=4−1=3;
  • to the left of p_2=3p2​=3 is p_1=p_2+1=3+1=4p1​=p2​+1=3+1=4;
  • to the right of p_3=1p3​=1 is p_4=p_3+1=1+1=2p4​=p3​+1=1+1=2;
  • to the left of p_4=2p4​=2 is p_3=p_4-1=2-1=1p3​=p4​−1=2−1=1.
  • for all ii is p_i \ne ipi​=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.

Input

The first line of input data contains a single integer tt (1 \le t \le 10^41≤t≤104) — 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 \le n \le 2 \cdot 10^52≤n≤2⋅105).

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

Output

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.

Sample 1

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

题解:注意到把1 2 分别放到倒数第二,第一的位置,其余数字按序输出即可满足条件。

特别地,n=3时不满足,单独讨论。

#include <iostream>
using namespace std;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		if (n == 3)
		{
			cout << -1 << endl;
		}
		else
		{
			for (int i = 3; i <= n; i++)
			{
				cout << i << " ";
			}
			cout << 2 << " " << 1 << endl;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linalw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值