Submission #841571

#TimeUsernameProblemLanguageResultExecution timeMemory
841571TheLostCookieLongest Trip (IOI23_longesttrip)C++17
100 / 100
10 ms856 KiB
#include "longesttrip.h" #include <functional> #include <vector> std::vector<int> longest_trip(int N, int D) { //Invariant: l0.back() and l1.back() are NOT connected. std::vector<int> l0,l1; auto mergeLines = [&l0, &l1]()->void{ while(!l1.empty()) { l0.push_back(l1.back()); l1.pop_back(); } return; }; auto q = [](int u, int v)->bool{ return are_connected({u},{v}); }; l0.push_back(0); int u=1; while(u<N) { if(l1.empty()) { (q(l0.back(),u)?l0:l1).push_back(u); u++; } else if(u==N-1) { if(!q(l0.back(),u)) l1.push_back(u); else { l0.push_back(u); if(q(l1.back(),u)) { mergeLines(); } } u++; } else { int v=u+1; bool b0 = q(l0.back(),u), b1 = q(l1.back(),v), b2 = q(u,v); if(b0&&b1) { l0.push_back(u); l1.push_back(v); if(b2) mergeLines(); } else if(!b0&&!b1) { l0.push_back(v); l1.push_back(u); if(b2) mergeLines(); } else if(b0&&!b1) { if(b2) { l0.push_back(u); l0.push_back(v); } else { l1.push_back(u); l0.push_back(v); } } else { if(b2) { l1.push_back(v); l1.push_back(u); } else { l0.push_back(v); l1.push_back(u); } } u+=2; } } if(l1.empty()) return l0; else if(!are_connected(l0,l1)) { if(l0.size()>l1.size()) return l0; else return l1; } else if(l0.size()>1&&(!are_connected({l0.front()},{l0.back()}))) { std::reverse(l0.begin(),l0.end()); mergeLines(); return l0; } else if(l1.size()>1&&(!are_connected({l1.front()},{l1.back()}))) { std::reverse(l1.begin(),l1.end()); mergeLines(); return l0; } else { std::vector<int> le(l0), ri(l1); for(int rep = 0; rep < 2; rep++) { while(le.size()>1) { std::vector<int> le0,le1; for(int i = 0; i < int(le.size()); i++) { (i%2?le0:le1).push_back(le[i]); } if(are_connected(le0,ri)) std::swap(le,le0); else std::swap(le,le1); } swap(le,ri); } int lei = 0, rii = 0; while(l0[lei] != le[0]) lei++; while(l1[rii] != ri[0]) rii++; std::vector<int> ans; for(int i = 0; i < int(l0.size()); i++) { ans.push_back(l0[(lei+i+1)%l0.size()]); } for(int i = 0; i < int(l1.size()); i++) { ans.push_back(l1[(rii+i)%l1.size()]); } return ans; } }
#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...