Submission #840251

#TimeUsernameProblemLanguageResultExecution timeMemory
840251TranGiaHuy1508Longest Trip (IOI23_longesttrip)C++17
100 / 100
19 ms308 KiB
#include <bits/stdc++.h> using namespace std; #include "longesttrip.h" vector<int> subrange(vector<int> &V, int l, int r){ vector<int> res(V.begin() + l, V.begin() + r + 1); return res; } vector<int> longest_trip(int N, int D){ // Assuming D = 1 vector<int> v1 = {0}, v2 = {1}; bool stupid = false; for (int i = 2; i < N; i++){ if (are_connected({v1.back()}, {i})){ v1.push_back(i); stupid = false; } else if (stupid || are_connected({v2.back()}, {i})){ v2.push_back(i); stupid = true; } else{ reverse(v2.begin(), v2.end()); v1.insert(v1.end(), v2.begin(), v2.end()); v2.clear(); v2.push_back(i); stupid = false; } } bool bruh = false; if (v1.front() != v1.back() && !are_connected({v1.front()}, {v1.back()})) bruh = true; if (v2.front() != v2.back() && !are_connected({v2.front()}, {v2.back()})) bruh = true; if (bruh){ if (are_connected({v1.front()}, {v2.front()})){ reverse(v1.begin(), v1.end()); v1.insert(v1.end(), v2.begin(), v2.end()); return v1; } if (are_connected({v1.back()}, {v2.front()})){ v1.insert(v1.end(), v2.begin(), v2.end()); return v1; } if (are_connected({v1.front()}, {v2.back()})){ v2.insert(v2.end(), v1.begin(), v1.end()); return v2; } if (are_connected({v1.back()}, {v2.back()})){ reverse(v1.begin(), v1.end()); v2.insert(v2.end(), v1.begin(), v1.end()); return v2; } } if (!are_connected(v1, v2)){ if (v1.size() > v2.size()) return v1; return v2; } int l1 = 0, r1 = v1.size() - 1; int l2 = 0, r2 = v2.size() - 1; while (l1 < r1){ int mid = (l1 + r1) >> 1; bool edge = are_connected(subrange(v1, l1, mid), subrange(v2, l2, r2)); if (edge) r1 = mid; else l1 = mid + 1; } while (l2 < r2){ int mid = (l2 + r2) >> 1; bool edge = are_connected(subrange(v1, l1, r1), subrange(v2, l2, mid)); if (edge) r2 = mid; else l2 = mid + 1; } vector<int> res1, res2; for (int i = l1; i < (int)v1.size(); i++) res1.push_back(v1[i]); for (int i = 0; i < l1; i++) res1.push_back(v1[i]); for (int i = l2; i < (int)v2.size(); i++) res2.push_back(v2[i]); for (int i = 0; i < l2; i++) res2.push_back(v2[i]); reverse(res1.begin(), res1.end()); res2.insert(res2.begin(), res1.begin(), res1.end()); return res2; }
#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...