G. Special Permutation Codeforces Round 640 (Div. 4)

A permutation of length n� is an array p=[p1,p2,…,pn]�=[�1,�2,…,��], which contains every integer from 11 to n� (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5]�=[3,1,4,2,5] is a permutation of length 55.

For a given number n� (n≥2�≥2), find a permutation p� in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 22 and 44, inclusive. Formally, find such permutation p� that 2≤|pi−pi+1|≤42≤|��−��+1|≤4 for each i� (1≤i<n1≤�<�).

Print any such permutation for the given integer n� or determine that it does not exist.

Input

The first line contains an integer t� (1≤t≤1001≤�≤100) — the number of test cases in the input. Then t� test cases follow.

Each test case is described by a single line containing an integer n� (2≤n≤10002≤�≤1000).

Output

Print t� lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.

题目大致意思:n个数排序,相邻的两个差值为2~4。问能不能排成功。

写法很多,这里只是其中一种。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n"

ll n,m;

void solve(){
	cin >> n;
	if(n < 4){
		cout << -1 << endl;
		return;
	}
	if(n == 4){
		cout << "3 1 4 2" << endl;
		return ;
	}
	ll x,y;
	if(n%2 == 0)x=n-1,y=n;
	else x=n,y=n-1;
	for(;y>0;y -= 2)
		cout << y << " ";
	cout << "5 1";
	for(ll i = 3 ; i <= x ; i += 2)
		if(i == 5)continue;
		else cout << " " << i;
	cout << endl;
	return;
}

int main(){
	ll t=1;cin >> t;
	while(t--)solve();
	return 0;
}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值