#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
constexpr int MAX_N = 43000;
constexpr int MAX_CALLS = 1000000;
namespace {
void WrongAnswer(int code) {
printf("Wrong Answer [%d]\n", code);
exit(0);
}
int N;
int counterpart[2 * MAX_N + 1];
int num_queries;
int num_kinds;
int on[2 * MAX_N + 1];
int cntt[2 * MAX_N + 1];
int num_answers;
int answer[2 * MAX_N + 1];
} // namespace
int Query(int x) {
if (!(1 <= x && x <= 2 * N)) {
WrongAnswer(1);
}
if (++num_queries > MAX_CALLS) {
WrongAnswer(2);
}
const int type = std::min(x, counterpart[x]);
if (on[x]) {
--on[x];
--cntt[type];
if (cntt[type] == 0) {
--num_kinds;
}
} else {
++on[x];
++cntt[type];
if (cntt[type] == 1) {
++num_kinds;
}
}
return num_kinds;
}
void Answer(int a, int b) {
if (++num_answers > N) {
WrongAnswer(6);
}
if (!(1 <= a && a <= 2 * N && 1 <= b && b <= 2 * N)) {
WrongAnswer(3);
}
if (answer[a] != 0) {
WrongAnswer(4);
}
answer[a] = b;
if (answer[b] != 0) {
WrongAnswer(4);
}
answer[b] = a;
if (!(counterpart[a] == b && counterpart[b] == a)) {
WrongAnswer(5);
}
}
mt19937 rng(66);
void Solve(int n) {
vector<int> p1, p2;
int ans = 0, prevans = -1;
vector<int> bulanea(2 * n);
iota(bulanea.begin(), bulanea.end(), 1);
shuffle(bulanea.begin(), bulanea.end(), rng);
for(int i: bulanea) {
ans = Query(i);
if(ans == prevans) {
p2.push_back(i);
} else {
p1.push_back(i);
}
prevans = ans;
}
assert(p1.size() == n && p2.size() == n);
vector<int> pr(n, 0);
vector<bool> bagat(n, 1);
vector<int> bit_order;
vector<int> best_order; int mn = INT_MAX;
for(int bit = 0; (1 << bit) < n; ++bit) {
bit_order.push_back(bit);
}
int f = 0;
while(f++ < 1000) {
vector<bool> bruh(n, true);
int cost = 0;
for(int b: bit_order) {
for(int i = 0; i < n; ++i) {
if(i >> b & 1) {
if(!bruh[i]) ++cost;
bruh[i] = 1;
} else {
if(bruh[i]) ++cost;
bruh[i] = 0;
}
}
}
if(cost < mn) {
mn = cost;
best_order = bit_order;
}
shuffle(bit_order.begin(), bit_order.end(), rng);
}
for(int bit: best_order) {
int cntt = 0;
for(int i = 0; i < n; ++i) {
if(i >> bit & 1) {
++cntt;
if(!bagat[i]) prevans = Query(p1[i]);
bagat[i] = 1;
} else {
if(bagat[i]) prevans = Query(p1[i]);
bagat[i] = 0;
}
}
for(int i = 0; i < n && cntt; ++i) {
if(pr[i] + (1 << bit) >= n) continue;
int ans = Query(p2[i]);
if(ans == prevans) {
pr[i] += (1 << bit);
--cntt;
}
prevans = ans;
}
}
for(int i = 0; i < n; ++i) {
Answer(p2[i], p1[pr[i]]);
}
}
int main() {
if (scanf("%d", &N) != 1) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
for (int i = 1; i <= N; ++i) {
int x, y;
if (scanf("%d%d", &x, &y) != 2) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
counterpart[x] = y;
counterpart[y] = x;
}
Solve(N);
if (num_answers != N) {
WrongAnswer(6);
}
printf("Accepted: %d\n", num_queries);
return 0;
}
/*
4
1 5
2 6
3 4
7 8
*/
Compilation message
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from minerals.cpp:4:
minerals.cpp: In function 'void Solve(int)':
minerals.cpp:89:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
89 | assert(p1.size() == n && p2.size() == n);
| ~~~~~~~~~~^~~~
minerals.cpp:89:40: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
89 | assert(p1.size() == n && p2.size() == n);
| ~~~~~~~~~~^~~~
/usr/bin/ld: /tmp/ccL8nJ3Y.o: in function `Query(int)':
grader.cpp:(.text+0x30): multiple definition of `Query(int)'; /tmp/cc2b31sY.o:minerals.cpp:(.text+0x30): first defined here
/usr/bin/ld: /tmp/ccL8nJ3Y.o: in function `Answer(int, int)':
grader.cpp:(.text+0x100): multiple definition of `Answer(int, int)'; /tmp/cc2b31sY.o:minerals.cpp:(.text+0x100): first defined here
/usr/bin/ld: /tmp/ccL8nJ3Y.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cc2b31sY.o:minerals.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status