Submission #928456

#TimeUsernameProblemLanguageResultExecution timeMemory
928456aykhnLongest Trip (IOI23_longesttrip)C++17
5 / 100
21 ms1732 KiB
#include <bits/stdc++.h> #include "longesttrip.h" using namespace std; map<pair<vector<int>, vector<int>>, int> mp; int ask(vector<int> a, vector<int> b) { if (a.empty() || b.empty()) return 0; if (mp.find({a, b}) != mp.end()) return mp[{a, b}]; return mp[{a, b}] = mp[{b, a}] = are_connected(a, b); } vector<int> longest_trip(int n, int D) { mp.clear(); vector<int> v1, v2; v1.push_back(0); for (int i = 1; i < n; i++) { int ask1 = ask(v1, {i}), ask2 = ask(v2, {i}); if (ask1 + ask2 == 1) { if (ask1) v1.push_back(i); else v2.push_back({i}); } else if (ask1 && ask2) { int id1 = -1, id2 = -1; int l = 0, r = (int)v1.size() - 1; while (l < r) { int mid = (l + r) >> 1; vector<int> x; for (int j = 0; j <= mid; j++) { x.push_back(v1[j]); } if (ask(x, {i})) r = mid; else l = mid + 1; } id1 = l; l = 0, r = (int)v2.size() - 1; while (l < r) { int mid = (l + r) >> 1; vector<int> x; for (int j = 0; j <= mid; j++) { x.push_back(v2[j]); } if (ask(x, {i})) r = mid; else l = mid + 1; } id2 = l; vector<int> nw; for (int j = (id2 + 1) % v2.size(); 1; j = (j + 1) % v2.size()) { nw.push_back(v2[j]); if (j == id2) break; } nw.push_back(i), nw.push_back(v1[id1]); for (int j = (id1 + 1) % v1.size(); j != id1; j = (j + 1) % v1.size()) { nw.push_back(v1[j]); } v2.clear(); v1 = nw; } else if (ask1) { vector<int> nw; if (ask({v1.back()}, {i})) { nw = v1; nw.push_back(i); } else { int l = 0, r = (int)v1.size() - 1; while (l < r) { int mid = (l + r) >> 1; vector<int> x; for (int j = 0; j <= mid; j++) { x.push_back(v1[j]); } if (ask(x, {i})) r = mid; else l = mid + 1; } int idx = l; nw.push_back(i), nw.push_back(v1[idx]); for (int j = (idx + 1) % v1.size(); j != idx; j = (j + 1) % v1.size()) { nw.push_back(v1[j]); } } v1 = nw; } else if (ask2) { vector<int> nw; if (ask({v2.back()}, {i})) { nw = v2; nw.push_back(i); } else { int l = 0, r = (int)v2.size() - 1; while (l < r) { int mid = (l + r) >> 1; vector<int> x; for (int j = 0; j <= mid; j++) { x.push_back(v2[j]); } if (ask(x, {i})) r = mid; else l = mid + 1; } int idx = l; nw.push_back(i), nw.push_back(v2[idx]); for (int j = (idx + 1) % v2.size(); j != idx; j = (j + 1) % v2.size()) { nw.push_back(v2[j]); } } v2 = nw; } else v2.push_back(i); } if (v1.size() > v2.size()) return v1; else return v2; };
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...