#include "xylophone.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
#define ALL(arr) begin(arr), end(arr)
#define CONTAINS(arr, val) (find(begin(arr), end(arr), val) != end(arr))
vector<vector<int>> poss;
vector<int> found;
vector<int> foundvals;
int binSearch(int bot, int top, int diff) {
int mid;
int low = bot, high = top;
while (low != high) {
mid = (low+high)/2;
int res = high;
if (mid != high)
res = query(bot, mid);
if (res == diff) high = mid;
else low = mid+1;
}
return low;
}
void solve(int N) {
poss.resize(N+1);
int n = 1;
int res = binSearch(1, N, N-1);
poss[res] = {N};
found.push_back(res);
answer(res, N);
foundvals.push_back(N);
while (n < N) {
int tmp = 0;
for (int iii = 0; iii < found.size(); iii++) {
int i = found[iii];
if (i != N && poss[i+1].size() == 0) {
tmp++;
int low, high;
int diff = query(i, i+1);
low = poss[i][0]-diff, high = poss[i][0]+diff;
int c = 0;
if (low >= 1 && !CONTAINS(foundvals, low)) {
c++;
poss[i+1].push_back(low);
}
if (high <= N && !CONTAINS(foundvals, high)) {
c++;
poss[i+1].push_back(high);
}
if (c == 1) {
answer(i+1, poss[i+1][0]);
found.push_back(i+1);
foundvals.push_back(poss[i+1][0]);
n++;
}
}
if (i != 1 && poss[i-1].size() == 0) {
tmp++;
int low, high;
int diff = query(i-1, i);
low = poss[i][0]-diff, high = poss[i][0]+diff;
int c = 0;
if (low >= 1 && !CONTAINS(foundvals, low)) {
c++;
poss[i-1].push_back(low);
}
if (high <= N && !CONTAINS(foundvals, high)) {
c++;
poss[i-1].push_back(high);
}
if (c == 1) {
answer(i-1, poss[i-1][0]);
found.push_back(i-1);
foundvals.push_back(poss[i-1][0]);
n++;
}
}
}
if (!tmp) {
for (int iii = 0; iii < found.size(); iii++) {
int i = found[iii];
if (i != 1 && i != N && (poss[i-1].size() > 1 ^ poss[i+1].size() > 1)) {
bool below = poss[i-1].size() > 1;
int res = query(i-1, i+1);
int i0 = i + (below ? 1 : -1);
int i0i = abs(poss[i][0] - poss[i + (below ? 1 : -1)][0]);
int ii1 = abs(poss[i][0] - poss[i + (below ? -1 : 1)][0]);
int corr;
if (res == i0i || ii1 == res) {
if (poss[i0][0] > poss[i][0])
corr = max(poss[i + (below ? -1 : 1)][0], poss[i + (below ? -1 : 1)][1]);
else corr = min(poss[i + (below ? -1 : 1)][0], poss[i + (below ? -1 : 1)][1]);
}
else {
if (poss[i0][0] > poss[i][0])
corr = min(poss[i + (below ? -1 : 1)][0], poss[i + (below ? -1 : 1)][1]);
else corr = max(poss[i + (below ? -1 : 1)][0], poss[i + (below ? -1 : 1)][1]);
}
poss[i + (below ? -1 : 1)].clear();
poss[i + (below ? -1 : 1)].push_back(corr);
foundvals.push_back(corr);
found.push_back(i + (below ? -1 : 1));
answer(i + (below ? -1 : 1), corr);
n++;
break;
}
}
}
}
}
Compilation message
xylophone.cpp: In function 'void solve(int)':
xylophone.cpp:40:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int iii = 0; iii < found.size(); iii++) {
| ~~~~^~~~~~~~~~~~~~
xylophone.cpp:86:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int iii = 0; iii < found.size(); iii++) {
| ~~~~^~~~~~~~~~~~~~
xylophone.cpp:88:47: warning: suggest parentheses around comparison in operand of '^' [-Wparentheses]
88 | if (i != 1 && i != N && (poss[i-1].size() > 1 ^ poss[i+1].size() > 1)) {
| ~~~~~~~~~~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
2 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
208 KB |
Output is correct |
12 |
Correct |
2 ms |
208 KB |
Output is correct |
13 |
Correct |
2 ms |
208 KB |
Output is correct |
14 |
Correct |
2 ms |
208 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
2 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
208 KB |
Output is correct |
12 |
Correct |
2 ms |
208 KB |
Output is correct |
13 |
Correct |
2 ms |
208 KB |
Output is correct |
14 |
Correct |
2 ms |
208 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
3 ms |
208 KB |
Output is correct |
17 |
Correct |
6 ms |
316 KB |
Output is correct |
18 |
Correct |
9 ms |
348 KB |
Output is correct |
19 |
Correct |
13 ms |
356 KB |
Output is correct |
20 |
Correct |
14 ms |
344 KB |
Output is correct |
21 |
Correct |
8 ms |
344 KB |
Output is correct |
22 |
Correct |
12 ms |
344 KB |
Output is correct |
23 |
Correct |
8 ms |
356 KB |
Output is correct |
24 |
Correct |
8 ms |
336 KB |
Output is correct |
25 |
Correct |
10 ms |
356 KB |
Output is correct |
26 |
Correct |
13 ms |
336 KB |
Output is correct |
27 |
Correct |
14 ms |
340 KB |
Output is correct |
28 |
Correct |
9 ms |
336 KB |
Output is correct |
29 |
Correct |
13 ms |
340 KB |
Output is correct |
30 |
Correct |
13 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
2 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
208 KB |
Output is correct |
12 |
Correct |
2 ms |
208 KB |
Output is correct |
13 |
Correct |
2 ms |
208 KB |
Output is correct |
14 |
Correct |
2 ms |
208 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
3 ms |
208 KB |
Output is correct |
17 |
Correct |
6 ms |
316 KB |
Output is correct |
18 |
Correct |
9 ms |
348 KB |
Output is correct |
19 |
Correct |
13 ms |
356 KB |
Output is correct |
20 |
Correct |
14 ms |
344 KB |
Output is correct |
21 |
Correct |
8 ms |
344 KB |
Output is correct |
22 |
Correct |
12 ms |
344 KB |
Output is correct |
23 |
Correct |
8 ms |
356 KB |
Output is correct |
24 |
Correct |
8 ms |
336 KB |
Output is correct |
25 |
Correct |
10 ms |
356 KB |
Output is correct |
26 |
Correct |
13 ms |
336 KB |
Output is correct |
27 |
Correct |
14 ms |
340 KB |
Output is correct |
28 |
Correct |
9 ms |
336 KB |
Output is correct |
29 |
Correct |
13 ms |
340 KB |
Output is correct |
30 |
Correct |
13 ms |
352 KB |
Output is correct |
31 |
Correct |
30 ms |
360 KB |
Output is correct |
32 |
Correct |
23 ms |
428 KB |
Output is correct |
33 |
Correct |
54 ms |
492 KB |
Output is correct |
34 |
Correct |
69 ms |
560 KB |
Output is correct |
35 |
Correct |
56 ms |
532 KB |
Output is correct |
36 |
Correct |
50 ms |
576 KB |
Output is correct |
37 |
Correct |
36 ms |
568 KB |
Output is correct |
38 |
Correct |
39 ms |
608 KB |
Output is correct |
39 |
Correct |
93 ms |
608 KB |
Output is correct |
40 |
Correct |
63 ms |
556 KB |
Output is correct |
41 |
Correct |
66 ms |
576 KB |
Output is correct |
42 |
Correct |
46 ms |
604 KB |
Output is correct |
43 |
Correct |
36 ms |
584 KB |
Output is correct |
44 |
Correct |
45 ms |
576 KB |
Output is correct |
45 |
Correct |
76 ms |
556 KB |
Output is correct |
46 |
Correct |
84 ms |
724 KB |
Output is correct |
47 |
Correct |
72 ms |
512 KB |
Output is correct |
48 |
Correct |
80 ms |
536 KB |
Output is correct |
49 |
Correct |
70 ms |
516 KB |
Output is correct |
50 |
Correct |
91 ms |
604 KB |
Output is correct |
51 |
Correct |
79 ms |
588 KB |
Output is correct |
52 |
Correct |
78 ms |
564 KB |
Output is correct |
53 |
Correct |
63 ms |
552 KB |
Output is correct |
54 |
Correct |
76 ms |
556 KB |
Output is correct |
55 |
Correct |
78 ms |
528 KB |
Output is correct |
56 |
Correct |
70 ms |
548 KB |
Output is correct |
57 |
Correct |
78 ms |
592 KB |
Output is correct |
58 |
Correct |
63 ms |
596 KB |
Output is correct |
59 |
Correct |
67 ms |
712 KB |
Output is correct |
60 |
Correct |
65 ms |
512 KB |
Output is correct |