Permutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 179 Accepted Submission(s): 132
Special Judge
Problem Description
A permutation
p1,p2,...,pn
of
1,2,...,n
is called a lucky permutation if and only if
pi≡0(mod|pi−pi−2|)
for
i=3...n
.
Now you need to construct a lucky permutation with a given n .
Now you need to construct a lucky permutation with a given n .
Input
The first line is the number of test cases.
For each test case, one single line contains a positive integer n(3≤n≤105) .
For each test case, one single line contains a positive integer n(3≤n≤105) .
Output
For each test case, output a single line with
n
numbers
p1,p2,...,pn
.
It is guaranteed that there exists at least one solution. And if there are different solutions, print any one of them.
It is guaranteed that there exists at least one solution. And if there are different solutions, print any one of them.
Sample Input
1 6
Sample Output
1 3 2 6 4 5
题意:构造满足pi%(|pi-pi-2|)==0的序列。
思路:i和i+2的奇偶性是相同的,所以奇偶序列是互不干扰的。根据pi%(|pi-pi-2|)==0推出,只要奇子序列的数和偶子序列的数分别是相邻的自然数就行了。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int num1=1,num2=(n+1)/2+1,i=1;
while(i<=n)
{
if(i%2==1)
{
if(i==1)
printf("%d",num1++),i++;
else
printf(" %d",num1++),i++;
}
if(i%2==0&&i<=n)
printf(" %d",num2++),i++;
}
printf("\n");
}
return 0;
}