Submission #967551

#TimeUsernameProblemLanguageResultExecution timeMemory
967551Jarif_RahmanLongest Trip (IOI23_longesttrip)C++17
70 / 100
17 ms960 KiB
#include "longesttrip.h" #include <bits/stdc++.h> #define pb push_back #define f first #define sc second using namespace std; typedef long long int ll; typedef string str; vector<int> longest_trip(int n, int D){ vector<int> A = {0}, B = {}; for(int i = 1; i < n; i++){ if(B.empty()){ if(are_connected({A.back()}, {i})) A.push_back(i); else if(are_connected({A[0]}, {i})) A.insert(A.begin(), i); else B = {i}; } else{ if(are_connected({A.back()}, {i})) A.push_back(i); else if(are_connected({B.back()}, {i})) B.push_back(i); else{ reverse(B.begin(), B.end()); A.insert(A.end(), B.begin(), B.end()); B.clear(); i--; } } if(A.size() < B.size()) swap(A, B); } int na = A.size(), nb = B.size(); int ans = nb, x = -1, y = -1; for(int i = 0; i < nb; i++){ if(!are_connected({B[i]}, A)) continue; int l = 0, r = na-1; while(l < r){ int m = (l+r)/2; vector<int> s; for(int j = 0; j <= m; j++) s.push_back(A[j]); if(are_connected({B[i]}, s)) r = m; else l = m+1; } if(max(i+1, nb-i)+max(l+1, na-l) > ans){ ans = max(i+1, nb-i)+max(l+1, na-l), x = l, y = i; } l = 0, r = na-1; while(l < r){ int m = (l+r+1)/2; vector<int> s; for(int j = m; j < na; j++) s.push_back(A[j]); if(are_connected({B[i]}, s)) l = m; else r = m-1; } if(max(i+1, nb-i)+max(l+1, na-l) > ans){ ans = max(i+1, nb-i)+max(l+1, na-l), x = l, y = i; } } vector<int> C = A; if(x != -1){ C.clear(); if(na > 1 && are_connected({A[0]}, {A.back()})){ for(int i = x+1; i <= na+x; i++) C.push_back(A[i%na]); } else if(na-x > x+1){ for(int i = na-1; i >= x; i--) C.push_back(A[i]); } else{ for(int i = 0; i <= x; i++) C.push_back(A[i]); } if(nb > 1 && are_connected({B[0]}, {B.back()})){ for(int i = y; i < nb+y; i++) C.push_back(B[i%nb]); } else if(nb-y > y+1){ for(int i = y; i < nb; i++) C.push_back(B[i]); } else{ for(int i = y; i >= 0; i--) C.push_back(B[i]); } } return C; }
#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...