# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
795671 | AbdullahMohammedAhmad | Minerals (JOI19_minerals) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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
*/