#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;
forn(i,N) q.push({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,i+1,SIZE(a)) ans.pb(a[p]);
forn(p,i+1) ans.pb(a[p]);
forsn(p,j+1,SIZE(b)) ans.pb(b[p]);
forn(p,j+1) ans.pb(b[p]);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
452 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
6 ms |
856 KB |
Output is correct |
10 |
Correct |
5 ms |
456 KB |
Output is correct |
11 |
Correct |
5 ms |
464 KB |
Output is correct |
12 |
Correct |
5 ms |
456 KB |
Output is correct |
13 |
Correct |
5 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
600 KB |
Output is correct |
10 |
Correct |
5 ms |
460 KB |
Output is correct |
11 |
Correct |
5 ms |
456 KB |
Output is correct |
12 |
Correct |
5 ms |
460 KB |
Output is correct |
13 |
Correct |
6 ms |
608 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Incorrect |
5 ms |
344 KB |
Incorrect |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
600 KB |
Output is correct |
4 |
Correct |
5 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
456 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
600 KB |
Output is correct |
10 |
Correct |
5 ms |
860 KB |
Output is correct |
11 |
Correct |
5 ms |
608 KB |
Output is correct |
12 |
Correct |
5 ms |
632 KB |
Output is correct |
13 |
Correct |
5 ms |
608 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Incorrect |
5 ms |
344 KB |
Incorrect |
17 |
Halted |
0 ms |
0 KB |
- |