Submission #44173

#TimeUsernameProblemLanguageResultExecution timeMemory
44173ShadowLightZagonetka (COI18_zagonetka)C++14
0 / 100
77 ms500 KiB
#include <bits/stdc++.h> #define ll long long #define TASKNAME "" using namespace std; const int INF = 1e9 + 7; const int MAXN = 1e6 + 7; const double EPS = 1e-8; vector <vector <int> > gr; vector <vector <int> > rev_gr; int n; vector <int> p; vector <int> pos; void query(vector <int> a) { cout << "query "; for (int x : a) { cout << x + 1 << " "; } cout << endl; } vector <bool> used; void dfs(int v) { if (used[v]) return; used[v] = 1; for (int u : rev_gr[v]) { dfs(u); } } vector <int> lvls; vector <bool> used1; int 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 (int i = 0; i < n; i++) { cin >> p[i]; p[i]--; pos[p[i]] = i; } gr.resize(n); rev_gr.resize(n); int iter = 0; for (int i = n - 1; i >= 0; i--) { used.assign(n, 0); for (int 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; int v = j; while (gr[v].size()) { int 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()) { int u = rev_gr[v][0]; if (u > j) break; swap(q[pos[v]], q[pos[u]]); v = u; } query(q); int ans; cin >> ans; if (!ans) { gr[j].push_back(i); rev_gr[i].push_back(j); dfs(j); iter++; } } } vector <int> resa(n, -1), resb(n, -1); multiset <int> q; vector <int> cnt(n, 0); for (int i = 0; i < n; i++) { cnt[i] = (int) gr[i].size(); if (!cnt[i]) { q.insert(pos[i]); } } int now = 0; while (!q.empty()) { int v = *q.begin(); q.erase(v); resa[v] = now++; for (int u : rev_gr[p[v]]) { cnt[u]--; if (!cnt[u]) { q.insert(pos[u]); } } } q.clear(); for (int i = 0; i < n; i++) { cnt[i] = (int) rev_gr[i].size(); if (!cnt[i]) { q.insert(pos[i]); } } now = n - 1; while (!q.empty()) { int v = *q.begin(); q.erase(v); resb[v] = now--; for (int u : gr[p[v]]) { cnt[u]--; if (!cnt[u]) { q.insert(pos[u]); } } } cout << "end" << endl; for (int x : resa) { assert(x != -1); cout << x + 1 << " "; } cout << endl; for (int x : resb) { assert(x != -1); cout << x + 1 << " "; } cout << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...