제출 #1080518

#제출 시각아이디문제언어결과실행 시간메모리
1080518aykhn가장 긴 여행 (IOI23_longesttrip)C++17
85 / 100
13 ms600 KiB
#include "longesttrip.h" #include <bits/stdc++.h> using namespace std; vector<int> longest_trip(int N, int D) { vector<int> p1 = {0}, p2 = {1}; for (int i = 2; i < N; i++) { if (are_connected({p1.back()}, {i})) p1.push_back(i); else if (are_connected({p2.back()}, {i})) p2.push_back(i); else { while (!p2.empty()) { p1.push_back(p2.back()); p2.pop_back(); } p2 = {i}; } } if (p1.size() < p2.size()) swap(p1, p2); if (!are_connected(p1, p2)) return p1; if (are_connected({p1.back()}, {p2.back()})) { while (!p2.empty()) { p1.push_back(p2.back()); p2.pop_back(); } } else if (are_connected({p1[0]}, {p2.back()})) { for (int &i : p1) p2.push_back(i); p1 = {}; swap(p1, p2); } else if (are_connected({p1.back()}, {p2[0]})) { for (int &i : p2) p1.push_back(i); p2 = {}; } else if (are_connected({p1[0]}, {p2[0]})) { reverse(p1.begin(), p1.end()); for (int &i : p2) p1.push_back(i); p2 = {}; } else { { int l = 0, r = (int)p1.size() - 1; while (l < r) { int mid = (l + r) >> 1; vector<int> q; for (int i = l; i <= mid; i++) q.push_back(p1[i]); if (are_connected(q, p2)) r = mid; else l = mid + 1; } vector<int> nw = {p1[l]}; for (int i = (l + 1) % (int)p1.size(); i != l; i = (i + 1) % (int)p1.size()) { nw.push_back(p1[i]); } swap(nw, p1); } { int l = 0, r = (int)p2.size() - 1; while (l < r) { int mid = (l + r) >> 1; vector<int> q; for (int i = l; i <= mid; i++) q.push_back(p2[i]); if (are_connected(q, {p1[0]})) r = mid; else l = mid + 1; } vector<int> nw = {p2[l]}; for (int i = (l + 1) % (int)p2.size(); i != l; i = (i + 1) % (int)p2.size()) { nw.push_back(p2[i]); } swap(nw, p2); } reverse(p1.begin(), p1.end()); for (int &i : p2) p1.push_back(i); p2 = {}; } return p1; }
#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...