#include <bits/stdc++.h>
using namespace std;
int query(int s, int t);
void answer(int i, int a);
void solve(int N) {
vector<int> ans (N+1);
vector<bool> used (N+1);
int l = 1, r = N;
while (l+1 < r) {
int mid = (l+r)/2;
if (query(mid, N) == N-1) l = mid;
else r = mid;
}
ans[l] = 1;
used[1] = true;
if (l > 1) {
ans[l-1] = ans[l] + query(l-1, l);
used[ans[l-1]] = true;
}
if (l < N) {
ans[l+1] = ans[l] + query(l, l+1);
used[ans[l+1]] = true;
}
for (int i = l-2; i > 0; i--) {
int q = query(i, i+1);
if (ans[i+1] + q > N || used[ans[i+1] + q]) ans[i] = ans[i+1] - q;
else if (ans[i+1] - q < 1 || used[ans[i+1] - q]) ans[i] = ans[i+1] + q;
else {
int q2 = query(i, i+2);
ans[i] = ans[i+1] - q;
if (abs(ans[i] - ans[i+1]) != q || max({ans[i], ans[i+1], ans[i+2]}) - min({ans[i], ans[i+1], ans[i+2]}) != q2) ans[i] = ans[i+1] + q;
}
used[ans[i]] = true;
}
for (int i = l+2; i <= N; i++) {
int q = query(i-1, i);
if (ans[i-1] + q > N || used[ans[i-1] + q]) ans[i] = ans[i-1] - q;
else if (ans[i-1] - q < 1 || used[ans[i-1] - q]) ans[i] = ans[i-1] + q;
else {
int q2 = query(i-2, i);
ans[i] = ans[i-1] - q;
if (abs(ans[i] - ans[i-1]) != q || max({ans[i], ans[i-1], ans[i-2]}) - min({ans[i], ans[i-1], ans[i-2]}) != q2) ans[i] = ans[i-1] + q;
}
used[ans[i]] = true;
}
for (int i = 1; i <= N; i++) answer(i, ans[i]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 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 |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
4 ms |
440 KB |
Output is correct |
19 |
Correct |
5 ms |
436 KB |
Output is correct |
20 |
Correct |
5 ms |
440 KB |
Output is correct |
21 |
Correct |
5 ms |
440 KB |
Output is correct |
22 |
Correct |
5 ms |
440 KB |
Output is correct |
23 |
Correct |
4 ms |
440 KB |
Output is correct |
24 |
Correct |
4 ms |
440 KB |
Output is correct |
25 |
Correct |
4 ms |
440 KB |
Output is correct |
26 |
Correct |
5 ms |
440 KB |
Output is correct |
27 |
Correct |
6 ms |
600 KB |
Output is correct |
28 |
Correct |
6 ms |
444 KB |
Output is correct |
29 |
Correct |
5 ms |
436 KB |
Output is correct |
30 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 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 |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
4 ms |
440 KB |
Output is correct |
19 |
Correct |
5 ms |
436 KB |
Output is correct |
20 |
Correct |
5 ms |
440 KB |
Output is correct |
21 |
Correct |
5 ms |
440 KB |
Output is correct |
22 |
Correct |
5 ms |
440 KB |
Output is correct |
23 |
Correct |
4 ms |
440 KB |
Output is correct |
24 |
Correct |
4 ms |
440 KB |
Output is correct |
25 |
Correct |
4 ms |
440 KB |
Output is correct |
26 |
Correct |
5 ms |
440 KB |
Output is correct |
27 |
Correct |
6 ms |
600 KB |
Output is correct |
28 |
Correct |
6 ms |
444 KB |
Output is correct |
29 |
Correct |
5 ms |
436 KB |
Output is correct |
30 |
Correct |
5 ms |
344 KB |
Output is correct |
31 |
Correct |
11 ms |
448 KB |
Output is correct |
32 |
Correct |
21 ms |
420 KB |
Output is correct |
33 |
Correct |
31 ms |
444 KB |
Output is correct |
34 |
Correct |
26 ms |
444 KB |
Output is correct |
35 |
Correct |
27 ms |
448 KB |
Output is correct |
36 |
Correct |
20 ms |
452 KB |
Output is correct |
37 |
Correct |
25 ms |
636 KB |
Output is correct |
38 |
Correct |
21 ms |
456 KB |
Output is correct |
39 |
Correct |
26 ms |
448 KB |
Output is correct |
40 |
Correct |
43 ms |
428 KB |
Output is correct |
41 |
Correct |
30 ms |
436 KB |
Output is correct |
42 |
Correct |
26 ms |
432 KB |
Output is correct |
43 |
Correct |
24 ms |
456 KB |
Output is correct |
44 |
Correct |
26 ms |
448 KB |
Output is correct |
45 |
Correct |
33 ms |
428 KB |
Output is correct |
46 |
Correct |
40 ms |
448 KB |
Output is correct |
47 |
Correct |
31 ms |
444 KB |
Output is correct |
48 |
Correct |
29 ms |
432 KB |
Output is correct |
49 |
Correct |
25 ms |
448 KB |
Output is correct |
50 |
Correct |
37 ms |
452 KB |
Output is correct |
51 |
Correct |
35 ms |
444 KB |
Output is correct |
52 |
Correct |
27 ms |
428 KB |
Output is correct |
53 |
Correct |
32 ms |
452 KB |
Output is correct |
54 |
Correct |
30 ms |
448 KB |
Output is correct |
55 |
Correct |
32 ms |
452 KB |
Output is correct |
56 |
Correct |
28 ms |
428 KB |
Output is correct |
57 |
Correct |
35 ms |
596 KB |
Output is correct |
58 |
Correct |
28 ms |
444 KB |
Output is correct |
59 |
Correct |
30 ms |
444 KB |
Output is correct |
60 |
Correct |
39 ms |
436 KB |
Output is correct |