#include "xylophone.h"
#include "bits/stdc++.h"
using namespace std;
int q[5005][5005], a[5005];
bool check_permutation(int n) {
bool b[n+1], pos[n+1];
memset(b, 0, sizeof(b));
for (int i = 1; i <= n; ++i) if (a[i] <= n) b[a[i]] = 1;
for (int i = 1; i <= n; ++i) if (!b[i]) return false;
int pos1, pos2;
for (int i = 1; i <= n; ++i) {
if (a[i] == 1) pos1 = i;
if (a[i] == n) pos2 = i;
}
if (pos1 > pos2) return false;
for (int i = 1; i < n; ++i) if (abs(a[i+1] - a[i]) != q[i][i+1]) return false;
for (int i = 1; i < n-1; ++i)
if (max(a[i], max(a[i+1], a[i+2])) - min(a[i], min(a[i+1], a[i+2])) != q[i][i+2]) return false;
return true;
}
void build_permutation(int n, int x, int y) {
a[1] = x;
a[2] = y;
for (int i = 3; i <= n; ++i) {
if (q[i-2][i-1] + q[i-1][i] == q[i-2][i]) {
if (a[i-2] > a[i-1]) a[i] = a[i-1] - q[i-1][i];
else a[i] = a[i-1] + q[i-1][i];
} else {
if (a[i-2] > a[i-1]) a[i] = a[i-1] + q[i-1][i];
else a[i] = a[i-1] - q[i-1][i];
}
}
int mi = *min_element(a+1, a+n+1) - 1;
for (int i = 1; i <= n; ++i) a[i] -= mi;
}
void solve(int n) {
for (int i = 1; i <= n-1; ++i) q[i][i+1] = q[i+1][i] = query(i, i+1);
for (int i = 1; i <= n-2; ++i) q[i][i+2] = q[i+2][i] = query(i, i+2);
for (int i = 1; i < n; ++i) {
build_permutation(n, 10000, 10000 + i);
if (check_permutation(n)) {
for (int i = 1; i <= n; ++i) answer(i, a[i]);
return;
}
}
for (int i = 1; i < n; ++i) {
build_permutation(n, 10000 + i, 10000);
if (check_permutation(n)) {
for (int i = 1; i <= n; ++i) answer(i, a[i]);
return;
}
}
}
Compilation message
xylophone.cpp: In function 'bool check_permutation(int)':
xylophone.cpp:6:18: warning: unused variable 'pos' [-Wunused-variable]
6 | bool b[n+1], pos[n+1];
| ^~~
xylophone.cpp:15:5: warning: 'pos1' may be used uninitialized in this function [-Wmaybe-uninitialized]
15 | if (pos1 > pos2) return false;
| ^~
xylophone.cpp:15:5: warning: 'pos2' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
3 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
2 ms |
616 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
636 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
3 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
2 ms |
616 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
636 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
5 ms |
1104 KB |
Output is correct |
17 |
Correct |
10 ms |
2204 KB |
Output is correct |
18 |
Correct |
18 ms |
3640 KB |
Output is correct |
19 |
Correct |
26 ms |
4340 KB |
Output is correct |
20 |
Correct |
24 ms |
4304 KB |
Output is correct |
21 |
Correct |
21 ms |
4256 KB |
Output is correct |
22 |
Correct |
17 ms |
4356 KB |
Output is correct |
23 |
Correct |
17 ms |
4336 KB |
Output is correct |
24 |
Correct |
17 ms |
4236 KB |
Output is correct |
25 |
Correct |
24 ms |
4344 KB |
Output is correct |
26 |
Correct |
13 ms |
4236 KB |
Output is correct |
27 |
Correct |
17 ms |
4236 KB |
Output is correct |
28 |
Correct |
22 ms |
4348 KB |
Output is correct |
29 |
Correct |
25 ms |
4348 KB |
Output is correct |
30 |
Correct |
23 ms |
4280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
3 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
2 ms |
616 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
636 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
5 ms |
1104 KB |
Output is correct |
17 |
Correct |
10 ms |
2204 KB |
Output is correct |
18 |
Correct |
18 ms |
3640 KB |
Output is correct |
19 |
Correct |
26 ms |
4340 KB |
Output is correct |
20 |
Correct |
24 ms |
4304 KB |
Output is correct |
21 |
Correct |
21 ms |
4256 KB |
Output is correct |
22 |
Correct |
17 ms |
4356 KB |
Output is correct |
23 |
Correct |
17 ms |
4336 KB |
Output is correct |
24 |
Correct |
17 ms |
4236 KB |
Output is correct |
25 |
Correct |
24 ms |
4344 KB |
Output is correct |
26 |
Correct |
13 ms |
4236 KB |
Output is correct |
27 |
Correct |
17 ms |
4236 KB |
Output is correct |
28 |
Correct |
22 ms |
4348 KB |
Output is correct |
29 |
Correct |
25 ms |
4348 KB |
Output is correct |
30 |
Correct |
23 ms |
4280 KB |
Output is correct |
31 |
Correct |
41 ms |
9072 KB |
Output is correct |
32 |
Correct |
155 ms |
12816 KB |
Output is correct |
33 |
Correct |
153 ms |
17864 KB |
Output is correct |
34 |
Correct |
423 ms |
20680 KB |
Output is correct |
35 |
Correct |
464 ms |
20568 KB |
Output is correct |
36 |
Correct |
97 ms |
20552 KB |
Output is correct |
37 |
Correct |
354 ms |
20564 KB |
Output is correct |
38 |
Correct |
331 ms |
20592 KB |
Output is correct |
39 |
Correct |
89 ms |
20528 KB |
Output is correct |
40 |
Correct |
400 ms |
20648 KB |
Output is correct |
41 |
Correct |
481 ms |
20572 KB |
Output is correct |
42 |
Correct |
93 ms |
20576 KB |
Output is correct |
43 |
Correct |
91 ms |
20604 KB |
Output is correct |
44 |
Correct |
380 ms |
20576 KB |
Output is correct |
45 |
Correct |
225 ms |
20556 KB |
Output is correct |
46 |
Correct |
360 ms |
20508 KB |
Output is correct |
47 |
Correct |
120 ms |
20488 KB |
Output is correct |
48 |
Correct |
374 ms |
20532 KB |
Output is correct |
49 |
Correct |
128 ms |
20520 KB |
Output is correct |
50 |
Correct |
320 ms |
20576 KB |
Output is correct |
51 |
Correct |
238 ms |
20576 KB |
Output is correct |
52 |
Correct |
269 ms |
20508 KB |
Output is correct |
53 |
Correct |
408 ms |
20512 KB |
Output is correct |
54 |
Correct |
122 ms |
20572 KB |
Output is correct |
55 |
Correct |
489 ms |
20572 KB |
Output is correct |
56 |
Correct |
599 ms |
20576 KB |
Output is correct |
57 |
Correct |
160 ms |
20528 KB |
Output is correct |
58 |
Correct |
620 ms |
20512 KB |
Output is correct |
59 |
Correct |
474 ms |
20556 KB |
Output is correct |
60 |
Correct |
224 ms |
20572 KB |
Output is correct |