#include "monster.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> my_sort(vector<int> nw) {
map<pair<int, int>, int> vl;
map<int, int> cnt;
for (int i = 0; i < (int)nw.size(); ++i) {
for (int j = i + 1; j < (int)nw.size(); ++j) {
if (Query(nw[i], nw[j])) {
vl[{nw[i], nw[j]}] = 1;
cnt[nw[i]]++;
} else {
vl[{nw[j], nw[i]}] = 1;
cnt[nw[j]]++;
}
}
}
int start;
for (int i = 0; i < (int)nw.size(); ++i) {
if (cnt[nw[i]] == 1) {
bool check = true;
for (int j = 0; j < (int)nw.size(); ++j) {
if (vl[{nw[i], nw[j]}] && cnt[nw[j]] != 1) {
check = false;
break;
}
}
if (check) {
start = nw[i];
break;
}
}
}
vector<int> ans = {start};
set<int> used = {start};
while (ans.size() < nw.size()) {
for (int i = 0; i < (int)nw.size(); ++i) {
if (!used.count(nw[i]) && vl[{ans.back(), nw[i]}]) {
ans.push_back(nw[i]);
used.insert(nw[i]);
break;
}
}
}
return ans;
}
vector<int> Solve(int n) {
vector<int> ord = {0};
for (int i = 1; i < n; ++i) {
int l = -1, r = ord.size();
while (l < r - 1) {
int m = (l + r) / 2;
if (Query(i, ord[m])) {
l = m;
} else {
r = m;
}
}
vector<int> ord_nw;
for (int j = 0; j <= l; ++j) {
ord_nw.push_back(ord[j]);
}
ord_nw.push_back(i);
for (int j = r; j < (int)ord.size(); ++j) {
ord_nw.push_back(ord[j]);
}
ord = ord_nw;
}
// for (int i = 0; i < n; ++i) {
// cout << ord[i] << ' ';
// }
// cout << endl;
int pos = 0;
vector<int> poses;
while (pos < n - 2) {
int r = pos + 2;
if (Query(ord[pos], ord[r])) {
while (r + 1 < n && Query(ord[pos], ord[r + 1])) {
r++;
}
if (r == pos + 2) {
poses.push_back(pos);
} else {
if (Query(ord[pos + 1], ord[r])) {
reverse(ord.begin() + pos, ord.begin() + r + 1);
} else {
reverse(ord.begin() + pos, ord.begin() + r);
}
}
} else {
while (r + 1 < n && Query(ord[r + 1], ord[pos])) {
r++;
}
if (r > pos + 2) {
if (Query(ord[r], ord[pos + 1])) {
reverse(ord.begin() + pos, ord.begin() + pos + 2);
reverse(ord.begin() + pos + 2, ord.begin() + r + 2);
r++;
} else {
reverse(ord.begin() + pos + 1, ord.begin() + r + 2);
r++;
}
} else {
vector<int> vals = {ord[pos], ord[pos + 1], ord[pos + 2], ord[pos + 3]};
vals = my_sort(vals);
ord[pos] = vals[0];
ord[pos + 1] = vals[1];
ord[pos + 2] = vals[2];
ord[pos + 3] = vals[3];
r++;
}
}
pos = r + 1;
}
if (pos == n - 2) {
swap(ord[n - 2], ord[n - 1]);
}
for (int i = 0; i < (int)poses.size(); ++i) {
if (poses[i] == -1) {
continue;
}
if (poses[i] != 0) {
vector<int> vals = {ord[poses[i] - 1], ord[poses[i]], ord[poses[i] + 1], ord[poses[i] + 2]};
vals = my_sort(vals);
ord[poses[i]] = vals[1];
ord[poses[i] + 1] = vals[2];
ord[poses[i] + 2] = vals[3];
} else if (i + 1 == (int)poses.size() || poses[i] + 3 != poses[i + 1]) {
vector<int> vals = {ord[poses[i]], ord[poses[i] + 1], ord[poses[i] + 2], ord[poses[i] + 3]};
vals = my_sort(vals);
ord[poses[i]] = vals[0];
ord[poses[i] + 1] = vals[1];
ord[poses[i] + 2] = vals[2];
} else {
vector<int> vals = {ord[poses[i]], ord[poses[i] + 1], ord[poses[i] + 2], ord[poses[i] + 3], ord[poses[i] + 4], ord[poses[i] + 5]};
vals = my_sort(vals);
ord[poses[i]] = vals[0];
ord[poses[i] + 1] = vals[1];
ord[poses[i] + 2] = vals[2];
ord[poses[i] + 3] = vals[3];
ord[poses[i] + 4] = vals[4];
ord[poses[i] + 5] = vals[5];
poses[i + 1] = -1;
}
}
vector<int> ans(n);
for (int i = 0; i < n; ++i) {
ans[ord[i]] = i;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
1112 KB |
Output is correct |
17 |
Correct |
8 ms |
856 KB |
Output is correct |
18 |
Correct |
7 ms |
1112 KB |
Output is correct |
19 |
Correct |
7 ms |
700 KB |
Output is correct |
20 |
Correct |
6 ms |
860 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 |
6 ms |
856 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 |
7 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
1112 KB |
Output is correct |
17 |
Correct |
8 ms |
856 KB |
Output is correct |
18 |
Correct |
7 ms |
1112 KB |
Output is correct |
19 |
Correct |
7 ms |
700 KB |
Output is correct |
20 |
Correct |
6 ms |
860 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 |
6 ms |
856 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 |
7 ms |
856 KB |
Output is correct |
33 |
Correct |
55 ms |
1216 KB |
Output is correct |
34 |
Correct |
39 ms |
1128 KB |
Output is correct |
35 |
Correct |
39 ms |
1140 KB |
Output is correct |
36 |
Correct |
41 ms |
664 KB |
Output is correct |
37 |
Correct |
46 ms |
1224 KB |
Output is correct |
38 |
Correct |
51 ms |
856 KB |
Output is correct |
39 |
Correct |
47 ms |
1232 KB |
Output is correct |
40 |
Correct |
55 ms |
848 KB |
Output is correct |
41 |
Correct |
49 ms |
1156 KB |
Output is correct |
42 |
Correct |
48 ms |
956 KB |
Output is correct |
43 |
Correct |
59 ms |
856 KB |
Output is correct |
44 |
Correct |
52 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
44 ms |
1204 KB |
Partially correct |
2 |
Partially correct |
45 ms |
1016 KB |
Partially correct |
3 |
Partially correct |
51 ms |
848 KB |
Partially correct |
4 |
Partially correct |
43 ms |
860 KB |
Partially correct |
5 |
Partially correct |
65 ms |
924 KB |
Partially correct |
6 |
Partially correct |
47 ms |
856 KB |
Partially correct |
7 |
Partially correct |
60 ms |
952 KB |
Partially correct |
8 |
Partially correct |
60 ms |
856 KB |
Partially correct |
9 |
Partially correct |
57 ms |
952 KB |
Partially correct |
10 |
Partially correct |
67 ms |
1200 KB |
Partially correct |
11 |
Partially correct |
50 ms |
1112 KB |
Partially correct |
12 |
Partially correct |
43 ms |
1204 KB |
Partially correct |
13 |
Partially correct |
52 ms |
748 KB |
Partially correct |
14 |
Partially correct |
59 ms |
1208 KB |
Partially correct |
15 |
Partially correct |
51 ms |
1240 KB |
Partially correct |
16 |
Partially correct |
41 ms |
920 KB |
Partially correct |
17 |
Partially correct |
52 ms |
848 KB |
Partially correct |
18 |
Partially correct |
61 ms |
692 KB |
Partially correct |