Submission #911230

#TimeUsernameProblemLanguageResultExecution timeMemory
911230biankLongest Trip (IOI23_longesttrip)C++17
100 / 100
11 ms864 KiB
#include <bits/stdc++.h> using namespace std; #define ALL(x) begin(x),end(x) #define SIZE(x) (int)x.size() #define forn(i,n) for(int i=0;i<int(n);i++) #define dforn(i,n) for(int i=int(n)-1;i>=0;i--) #define forsn(i,s,n) for(int i=int(s);i<int(n);i++) #define pb push_back typedef vector<int> vi; bool are_connected(vi A, vi B); int n; const int MAXN = 256; int c[MAXN][MAXN]; bool query(int x, int y) { if(c[x][y]==1) return true; if(c[x][y]==0) return false; forn(z,n) { if(z==x||z==y) continue; if(c[x][z]==0 && c[y][z]==0) { c[x][y]=c[y][x]=1; return true; } } c[x][y]=c[y][x]=are_connected({x},{y}); return c[x][y]; } int bs(vi a, vi b) { int l=0, r=SIZE(a); while(l+1<r) { int m=(l+r)/2; if(are_connected(b,vi(a.begin()+m,a.begin()+r))) l=m; else r=m; } return l; } vi longest_trip(int N, int D) { n=N; memset(c,-1,sizeof c); (void)D; vi P; forn(i,n) P.pb(i); srand(time(0)); random_shuffle(ALL(P)); vi a={P[0]}, b; forsn(i,1,n) { if(query(a.back(),P[i])) { a.pb(P[i]); } else if(!b.empty() && query(a.back(),b.back())) { dforn(j,SIZE(b)) a.pb(b[j]); b={P[i]}; } else if(b.empty() || query(b.back(),P[i])) { b.pb(P[i]); } } if(SIZE(b)>SIZE(a)) swap(a,b); if(b.empty()||!are_connected(a,b)) return a; if(query(a.back(),b.front())) { forn(i,SIZE(b)) a.pb(b[i]); return a; } else if(query(b.back(),a.front())) { forn(i,SIZE(a)) b.pb(a[i]); return b; } else if(query(a.front(),b.front())) { reverse(ALL(a)); forn(i,SIZE(b)) a.pb(b[i]); return a; } else if(query(a.back(),b.back())) { dforn(i,SIZE(b)) a.pb(b[i]); return a; } int i=bs(a,b); int j=bs(b,{a[i]}); vi ans; forsn(p,j+1,SIZE(b)) ans.pb(b[p]); forn(p,j+1) ans.pb(b[p]); forsn(p,i,SIZE(a)) ans.pb(a[p]); forn(p,i) ans.pb(a[p]); 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...