# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
795625 | 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);
}
}
deque<int> m;
void clear_queue()
{
while(!m.empty())
{
Query(m.back());
m.pop_back();
}
}
void Solve(int N) {
vector<vector<int>> prev1(1);
vector<vector<int>> prev2(1);
for(int i = 1; i <= 2*N; i++)
{
m.push_back(i);
if(Query(i) != m.size())
{
prev2[0].push_back(i);
Query(i);
m.pop_back();
}
else
{
prev1[0].push_back(i);
}
}
clear_queue();
while(prev1.size() < N)
{
vector<vector<int>> now1;
vector<vector<int>> now2;
for(int i = 0; i < prev1.size(); i++)
{
//ONE
if(prev1[i].size() == 1)
{
now1.push_back(prev1[i]);
now2.push_back(prev2[i]);
continue;
}
now1.push_back(vector<int>());
now1.push_back(vector<int>());
now2.push_back(vector<int>());
now2.push_back(vector<int>());
for(int j = 0; j < prev1[i].size(); j++)
{
if(j%2 == 0)
{
now1[now1.size()-2].push_back(prev1[i][j]);
m.push_back(prev1[i][j]);
Query(prev1[i][j]);
}
else
{
now1[now1.size()-1].push_back(prev1[i][j]);
}
}
for(int j = 0; j < prev2[i].size(); j++)
{
m.push_back(prev2[i][j]);
if(Query(prev2[i][j]) == m.size())
{
now2[now2.size()-1].push_back(prev2[i][j]);
Query(prev2[i][j]);
m.pop_back();
}
else
{
now2[now2.size()-2].push_back(prev2[i][j]);
Query(prev2[i][j]);
m.pop_back();
}
}
clear_queue();
}
prev1 = now1;
prev2 = now2;
}
for(int i = 0; i < N; i++)
{
Answer(prev1[i][0], prev2[i][0]);
}
}
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
*/