#include "monster.h"
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, bool> mp;
bool query(int i, int j) {
if (mp.count({i, j})) {
return mp[{i, j}];
}
mp[{i, j}] = Query(i, j);
mp[{j, i}] = !mp[{i, j}];
return mp[{i, j}];
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
596 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
1 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 |
936 KB |
Output is correct |
17 |
Correct |
8 ms |
448 KB |
Output is correct |
18 |
Correct |
8 ms |
912 KB |
Output is correct |
19 |
Correct |
6 ms |
600 KB |
Output is correct |
20 |
Correct |
6 ms |
1016 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 |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
8 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 |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
536 KB |
Output is correct |
32 |
Correct |
5 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
596 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
1 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 |
936 KB |
Output is correct |
17 |
Correct |
8 ms |
448 KB |
Output is correct |
18 |
Correct |
8 ms |
912 KB |
Output is correct |
19 |
Correct |
6 ms |
600 KB |
Output is correct |
20 |
Correct |
6 ms |
1016 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 |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
8 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 |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
536 KB |
Output is correct |
32 |
Correct |
5 ms |
600 KB |
Output is correct |
33 |
Correct |
47 ms |
2432 KB |
Output is correct |
34 |
Correct |
46 ms |
2692 KB |
Output is correct |
35 |
Correct |
55 ms |
2188 KB |
Output is correct |
36 |
Correct |
48 ms |
2536 KB |
Output is correct |
37 |
Correct |
53 ms |
2676 KB |
Output is correct |
38 |
Correct |
53 ms |
1972 KB |
Output is correct |
39 |
Correct |
55 ms |
2616 KB |
Output is correct |
40 |
Correct |
49 ms |
2532 KB |
Output is correct |
41 |
Correct |
49 ms |
2532 KB |
Output is correct |
42 |
Correct |
46 ms |
2172 KB |
Output is correct |
43 |
Correct |
46 ms |
2400 KB |
Output is correct |
44 |
Correct |
40 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
2432 KB |
Output is correct |
2 |
Correct |
52 ms |
1840 KB |
Output is correct |
3 |
Correct |
51 ms |
1940 KB |
Output is correct |
4 |
Correct |
47 ms |
2320 KB |
Output is correct |
5 |
Correct |
46 ms |
1936 KB |
Output is correct |
6 |
Correct |
44 ms |
1952 KB |
Output is correct |
7 |
Correct |
54 ms |
2216 KB |
Output is correct |
8 |
Correct |
62 ms |
1772 KB |
Output is correct |
9 |
Correct |
51 ms |
1744 KB |
Output is correct |
10 |
Correct |
55 ms |
2704 KB |
Output is correct |
11 |
Correct |
51 ms |
2576 KB |
Output is correct |
12 |
Correct |
63 ms |
2052 KB |
Output is correct |
13 |
Correct |
49 ms |
2592 KB |
Output is correct |
14 |
Correct |
71 ms |
2708 KB |
Output is correct |
15 |
Correct |
52 ms |
2280 KB |
Output is correct |
16 |
Correct |
40 ms |
1672 KB |
Output is correct |
17 |
Correct |
58 ms |
2212 KB |
Output is correct |
18 |
Correct |
47 ms |
1852 KB |
Output is correct |