forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1541A - Pretty Permutations.cpp
110 lines (66 loc) · 2.23 KB
/
1541A - Pretty Permutations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
There are n cats in a line, labeled from 1 to n, with the i-th cat at position i. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.
For example, if there are 3 cats, this is a valid reordering: [3,1,2]. No cat is in its original position. The total distance the cats move is 1+1+2=4 as cat 1 moves one place to the right, cat 2 moves one place to the right, and cat 3 moves two places to the left.
Input
The first line contains a single integer t (1=t=100) — the number of test cases. Then t test cases follow.
The first and only line of each test case contains one integer n (2=n=100) — the number of cats.
It can be proven that under the constraints of the problem, an answer always exist.
Output
Output t answers, one for each test case. Each answer consists of n integers — a permutation with the minimum total distance. If there are multiple answers, print any.
Example
inputCopy
2
2
3
outputCopy
2 1
3 1 2
Note
For the first test case, there is only one possible permutation that satisfies the conditions: [2,1].
The second test case was described in the statement. Another possible answer is [2,3,1].
*/
#include<bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
if (n % 2 == 0) {
for (int i = 2; i <= n; i += 2) cout << i << " " << i - 1 << " ";
} else {
for (int i = 2; i <= n - 3; i += 2) cout << i << " " << i - 1 << " ";
cout << n - 1 << " " << n << " " << n - 2;
}
cout << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
/*
#include<bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector <int> v(n);
for (int i = 0; i < n; i++) v[i] = i + 1;
for (int i = 0; i < n - 1; i += 2) swap(v[i], v[i + 1]);
if (n % 2) {
swap(v[n - 1], v[n - 2]);
}
for (auto &x: v) cout << x << " ";
cout << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
*/