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;
}