-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathAbsolute Permutation.cpp
124 lines (121 loc) · 4 KB
/
Absolute Permutation.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
// While this solution works, there is a much more elegant solution I found after reading the editorial.
// If N is even, then we simply need to consider N as a bunch 2*K slots. Solve the problem
// for each "slot" instead of solving for N directly.
int main() {
int T;
cin>>T;
while(T > 0) {
long long N, K;
cin>>N>>K;
// Algorithm basically reduces down to either an edge case or an outside->inside approach
if(K == 0) {
for(long long i = 1; i <= N; i++) {
cout<<i<<" ";
}
cout<<"\n";
T--;
continue;
}
if(K > N/2 || N%2 == 1) {
cout<<"-1\n";
T--;
continue;
}
// *sigh* here we go...
bool possible = true;
bool *used = new bool[N+1];
long long *answer = new long long[N];
memset(used, false, sizeof(used));
for(long long i = 1; i <= N/2; i++) {
// Solve left index
if(i+K <= N && i-K >= 1) {
if(!used[i+K] && !used[i-K]) {
used[i-K] = true;
answer[i-1] = i-K;
} else if(!used[i+K] && used[i-K]) {
used[i+K] = true;
answer[i-1] = i+K;
} else if(used[i+K] && !used[i-K]) {
used[i-K] = true;
answer[i-1] = i-K;
} else {
possible = false;
break;
}
} else if(i+K > N && i-K >= 1) {
if(!used[i-K]) {
used[i-K] = true;
answer[i-1] = i-K;
} else {
possible = false;
break;
}
} else if(i+K <= N && i-K <= 1) {
if(!used[i+K]) {
used[i+K] = true;
answer[i-1] = i+K;
} else {
possible = false;
break;
}
} else {
possible = false;
break;
}
// Solve right index
long long rightIndex = N-i+1;
if(rightIndex+K <= N && rightIndex-K >= 1) {
if(!used[rightIndex+K] && !used[rightIndex-K]) {
used[rightIndex+K] = true;
answer[rightIndex-1] = rightIndex+K;
} else if(!used[rightIndex+K] && used[rightIndex-K]) {
used[rightIndex+K] = true;
answer[rightIndex-1] = rightIndex+K;
} else if(used[rightIndex+K] && !used[rightIndex-K]) {
used[rightIndex-K] = true;
answer[rightIndex-1] = rightIndex-K;
} else {
possible = false;
break;
}
} else if(rightIndex+K > N && rightIndex-K >= 1) {
if(!used[rightIndex-K]) {
used[rightIndex-K] = true;
answer[rightIndex-1] = rightIndex-K;
} else {
possible = false;
break;
}
} else if(rightIndex+K <= N && rightIndex-K <= 1) {
if(!used[rightIndex+K]) {
used[rightIndex+K] = true;
answer[rightIndex-1] = rightIndex+K;
} else {
possible = false;
break;
}
} else {
possible = false;
break;
}
}
// I can start to see why some people may not want to do this for a living...
if(!possible) {
cout<<"-1";
} else {
for(int i = 0; i < N; i++) {
cout<<answer[i]<<" ";
}
}
cout<<"\n";
T--;
}
return 0;
}