Submission #232796

#TimeUsernameProblemLanguageResultExecution timeMemory
232796pedy4000Zagonetka (COI18_zagonetka)C++14
100 / 100
83 ms508 KiB
#include <algorithm> #include <iostream> #include <vector> #include <set> using namespace std; typedef long long ll; const int N = 1e2 + 2; int n, k, ans; int p[N], q[N], ind[N]; int a[N], b[N]; bool adj[N][N]; vector <int> vec; vector <int> G[N]; set <int> Set; void dfs (int v) { for (int u: G[v]) if (a[u] == 0) dfs(u); a[v] = *Set.begin(); Set.erase(Set.begin()); } void DFS (int v) { for (int u: G[v]) if (b[u] == 0) DFS(u); b[v] = *(--Set.end()); Set.erase(--Set.end()); } int main() { ios::sync_with_stdio(false), cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> p[i]; ind[p[i]] = i; } for (int i = 1; i <= n; i++) adj[i][i] = true; for (int dif = 1; dif < n; dif++) for (int l = 1; l + dif <= n; l++) { int r = l + dif; bool ok = false; for (int k = l + 1; k < r; k++) if (adj[l][k] && adj[k][r]) { ok = true; break; } if (ok) { adj[l][r] = true; continue; } for (int k = l; k <= r; k++) if (adj[l][k] || adj[k][r]) vec.push_back(k); sort(vec.begin(), vec.end()); for (int i = 0; i < n; i++) q[i] = p[i]; for (int k = r; l <= k; k--) if (adj[l][k]) { q[ind[k]] = vec.back(); vec.pop_back(); } for (int k = r; l <= k; k--) if (adj[k][r]) { q[ind[k]] = vec.back(); vec.pop_back(); } cout << "query "; for (int i = 0; i < n; i++) cout << q[i] << " "; cout << "\n" << flush; cin >> ok; adj[l][r] = !ok; } for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) if (adj[i][j]) G[ind[j]].push_back(ind[i]); for (int i = 0; i < n; i++) sort(G[i].begin(), G[i].end()); for (int i = 1; i <= n; i++) Set.insert(i); for (int i = 0; i < n; i++) if (a[i] == 0) dfs(i); for (int i = 0; i < n; i++) G[i].clear(); for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) if (adj[i][j]) G[ind[i]].push_back(ind[j]); for (int i = 0; i < n; i++) sort(G[i].begin(), G[i].end()); for (int i = 1; i <= n; i++) Set.insert(i); for (int i = 0; i < n; i++) if (b[i] == 0) DFS(i); cout << "end\n"; for (int i = 0; i < n; i++) cout << a[i] << " "; cout << "\n"; for (int i = 0; i < n; i++) cout << b[i] << " "; cout << "\n" << flush; 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...