#include <bits/stdc++.h>
#include "xylophone.h"
using namespace std;
#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif
void solve(int N) {
vector<int> res, t(N), pos(N + 1), delta(N), dist(N + 1);
res = {1, 2};
pos[1] = 0;
pos[2] = 1;
t[0] = query(1, 2);
auto get = [&](int l, int r) {
if (l == r) {
return 0;
}
if (l > r) {
swap(l, r);
}
return t[r - 1] - (l == 0 ? 0 : t[l - 1]);
};
auto Right = [&](int u, int delta) {
int ans = -1;
for (int i = u; i < int(res.size()); ++i) {
if (get(u, i) > delta) {
break;
}
ans = i + 1;
}
return ans;
};
auto Left = [&](int u, int delta) {
int ans = -1;
for (int i = u; i >= 0; --i) {
if (get(u, i) > delta) {
break;
}
ans = i;
}
return ans;
};
for (int i = 3; i <= N; ++i) {
int a = pos[i - 2], b = pos[i - 1];
for (int j = 1; j < i; ++j) {
dist[j] = get(b, pos[j]);
}
int j = -1;
int AB = dist[i - 2], BC = query(i - 1, i), AC = query(i - 2, i);
dist[i] = BC;
if (a < b) {
if (AB + BC == AC) {
j = Right(b, BC);
} else {
j = Left(b, BC);
}
} else {
if (AB + BC == AC) {
j = Left(b, BC);
} else {
j = Right(b, BC);
}
}
assert(j != -1);
res.insert(res.begin() + j, i);
auto reset = [&]() {
for (int i = 0; i < int(res.size()); ++i) {
pos[res[i]] = i;
}
for (int i = 0; i + 1 < int(res.size()); ++i) {
t[i] = abs(dist[res[i]] - dist[res[i + 1]]);
if (i > 0) {
t[i] += t[i - 1];
}
}
};
reset();
}
assert(int(res.size()) == N);
if (res[0] > res.back()) {
reverse(res.begin(), res.end());
}
for (int i = 0; i < N; ++i) {
answer(res[i], i + 1);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
1 ms |
436 KB |
Output is correct |
7 |
Correct |
1 ms |
436 KB |
Output is correct |
8 |
Correct |
1 ms |
432 KB |
Output is correct |
9 |
Correct |
1 ms |
436 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
432 KB |
Output is correct |
12 |
Correct |
1 ms |
436 KB |
Output is correct |
13 |
Correct |
1 ms |
436 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
15 |
Correct |
1 ms |
432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
1 ms |
436 KB |
Output is correct |
7 |
Correct |
1 ms |
436 KB |
Output is correct |
8 |
Correct |
1 ms |
432 KB |
Output is correct |
9 |
Correct |
1 ms |
436 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
432 KB |
Output is correct |
12 |
Correct |
1 ms |
436 KB |
Output is correct |
13 |
Correct |
1 ms |
436 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
15 |
Correct |
1 ms |
432 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
4 ms |
440 KB |
Output is correct |
18 |
Correct |
8 ms |
712 KB |
Output is correct |
19 |
Correct |
16 ms |
464 KB |
Output is correct |
20 |
Correct |
11 ms |
456 KB |
Output is correct |
21 |
Correct |
13 ms |
464 KB |
Output is correct |
22 |
Correct |
11 ms |
708 KB |
Output is correct |
23 |
Correct |
11 ms |
956 KB |
Output is correct |
24 |
Correct |
10 ms |
704 KB |
Output is correct |
25 |
Correct |
12 ms |
700 KB |
Output is correct |
26 |
Correct |
10 ms |
960 KB |
Output is correct |
27 |
Correct |
10 ms |
704 KB |
Output is correct |
28 |
Correct |
10 ms |
964 KB |
Output is correct |
29 |
Correct |
12 ms |
712 KB |
Output is correct |
30 |
Correct |
10 ms |
964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
436 KB |
Output is correct |
6 |
Correct |
1 ms |
436 KB |
Output is correct |
7 |
Correct |
1 ms |
436 KB |
Output is correct |
8 |
Correct |
1 ms |
432 KB |
Output is correct |
9 |
Correct |
1 ms |
436 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
432 KB |
Output is correct |
12 |
Correct |
1 ms |
436 KB |
Output is correct |
13 |
Correct |
1 ms |
436 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
15 |
Correct |
1 ms |
432 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
4 ms |
440 KB |
Output is correct |
18 |
Correct |
8 ms |
712 KB |
Output is correct |
19 |
Correct |
16 ms |
464 KB |
Output is correct |
20 |
Correct |
11 ms |
456 KB |
Output is correct |
21 |
Correct |
13 ms |
464 KB |
Output is correct |
22 |
Correct |
11 ms |
708 KB |
Output is correct |
23 |
Correct |
11 ms |
956 KB |
Output is correct |
24 |
Correct |
10 ms |
704 KB |
Output is correct |
25 |
Correct |
12 ms |
700 KB |
Output is correct |
26 |
Correct |
10 ms |
960 KB |
Output is correct |
27 |
Correct |
10 ms |
704 KB |
Output is correct |
28 |
Correct |
10 ms |
964 KB |
Output is correct |
29 |
Correct |
12 ms |
712 KB |
Output is correct |
30 |
Correct |
10 ms |
964 KB |
Output is correct |
31 |
Correct |
29 ms |
972 KB |
Output is correct |
32 |
Correct |
57 ms |
992 KB |
Output is correct |
33 |
Correct |
103 ms |
768 KB |
Output is correct |
34 |
Correct |
118 ms |
776 KB |
Output is correct |
35 |
Correct |
128 ms |
540 KB |
Output is correct |
36 |
Correct |
104 ms |
796 KB |
Output is correct |
37 |
Correct |
120 ms |
1028 KB |
Output is correct |
38 |
Correct |
104 ms |
1296 KB |
Output is correct |
39 |
Correct |
102 ms |
788 KB |
Output is correct |
40 |
Correct |
139 ms |
768 KB |
Output is correct |
41 |
Correct |
136 ms |
1520 KB |
Output is correct |
42 |
Correct |
100 ms |
1032 KB |
Output is correct |
43 |
Correct |
107 ms |
1028 KB |
Output is correct |
44 |
Correct |
109 ms |
780 KB |
Output is correct |
45 |
Correct |
102 ms |
1052 KB |
Output is correct |
46 |
Correct |
117 ms |
772 KB |
Output is correct |
47 |
Correct |
115 ms |
776 KB |
Output is correct |
48 |
Correct |
113 ms |
1024 KB |
Output is correct |
49 |
Correct |
105 ms |
1036 KB |
Output is correct |
50 |
Correct |
103 ms |
1008 KB |
Output is correct |
51 |
Correct |
119 ms |
1040 KB |
Output is correct |
52 |
Correct |
121 ms |
1032 KB |
Output is correct |
53 |
Correct |
141 ms |
1008 KB |
Output is correct |
54 |
Correct |
121 ms |
1032 KB |
Output is correct |
55 |
Correct |
124 ms |
1028 KB |
Output is correct |
56 |
Correct |
132 ms |
792 KB |
Output is correct |
57 |
Correct |
135 ms |
1024 KB |
Output is correct |
58 |
Correct |
119 ms |
772 KB |
Output is correct |
59 |
Correct |
126 ms |
796 KB |
Output is correct |
60 |
Correct |
119 ms |
792 KB |
Output is correct |