#include "monster.h"
#include <bits/stdc++.h>
using namespace std;
namespace {
bool example_variable;
} // namespace
vector<int> Solve(int n) {
map<pair<int, int>, bool> mp;
function<bool(int, int)> Ask = [&](int x, int y) {
if (x > y) {
return !Ask(y, x);
}
if (!mp.count({x, y})) {
mp[{x, y}] = Query(x, y);
}
return mp[{x, y}];
};
vector<int> order(n);
iota(order.begin(), order.end(), 0);
vector<int> tmp(n);
function<void(int, int)> MergeSort = [&](int l, int r) {
if (l == r) {
return;
}
int mid = l + r >> 1;
MergeSort(l, mid);
MergeSort(mid + 1, r);
for (int i = l; i <= r; i++) {
tmp[i] = order[i];
}
int pl = l, pr = mid + 1;
int ptr = l;
while (pl <= mid || pr <= r) {
if (pl > mid) {
order[ptr++] = tmp[pr++];
} else if (pr > r) {
order[ptr++] = tmp[pl++];
} else {
order[ptr++] = (Ask(tmp[pl], tmp[pr]) ? tmp[pl++] : tmp[pr++]);
}
}
};
MergeSort(0, n - 1);
int pos = -1;
auto Check = [&]() {
if (Ask(order[0], order[2])) {
return false;
}
if (!Ask(order[0], order[1])) {
return false;
}
if (!Ask(order[1], order[2])) {
return false;
}
return true;
};
int to = -1;
if (n <= 10) {
vector<int> bad;
for (int i = 1; i < n; i++) {
if (!Ask(order[0], order[i])) {
bad.push_back(i);
}
}
if ((int) bad.size() > 1) {
pos = bad.rbegin()[1];
} else {
bool ok = true;
for (int i = 2; i < n; i++) {
if (!Ask(order[1], order[i])) {
ok = false;
break;
}
}
if (ok) {
pos = 1;
} else {
pos = 0;
}
}
} else {
if (Check()) {
if (!Ask(order[0], order[3]) && !Ask(order[1], order[3])) {
vector<int> bad;
for (int i = 1; i < n; i++) {
if (!Ask(order[0], order[i])) {
bad.push_back(i);
}
}
pos = bad.rbegin()[1];
} else {
vector<int> cnt(3);
auto Update = [&](int x) {
for (int i = 0; i < 3; i++) {
if (cnt[i] > 0) {
return;
}
}
for (int i = 0; i < 3; i++) {
cnt[i] += !Ask(order[i], x);
}
};
if (!Ask(order[3], order[5]) && Ask(order[3], order[4]) && Ask(order[4], order[5])) {
if (!Ask(order[3], order[6]) && !Ask(order[4], order[6])) {
vector<int> bad(order[6]);
int cnt = 0;
for (int i = 7; i < n; i++) {
if (!Ask(order[3], order[i])) {
bad.push_back(order[i]);
cnt = 0;
} else {
cnt += 1;
if (cnt >= 1) {
break;
}
}
}
if (cnt >= 1) {
to = bad.back();
Update(bad.back());
} else {
if ((int) bad.size() > 1) {
bad.pop_back();
}
to = bad.back();
Update(bad.back());
}
} else {
for (int i = 3; i < 6; i++) {
Update(order[i]);
}
}
} else {
Update(order[4]);
Update(order[3]);
}
for (int i = 0; i < 3; i++) {
if (cnt[i] > 0) {
pos = (i - 1 + 3) % 3;
}
}
}
} else {
vector<int> cnt(2);
auto Update = [&](int x) {
for (int i = 0; i < 2; i++) {
if (cnt[i] > 0) {
return;
}
}
for (int i = 0; i < 2; i++) {
cnt[i] += !Ask(order[i], x);
}
};
if (!Ask(order[2], order[4]) && Ask(order[2], order[3]) && Ask(order[3], order[4])) {
if (!Ask(order[2], order[5]) && !Ask(order[3], order[5])) {
vector<int> bad(order[5]);
int cnt = 0;
for (int i = 6; i < n; i++) {
if (!Ask(order[2], order[i])) {
bad.push_back(order[i]);
cnt = 0;
} else {
cnt += 1;
if (cnt >= 1) {
break;
}
}
}
if (cnt >= 1) {
to = bad.back();
Update(bad.back());
} else {
if ((int) bad.size() > 1) {
bad.pop_back();
}
to = bad.back();
Update(bad.back());
}
} else {
for (int i = 2; i < 5; i++) {
Update(order[i]);
}
}
} else {
Update(order[3]);
Update(order[2]);
}
for (int i = 0; i < 2; i++) {
if (cnt[i] > 0) {
pos = (i - 1 + 2) % 2;
}
}
}
}
vector<int> res;
function<void(int, int)> Solve = [&](int l, int r) {
for (int i = r; i >= l; i--) {
res.push_back(order[i]);
}
if (r == n - 1) {
return;
}
if (r == pos && to != -1) {
Solve(r + 1, to);
return;
}
for (int i = r + 1; i < n; i++) {
if (Ask(order[l], order[i]) == (l != l)) {
Solve(r + 1, i);
break;
}
}
};
Solve(0, pos);
reverse(res.begin(), res.end());
vector<int> ret(n);
for (int i = 0; i < n; i++) {
ret[res[i]] = i;
}
return ret;
}
Compilation message
monster.cpp: In lambda function:
monster.cpp:30:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | int mid = l + r >> 1;
| ~~^~~
monster.cpp: In lambda function:
monster.cpp:214:41: warning: self-comparison always evaluates to false [-Wtautological-compare]
214 | if (Ask(order[l], order[i]) == (l != l)) {
| ~ ^~ ~
monster.cpp: At global scope:
monster.cpp:8:6: warning: '{anonymous}::example_variable' defined but not used [-Wunused-variable]
8 | bool example_variable;
| ^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
596 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
5 ms |
720 KB |
Output is correct |
17 |
Correct |
6 ms |
668 KB |
Output is correct |
18 |
Correct |
9 ms |
628 KB |
Output is correct |
19 |
Correct |
6 ms |
1112 KB |
Output is correct |
20 |
Correct |
7 ms |
1248 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
4 ms |
724 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
4 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
596 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
5 ms |
720 KB |
Output is correct |
17 |
Correct |
6 ms |
668 KB |
Output is correct |
18 |
Correct |
9 ms |
628 KB |
Output is correct |
19 |
Correct |
6 ms |
1112 KB |
Output is correct |
20 |
Correct |
7 ms |
1248 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
4 ms |
724 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
4 ms |
856 KB |
Output is correct |
33 |
Correct |
46 ms |
2024 KB |
Output is correct |
34 |
Correct |
42 ms |
1636 KB |
Output is correct |
35 |
Correct |
43 ms |
1448 KB |
Output is correct |
36 |
Correct |
46 ms |
1744 KB |
Output is correct |
37 |
Correct |
41 ms |
1408 KB |
Output is correct |
38 |
Correct |
41 ms |
1756 KB |
Output is correct |
39 |
Correct |
45 ms |
1936 KB |
Output is correct |
40 |
Correct |
43 ms |
1588 KB |
Output is correct |
41 |
Correct |
45 ms |
1420 KB |
Output is correct |
42 |
Correct |
46 ms |
1996 KB |
Output is correct |
43 |
Correct |
26 ms |
1576 KB |
Output is correct |
44 |
Correct |
25 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
1424 KB |
Output is correct |
2 |
Correct |
44 ms |
1832 KB |
Output is correct |
3 |
Correct |
51 ms |
2168 KB |
Output is correct |
4 |
Correct |
41 ms |
1060 KB |
Output is correct |
5 |
Correct |
46 ms |
1336 KB |
Output is correct |
6 |
Correct |
35 ms |
1828 KB |
Output is correct |
7 |
Correct |
35 ms |
1276 KB |
Output is correct |
8 |
Correct |
46 ms |
1668 KB |
Output is correct |
9 |
Correct |
55 ms |
1836 KB |
Output is correct |
10 |
Correct |
40 ms |
1960 KB |
Output is correct |
11 |
Correct |
47 ms |
1916 KB |
Output is correct |
12 |
Partially correct |
52 ms |
2000 KB |
Partially correct |
13 |
Correct |
36 ms |
2036 KB |
Output is correct |
14 |
Correct |
35 ms |
1692 KB |
Output is correct |
15 |
Correct |
22 ms |
1236 KB |
Output is correct |
16 |
Correct |
26 ms |
984 KB |
Output is correct |
17 |
Correct |
28 ms |
756 KB |
Output is correct |
18 |
Correct |
42 ms |
1616 KB |
Output is correct |