Submission #847521

#TimeUsernameProblemLanguageResultExecution timeMemory
847521rainboyLongest Trip (IOI23_longesttrip)C++17
5 / 100
9 ms600 KiB
/* upsolved using some inspiration from EmeraldBlock */ #include "longesttrip.h" #include <vector> using namespace std; typedef vector<int> vi; const int N = 256; int qu1[N], qu2[N], cnt1, cnt2; vi longest_trip(int n, int d) { cnt1 = cnt2 = 0; for (int i = 0; i < n; i++) if (cnt1 == 0) qu1[cnt1++] = i; else if (cnt2 == 0) qu2[cnt2++] = i; else if (are_connected({ qu1[cnt1 - 1] }, { i })) qu1[cnt1++] = i; else if (i + 1 == n) { if (!are_connected({ qu2[cnt2 - 1] }, { i })) while (cnt2) qu1[cnt1++] = qu2[--cnt2]; qu2[cnt2++] = i; } else { if (are_connected({ i }, { i + 1 })) { if (!are_connected({ qu2[cnt2 - 1] }, { i })) while (cnt2) qu1[cnt1++] = qu2[--cnt2]; qu2[cnt2++] = i, qu2[cnt2++] = i + 1; } else { qu1[cnt1++] = i + 1; if (!are_connected({ qu2[cnt2 - 1] }, { i })) while (cnt2) qu1[cnt1++] = qu2[--cnt2]; qu2[cnt2++] = i; } i++; } vi uu, vv, ww; if (cnt2 == 0) for (int h = 0; h < cnt1; h++) ww.push_back(qu1[h]); else if (cnt1 == 0) for (int h = 0; h < cnt2; h++) ww.push_back(qu2[h]); else { vi uu, vv; int lower = 0, upper = cnt1 + 1; while (upper - lower > 1) { int k = (lower + upper) / 2; uu.clear(); for (int h = 0; h < k; h++) uu.push_back(qu1[(h - 1 + cnt1) % cnt1]); vv.clear(); for (int h = 0; h < cnt2; h++) vv.push_back(qu2[h]); if (are_connected(uu, vv)) upper = k; else lower = k; } int h1 = lower; if (h1 == cnt1) { if (cnt1 > cnt2) for (int h = 0; h < cnt1; h++) ww.push_back(qu1[h]); else for (int h = 0; h < cnt2; h++) ww.push_back(qu2[h]); } else { lower = 0, upper = cnt2; while (upper - lower > 1) { int k = (lower + upper) / 2; uu.clear(); for (int h = 0; h < cnt1; h++) uu.push_back(qu1[h]); vv.clear(); for (int h = 0; h < k; h++) vv.push_back(qu2[(h - 1 + cnt2) % cnt2]); if (are_connected(uu, vv)) upper = k; else lower = k; } int h2 = lower; if (h1 == 1) for (int h = cnt1 - 1; h >= 0; h--) ww.push_back(qu1[h]); else for (int h = 0; h < cnt1; h++) ww.push_back(qu1[(h1 + h) % cnt1]); if (h2 == 1) for (int h = 0; h < cnt2; h++) ww.push_back(qu2[h]); else for (int h = cnt2 - 1; h >= 0; h--) ww.push_back(qu2[(h2 + h) % cnt2]); } } return ww; }
#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...