# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
44180 | ShadowLight | Zagonetka (COI18_zagonetka) | 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>
#define ll long long
#define TASKNAME ""
using namespace std;
const ll INF = 1e9 + 7;
const ll MAXN = 1e6 + 7;
const double EPS = 1e-8;
vector <vector <ll> > gr;
vector <vector <ll> > rev_gr;
ll n;
vector <ll> p;
vector <ll> pos;
void query(vector <ll> a) {
cout << "query ";
for (ll x : a) {
cout << x + 1 << " ";
}
cout << endl;
}
vector <bool> used;
vector <ll> resa, resb;
void dfs(ll v) {
if (used[v]) return;
used[v] = 1;
for (ll u : rev_gr[v]) {
dfs(u);
}
}
ll last = 0;
void dfs1(ll v) {
if (resa[v] != -1) return;
vector <ll> a;
for (ll u : gr[p[v]]) {
a.push_back(pos[u]);
}
sort(a.begin(), a.end());
for (ll x : a) {
dfs1(x);
}
resa[v] = last++;
}
void dfs2(ll v) {
if (resb[v] != -1) return;
vector <ll> a;
for (ll u : rev_gr[p[v]]) {
a.push_back(pos[u]);
}
sort(a.begin(), a.end());
for (ll x : a) {
dfs2(x);
}
resb[v] = last--;
}
ll main() {
#ifdef MY
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#else
//freopen(TASKNAME".in", "r", stdin);
//freopen(TASKNAME".out", "w", stdout);
#endif // MY
cin >> n;
p.resize(n);
pos.resize(n, 0);
for (ll i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
pos[p[i]] = i;
}
gr.resize(n);
rev_gr.resize(n);
ll iter = 0;
for (ll i = n - 1; i >= 0; i--) {
used.assign(n, 0);
for (ll j = i + 1; j < n; j++) {
if (used[j]) {
gr[j].push_back(i);
rev_gr[i].push_back(j);
iter++;
continue;
}
auto q = p;
ll v = j;
while (gr[v].size()) {
ll u = gr[v][0];
swap(q[pos[v]], q[pos[u]]);
v = u;
}
swap(q[pos[v]], q[pos[i]]);
v = i;
while (rev_gr[v].size()) {
ll u = rev_gr[v][0];
if (u > j) break;
swap(q[pos[v]], q[pos[u]]);
v = u;
}
query(q);
ll ans;
cin >> ans;
if (!ans) {
gr[j].push_back(i);
rev_gr[i].push_back(j);
//cout << j << " " << i << "\n";
dfs(j);
iter++;
}
}
}
resa.resize(n, -1);
resb.resize(n, -1);
for (ll v = 0; v < n; v++) {
if (resa[v] == -1) {
dfs1(v);
}
}
last = n - 1;
for (ll v = 0; v < n; v++) {
if (resb[v] == -1) {
dfs2(v);
}
}
cout << "end" << endl;
for (ll x : resa) {
cout << x + 1 << " ";
}
cout << endl;
for (ll x : resb) {
cout << x + 1 << " ";
}
cout << endl;
}