# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
405535 | Victor | Chameleon's Love (JOI20_chameleon) | C++17 | 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 "chameleon.h"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = b - 1; i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
const int INF = 1'000'000'007;
bitset<1001> taken;
void bsearch(vi chams, int cham) {
if (sz(chams) == 1) {
taken[chams[0] - 1] = 1;
Answer(cham, chams[0]);
return;
}
int si = sz(chams);
vi next(si >> 1);
rep(i, 0, si >> 1) next[i] = chams[i];
int c1 = Query(next);
next.pb(cham);
int c2 = Query(next);
if (c1 == c2)
next.pop_back();
else {
next.clear();
rep(i, si >> 1, si) next.pb(chams[i]);
}
bsearch(next, cham);
}
void Solve(int n) {
int like[n * 2 + 1];
if (n <= 50) {
vvi allcands(n * 2 + 1);
rep(i, 1, n*2+1) {
vi qvec, &cands = allcands[i];
qvec.pb(i);
rep(j, 1, n * 2 + 1) {
if (i == j) continue;
qvec.pb(j);
int colors = Query(qvec);
qvec.pop_back();
if (colors == 1) cands.pb(j);
}
if (sz(cands) != 1) {
rep(mask, 3, 7) {
if (mask == 4) continue;
rep(j, 0, 3) if (mask & (1 << j)) qvec.pb(cands[j]);
if (Query(qvec) == 1) {
rep(j, 0, 3) if (!(mask & (1 << j))) {
like[i] = cands[j];
cands.erase(cands.begin() + j);
}
break;
}
qvec.pop_back();
qvec.pop_back();
}
}
}
rep(i, 1, n * 2 + 1) {
if (taken[i]) continue;
vi &cands = allcands[i];
int same = cands[0];
if (sz(cands) == 2 && like[same] == i)
same = cands[1];
Answer(i, same);
taken[same] = 1;
}
}
else
rep(i, 0, n * 2) {
if (taken[i]) continue;
taken[i] = 1;
vi vec;
rep(j, 0, n * 2) if (!taken[j]) vec.pb(j + 1);
bsearch(vec, i + 1);
}
}
namespace {
using std::exit;
using std::fprintf;
using std::printf;
using std::scanf;
constexpr int Q_MAX = 20'000;
constexpr int N_MAX = 500;
int N;
int Y[N_MAX * 2 + 1], C[N_MAX * 2 + 1], L[N_MAX * 2 + 1];
int query_count = 0;
int answer_count = 0;
bool finishes[N_MAX * 2 + 1];
void WrongAnswer(int code) {
printf("Wrong Answer [%d]\n", code);
exit(0);
}
} // namespace
int Query(const std::vector<int> &p) {
if (++query_count > Q_MAX) WrongAnswer(3);
bool presents[N_MAX * 2 + 1];
for (int i = 1; i <= N * 2; ++i) presents[i] = false;
for (const int k : p) {
if (k <= 0 || k > N * 2) WrongAnswer(1);
if (presents[k]) WrongAnswer(2);
presents[k] = true;
}
bool colors[N_MAX + 1];
for (int j = 1; j <= N; ++j) colors[j] = false;
int color_count = 0;
for (int i = 1; i <= N * 2; ++i) {
if (!presents[i]) continue;
const int color = presents[L[i]] ? C[L[i]] : C[i];
if (!colors[color]) {
++color_count;
colors[color] = true;
}
}
return color_count;
}
void Answer(int a, int b) {
++answer_count;
if (a <= 0 || a > N * 2) WrongAnswer(4);
if (b <= 0 || b > N * 2) WrongAnswer(4);
if (finishes[a]) WrongAnswer(5);
finishes[a] = true;
if (finishes[b]) WrongAnswer(5);
finishes[b] = true;
if (C[a] != C[b]) WrongAnswer(6);
}
int main() {
if (scanf("%d", &N) != 1) {
fprintf(stderr, "Error while reading input.\n");
exit(1);
}
for (int i = 1; i <= N * 2; ++i) {
if (scanf("%d", &Y[i]) != 1) {
fprintf(stderr, "Error while reading input.\n");
exit(1);
}
}
for (int i = 1; i <= N * 2; ++i) {
if (scanf("%d", &C[i]) != 1) {
fprintf(stderr, "Error while reading input.\n");
exit(1);
}
}
for (int i = 1; i <= N * 2; ++i) {
if (scanf("%d", &L[i]) != 1) {
fprintf(stderr, "Error while reading input.\n");
exit(1);
}
}
for (int i = 1; i <= N * 2; ++i) finishes[i] = false;
Solve(N);
if (answer_count != N) WrongAnswer(7);
printf("Accepted: %d\n", query_count);
return 0;
}