# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
574226 | MadokaMagicaFan | Mechanical Doll (IOI18_doll) | 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 "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll inf = 1e9;
const int md1 = 1e9+7;
const int md2 = 998244353;
#define sz(v) ((int)v.size())
#define pb push_back
#define pry cout << "YES\n"
#define prn cout << "NO\n"
#define endl '\n'
#define fst first
#define scn second
/* #define ONPC */
const int M = 1e5;
vector<int> v[M+5], x, y, c;
void answer(vector<int> C, vector<int> X, vector<int> Y);
int
news()
{
x.pb(0);
y.pb(0);
return sz(x);
}
int
dfs(int i, int s, int k, int ind)
{
if (k == x[ind-1]) {
if (s >= sz(v[i])-1) {
if (s == (1<<x[ind-1])-1)
return v[i][sz(v[i])-1];
else
return -ind;
}
return v[i][s];
}
int _x = dfs(i,s,k+1,ind);
int _y = dfs(i,s+(1<<k),k+1,ind);
if (_x == _y)
return _x;
int index = news();
x[index-1] = _x;
y[index-1] = _y;
return -index;
}
int
donode(int i)
{
if (sz(v[i]) == 1) return v[i][0];
int l = 31 - __builtin_clz(sz(v[i]));
if (sz(v[i]) != (1<<l))
++l;
int ind = news();
x[ind-1] = l;
int _x = dfs(i,0,1,ind);
int _y = dfs(i,1,1,ind);
x[ind-1] = _x;
y[ind-1] = _y;
return -ind;
}
void
construct_network(int m, vector<int> a)
{
int n = sz(a);
for (int i = 0; i < n-1; ++i)
v[a[i]].pb(a[i+1]);
v[a[n-1]].pb(0);
v[0].pb(a[0]);
for (int i = 0; i < m+1; ++i) {
if (sz(v[i]) == 0) {
c.pb(1);
continue;
}
c.pb(donode(i));
}
answer(c,x,y);
return;
}
#ifdef ONPC
void
answer(vector<int> C, vector<int> X, vector<int> Y)
{
vector<int> ans;
vector<bool> state(sz(X),0);
int tg = C[0];
while (tg) {
if (tg >= 0) {
ans.pb(tg);
tg = C[tg];
} else {
int index = -tg-1;
if (state[index]==0) tg = X[index];
else tg = Y[index];
state[index] = !state[index];
}
}
for (int i = 0; i < sz(X); ++i) {
if (state[i]) {
cout << "Error\n";
return;
}
}
cout << sz(ans) << endl;
for (int i = 0; i < sz(ans); ++i) {
cout << ans[i] << ' ';
}
cout << endl;
return;
}
void
solve()
{
int n, m;
cin >> m;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
cout << m << ' ';
construct_network(m, a);
}
int32_t
main(int argc, char **argv)
{
if (argc >= 2) {
freopen(argv[1], "r", stdin);
} else
ios_base::sync_with_stdio(0);cin.tie(0);
int t = 1;
/* cin >> t; */
while(t--)
solve();
}
#endif