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 "doll.h"
#include <iostream>
#include <algorithm>
#include <cassert>
#include <random>
#define le(v) ((int)v.size())
#define pb push_back
using namespace std;
vector<int> X, Y;
int inc = 0;
int go(int outs) {
int id = le(X);
X.pb(-1e9); Y.pb(-1e9);
if (outs > 2) {
if (outs & 1) {
outs++;
inc++;
}
X[id] = go(outs / 2);
Y[id] = go(outs / 2);
} else assert(outs == 2);
return -(id + 1);
}
mt19937 rng(1337);
void create_circuit(int M, vector<int> A) {
X.clear(); Y.clear();
int N = A.size();
vector<vector<int>> after(M + 1);
vector<int> wait(M + 1);
A.pb(0);
for (int i = 0; i < le(A) - 1; i++) {
after[A[i]].pb(A[i + 1]);
}
A.pop_back();
vector<int> C(M + 1);
C[0] = A[0];
for (int i = 1; i <= M; i++) {
if (!after[i].empty()) {
if (le(after[i]) == 1) {
C[i] = after[i][0];
} else {
int sz = le(after[i]);
inc = 0;
C[i] = go(sz);
wait[i] = inc;
}
}
reverse(after[i].begin(), after[i].end());
}
// for (int x : wait) {
// cout << x << " ";
// }
// return;
int cur = C[0];
int real = -1;
while (cur) {
// cerr << cur << " ";
if (cur > 0) {
real = cur;
cur = C[cur];
} else {
cur = (-cur) - 1;
if (X[cur] == -1e9) {
if (wait[real]
&& ((le(after[real]) == 1) || ((wait[real] + le(after[real])) % 2 == 0))) {
wait[real]--;
X[cur] = C[real];
} else {
X[cur] = after[real].back();
after[real].pop_back();
}
}
swap(X[cur], Y[cur]);
cur = Y[cur];
}
}
vector<bool> del(le(X), false);
for (int it = 0; it < 100; it++) {
for (int i = 0; i < le(X); i++) {
if (del[i]) continue;
if (X[i] < 0) {
int j = (-X[i]) - 1;
if (X[j] == Y[j]) {
X[i] = X[j];
del[j] = true;
}
}
if (Y[i] < 0) {
int j = (-Y[i]) - 1;
if (X[j] == Y[j]) {
Y[i] = X[j];
del[j] = true;
}
}
}
for (int i = 0; i < le(C); i++) {
if (C[i] < 0) {
int j = (-C[i]) - 1;
if (X[j] == Y[j]) {
C[i] = X[j];
del[j] = true;
}
}
}
}
vector<int> alive;
for (int i = 0; i < le(X); i++) {
if (!del[i]) {
alive.pb(i);
}
}
for (int i = 0; i < le(X); i++) {
if (X[i] < 0) {
int id = lower_bound(alive.begin(), alive.end(), (-X[i]) - 1) - alive.begin();
X[i] = -(id + 1);
}
if (Y[i] < 0) {
int id = lower_bound(alive.begin(), alive.end(), (-Y[i]) - 1) - alive.begin();
Y[i] = -(id + 1);
}
}
for (int i = 0; i < le(C); i++) {
if (C[i] < 0) {
int id = lower_bound(alive.begin(), alive.end(), (-C[i]) - 1) - alive.begin();
C[i] = -(id + 1);
}
}
int jump = 0;
for (int i = 0; i < le(X); i++) {
if (del[i]) {
jump++;
} else {
X[i - jump] = X[i];
Y[i - jump] = Y[i];
}
}
X.resize(le(X) - jump);
Y.resize(le(Y) - jump);
// for (int x : del) {
// cout << x << " ";
// }
// cout << endl;
// for (int i = 0; i < le(X); i++) {
// cout << X[i] << " " << Y[i] << endl;
// }
answer(C, X, Y);
}
Compilation message (stderr)
doll.cpp: In function 'void create_circuit(int, std::vector<int>)':
doll.cpp:31:6: warning: unused variable 'N' [-Wunused-variable]
31 | int N = A.size();
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |