#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 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);
vi longest_trip(int N, int D) {
(void)D;
queue<vi> q;
vi P(N);
forn(i,N) P.pb(i);
srand(time(0));
random_shuffle(ALL(P));
forn(i,N) q.push({P[i]});
auto plop = [&](){
vi x = q.front();
q.pop();
return x;
};
while(SIZE(q)>=3) {
vi a = plop(),
b = plop(),
c = plop();
if(are_connected({a.back()},{b.front()})) {
forn(i,SIZE(b)) a.pb(b[i]);
q.push(a), q.push(c);
} else if(are_connected({a.back()},{c.front()})) {
forn(i,SIZE(c)) a.pb(c[i]);
q.push(a), q.push(b);
} else {
reverse(ALL(b));
forn(i,SIZE(c)) b.pb(c[i]);
q.push(b), q.push(a);
}
}
vi a = plop(), b = plop();
if(SIZE(b)>SIZE(a)) swap(a,b);
if(b.empty() || !are_connected(a,b)) return a;
vi qa = {a.front()};
if(a.back() != qa.back()) qa.pb(a.back());
vi qb = {b.front()};
if(b.back() != qb.back()) qb.pb(b.back());
if(are_connected(qa, qb)) {
if(are_connected({a.back()}, {b.front()})) {
forn(i,SIZE(b)) a.pb(b[i]);
return a;
}
if(are_connected({b.back()}, {a.front()})) {
forn(i,SIZE(a)) b.pb(a[i]);
return b;
}
if(are_connected({a.front()},{b.front()})) {
reverse(ALL(a));
forn(i,SIZE(b)) a.pb(b[i]);
return a;
}
if(are_connected({a.back()}, {b.back()})) {
reverse(ALL(b));
forn(i,SIZE(b)) a.pb(b[i]);
return a;
}
}
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;
}
int i=l;
l=0, r=SIZE(b);
while(l+1<r) {
int m=(l+r)/2;
if(are_connected({a[i]},vi(b.begin()+m,b.begin()+r))) l=m;
else r=m;
}
int j=l;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
non-disjoint arrays |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
non-disjoint arrays |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
non-disjoint arrays |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
non-disjoint arrays |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
non-disjoint arrays |
2 |
Halted |
0 ms |
0 KB |
- |