#include "xylophone.h"
#include <bits/stdc++.h>
using namespace std;
int a[5001];
bool solved[5001];
void solve(int n) {
for (int i=0;i<5001;i++) {
a[i] = -1;
solved[i] = false;
}
int bottom = 1, top = n, middle, diff, diff2;
while (bottom + 1 < top) {
middle = (top + bottom) / 2;
diff = query(1, middle);
if (diff == n - 1) {
top = middle;
} else {
bottom = middle;
}
}
a[top] = n; solved[n] = true; answer(top, n);
if (top != n) {
a[top + 1] = n - query(top, top + 1);
solved[a[top + 1]] = true; answer(top + 1, a[top + 1]);
}
a[top - 1] = n - query(top - 1, top);
solved[a[top - 1]] = true; answer(top - 1, a[top - 1]);
for (int i=top+2;i<n+1;i++) {
diff = query(i - 1, i);
if (a[i - 1] - diff < 1 || solved[a[i - 1] - diff]) {
a[i] = a[i - 1] + diff;
} else if (a[i - 1] + diff > n || solved[a[i - 1] + diff]) {
a[i] = a[i - 1] - diff;
} else {
diff2 = query(i - 2, i);
if (a[i - 2] > a[i - 1]) {
if (diff2 == a[i - 2] - a[i - 1] + diff) {
a[i] = a[i - 1] - diff;
} else {
a[i] = a[i - 1] + diff;
}
} else {
if (diff2 == a[i - 1] - a[i - 2] + diff) {
a[i] = a[i - 1] + diff;
} else {
a[i] = a[i - 1] - diff;
}
}
}
solved[a[i]] = true; answer(i, a[i]);
}
for (int i=top-2;i>0;i--) {
diff = query(i, i + 1);
if (a[i + 1] - diff < 1 || solved[a[i + 1] - diff]) {
a[i] = a[i + 1] + diff;
} else if (a[i + 1] + diff > n || solved[a[i + 1] + diff]) {
a[i] = a[i + 1] - diff;
} else {
diff2 = query(i, i + 2);
if (a[i + 2] > a[i + 1]) {
if (diff2 == a[i + 2] - a[i + 1] + diff) {
a[i] = a[i + 1] - diff;
} else {
a[i] = a[i + 1] + diff;
}
} else {
if (diff2 == a[i + 1] - a[i + 2] + diff) {
a[i] = a[i + 1] + diff;
} else {
a[i] = a[i + 1] - diff;
}
}
}
solved[a[i]] = true; answer(i, a[i]);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
4 ms |
344 KB |
Output is correct |
18 |
Correct |
4 ms |
344 KB |
Output is correct |
19 |
Correct |
7 ms |
344 KB |
Output is correct |
20 |
Correct |
7 ms |
344 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
344 KB |
Output is correct |
23 |
Correct |
4 ms |
344 KB |
Output is correct |
24 |
Correct |
4 ms |
344 KB |
Output is correct |
25 |
Correct |
4 ms |
344 KB |
Output is correct |
26 |
Correct |
4 ms |
344 KB |
Output is correct |
27 |
Correct |
4 ms |
344 KB |
Output is correct |
28 |
Correct |
5 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
344 KB |
Output is correct |
30 |
Correct |
7 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
4 ms |
344 KB |
Output is correct |
18 |
Correct |
4 ms |
344 KB |
Output is correct |
19 |
Correct |
7 ms |
344 KB |
Output is correct |
20 |
Correct |
7 ms |
344 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
344 KB |
Output is correct |
23 |
Correct |
4 ms |
344 KB |
Output is correct |
24 |
Correct |
4 ms |
344 KB |
Output is correct |
25 |
Correct |
4 ms |
344 KB |
Output is correct |
26 |
Correct |
4 ms |
344 KB |
Output is correct |
27 |
Correct |
4 ms |
344 KB |
Output is correct |
28 |
Correct |
5 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
344 KB |
Output is correct |
30 |
Correct |
7 ms |
344 KB |
Output is correct |
31 |
Correct |
12 ms |
344 KB |
Output is correct |
32 |
Correct |
23 ms |
344 KB |
Output is correct |
33 |
Correct |
29 ms |
344 KB |
Output is correct |
34 |
Correct |
27 ms |
344 KB |
Output is correct |
35 |
Correct |
30 ms |
596 KB |
Output is correct |
36 |
Correct |
28 ms |
344 KB |
Output is correct |
37 |
Correct |
28 ms |
344 KB |
Output is correct |
38 |
Correct |
22 ms |
340 KB |
Output is correct |
39 |
Correct |
27 ms |
504 KB |
Output is correct |
40 |
Correct |
30 ms |
596 KB |
Output is correct |
41 |
Correct |
29 ms |
344 KB |
Output is correct |
42 |
Correct |
23 ms |
344 KB |
Output is correct |
43 |
Correct |
27 ms |
344 KB |
Output is correct |
44 |
Correct |
14 ms |
344 KB |
Output is correct |
45 |
Correct |
34 ms |
344 KB |
Output is correct |
46 |
Correct |
36 ms |
344 KB |
Output is correct |
47 |
Correct |
26 ms |
344 KB |
Output is correct |
48 |
Correct |
27 ms |
596 KB |
Output is correct |
49 |
Correct |
23 ms |
344 KB |
Output is correct |
50 |
Correct |
20 ms |
340 KB |
Output is correct |
51 |
Correct |
23 ms |
344 KB |
Output is correct |
52 |
Correct |
30 ms |
344 KB |
Output is correct |
53 |
Correct |
25 ms |
344 KB |
Output is correct |
54 |
Correct |
18 ms |
344 KB |
Output is correct |
55 |
Correct |
26 ms |
344 KB |
Output is correct |
56 |
Correct |
26 ms |
344 KB |
Output is correct |
57 |
Correct |
33 ms |
344 KB |
Output is correct |
58 |
Correct |
36 ms |
344 KB |
Output is correct |
59 |
Correct |
16 ms |
344 KB |
Output is correct |
60 |
Correct |
22 ms |
344 KB |
Output is correct |