#include "monster.h"
#include <bits/stdc++.h>
namespace monster{
int N;
std::map<std::pair<int, int>, int> memo;
bool ask(int a, int b) {
bool isSw = false;
if (a > b) {
isSw = true;
std::swap(a, b);
}
if (memo.find({a, b}) == memo.end()) {
memo[{a, b}] = Query(a, b);
}
return memo[{a, b}] ^ isSw;
}
std::vector<int> Solve(int n_) {
N = n_;
std::vector<int> res(N);
std::iota(res.begin(), res.end(), 0);
auto mergeSort = [&](auto &&self, int l, int r) -> void {
if (r - l == 1) {
return;
}
const int m = (l + r) / 2;
self(self, l, m);
self(self, m, r);
std::vector<int> lVec, rVec;
std::copy(res.begin() + l, res.begin() + m, std::back_inserter(lVec));
std::copy(res.begin() + m, res.begin() + r, std::back_inserter(rVec));
std::vector<int> mergedVec;
int i = 0, j = 0;
while (i != m - l and j != r - m) {
if (not ask(lVec[i], rVec[j])) {
mergedVec.push_back(lVec[i++]);
} else {
mergedVec.push_back(rVec[j++]);
}
}
if (i != m - l) {
std::copy(lVec.begin() + i, lVec.end(), std::back_inserter(mergedVec));
}
if (j != r - m) {
std::copy(rVec.begin() + j, rVec.end(), std::back_inserter(mergedVec));
}
std::copy(mergedVec.begin(), mergedVec.end(), res.begin() + l);
};
mergeSort(mergeSort, 0, N);
std::vector<bool> isF(N);
isF[0] = true;
int l = -1, r = -1;
// find the first block!
std::vector<int> countWin;
for (int i = 0; i < std::min(N, 20); ++i) {
int c = 0;
for (int j = 0; j < std::min(N, 20); ++j) {
if (i == j) continue;
if (ask(res[i], res[j])) {
++c;
}
}
if (i == 0 or countWin.back() >= c) {
countWin.push_back(c);
} else {
break;
}
}
if (countWin.size() == 1) {
l = 0;
r = 1;
} else if (countWin.size() == 2) {
assert(countWin[0] == countWin[1] and countWin[0] == 1);
if (not ask(res[0], res[1])) {
l = 0;
r = 2;
} else {
isF[1] = true;
l = 1;
r = 2;
}
} else {
l = 0;
r = (int)countWin.size();
}
while (r != N) {
isF[r] = true;
bool fin = false;
for (int i = r; i < N; ++i) {
if (ask(res[l], res[i])) {
fin = true;
l = r;
r = i + 1;
break;
}
}
if (not fin) {
break;
}
}
std::vector<int> answer;
std::stack<int> stk;
for (int i = 0; i < N; ++i) {
if (isF[i]) {
while (not stk.empty()) {
answer.push_back(stk.top());
stk.pop();
}
}
stk.push(res[i]);
}
while (not stk.empty()) {
answer.push_back(stk.top());
stk.pop();
}
memo.clear();
std::vector<int> tet(N);
for (int i = 0; i < N; ++i) {
tet[answer[i]] = i;
}
return tet;
}
}
std::vector<int> Solve(int n_) {
return monster::Solve(n_);
}
/*
#include <cstdio>
#include <cstdlib>
using std::exit;
using std::fprintf;
using std::printf;
using std::scanf;
constexpr int Q_MAX = 25000;
constexpr int N_MAX = 1'000;
int N;
int S[N_MAX];
int query_count = 0;
void WrongAnswer(int code) {
printf("Wrong Answer [%d]\n", code);
exit(0);
}
bool Query(int a, int b) {
if (++query_count > Q_MAX) WrongAnswer(6);
if (a < 0 || N <= a || b < 0 || N <= b) WrongAnswer(4);
if (a == b) WrongAnswer(5);
return (S[a] > S[b]) ^ (abs(S[a] - S[b]) == 1);
}
void main_() {
if (scanf("%d", &N) != 1) {
fprintf(stderr, "Error while reading.\n");
exit(1);
}
for (int i = 0; i < N; i++) {
if (scanf("%d", S + i) != 1) {
fprintf(stderr, "Error while reading.\n");
exit(1);
}
}
N = 1000;
int cas = 1000;
std::mt19937 mt(100);
std::uniform_int_distribution dist(1, N);
std::vector<int> A(N);
std::iota(A.begin(), A.end(), 0);
int mav = 0;
while (cas--) {
std::shuffle(A.begin(), A.end(), mt);
for (int i = 0; i < N; ++i) S[i] = A[i];
const auto T = Solve(N);
if (T != A) {
std::cout << "bad" << std::endl;
}
mav = std::max(mav, query_count);
query_count = 0;
}
std::cout << mav << std::endl;
std::vector<int> T = Solve(N);
if ((int)T.size() != N) WrongAnswer(1);
for (int i = 0; i < N; ++i) {
std::cout << S[T[i]] << ' ';
}
std::cout << std::endl;
for (int i = 0; i < N; i++) {
if (T[i] < 0 || N <= T[i]) WrongAnswer(2);
if (T[i] != S[i]) WrongAnswer(3);
}
printf("Accepted: %d\n", query_count);
return;
}
int main() {
main_();
}*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
0 ms |
208 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
208 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
9 ms |
340 KB |
Output is correct |
17 |
Correct |
11 ms |
304 KB |
Output is correct |
18 |
Correct |
14 ms |
340 KB |
Output is correct |
19 |
Correct |
11 ms |
360 KB |
Output is correct |
20 |
Correct |
15 ms |
368 KB |
Output is correct |
21 |
Correct |
0 ms |
208 KB |
Output is correct |
22 |
Correct |
1 ms |
208 KB |
Output is correct |
23 |
Correct |
0 ms |
208 KB |
Output is correct |
24 |
Correct |
0 ms |
208 KB |
Output is correct |
25 |
Correct |
1 ms |
208 KB |
Output is correct |
26 |
Correct |
8 ms |
336 KB |
Output is correct |
27 |
Correct |
0 ms |
208 KB |
Output is correct |
28 |
Correct |
1 ms |
208 KB |
Output is correct |
29 |
Correct |
1 ms |
208 KB |
Output is correct |
30 |
Correct |
0 ms |
208 KB |
Output is correct |
31 |
Correct |
1 ms |
208 KB |
Output is correct |
32 |
Correct |
7 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
0 ms |
208 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
208 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
9 ms |
340 KB |
Output is correct |
17 |
Correct |
11 ms |
304 KB |
Output is correct |
18 |
Correct |
14 ms |
340 KB |
Output is correct |
19 |
Correct |
11 ms |
360 KB |
Output is correct |
20 |
Correct |
15 ms |
368 KB |
Output is correct |
21 |
Correct |
0 ms |
208 KB |
Output is correct |
22 |
Correct |
1 ms |
208 KB |
Output is correct |
23 |
Correct |
0 ms |
208 KB |
Output is correct |
24 |
Correct |
0 ms |
208 KB |
Output is correct |
25 |
Correct |
1 ms |
208 KB |
Output is correct |
26 |
Correct |
8 ms |
336 KB |
Output is correct |
27 |
Correct |
0 ms |
208 KB |
Output is correct |
28 |
Correct |
1 ms |
208 KB |
Output is correct |
29 |
Correct |
1 ms |
208 KB |
Output is correct |
30 |
Correct |
0 ms |
208 KB |
Output is correct |
31 |
Correct |
1 ms |
208 KB |
Output is correct |
32 |
Correct |
7 ms |
336 KB |
Output is correct |
33 |
Correct |
92 ms |
812 KB |
Output is correct |
34 |
Correct |
78 ms |
832 KB |
Output is correct |
35 |
Correct |
65 ms |
812 KB |
Output is correct |
36 |
Correct |
88 ms |
812 KB |
Output is correct |
37 |
Correct |
81 ms |
812 KB |
Output is correct |
38 |
Correct |
74 ms |
784 KB |
Output is correct |
39 |
Correct |
82 ms |
908 KB |
Output is correct |
40 |
Correct |
90 ms |
812 KB |
Output is correct |
41 |
Correct |
90 ms |
956 KB |
Output is correct |
42 |
Correct |
90 ms |
904 KB |
Output is correct |
43 |
Correct |
42 ms |
584 KB |
Output is correct |
44 |
Correct |
47 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
896 KB |
Output is correct |
2 |
Correct |
82 ms |
864 KB |
Output is correct |
3 |
Correct |
90 ms |
832 KB |
Output is correct |
4 |
Correct |
90 ms |
824 KB |
Output is correct |
5 |
Correct |
75 ms |
980 KB |
Output is correct |
6 |
Correct |
79 ms |
712 KB |
Output is correct |
7 |
Correct |
50 ms |
748 KB |
Output is correct |
8 |
Correct |
104 ms |
972 KB |
Output is correct |
9 |
Correct |
90 ms |
856 KB |
Output is correct |
10 |
Correct |
88 ms |
836 KB |
Output is correct |
11 |
Correct |
79 ms |
840 KB |
Output is correct |
12 |
Correct |
93 ms |
864 KB |
Output is correct |
13 |
Correct |
73 ms |
744 KB |
Output is correct |
14 |
Correct |
62 ms |
676 KB |
Output is correct |
15 |
Correct |
38 ms |
552 KB |
Output is correct |
16 |
Correct |
54 ms |
640 KB |
Output is correct |
17 |
Correct |
67 ms |
800 KB |
Output is correct |
18 |
Correct |
62 ms |
592 KB |
Output is correct |