#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
mt19937_64 rnd(time(0));
using vi = vector<int>;
vi mrg(const vi&a, const vi&b) {
vi c = a;
for(int x : b) c.push_back(x);
return c;
}
vector<int> longest_trip(int n, int d)
{
deque<vi> pths;
auto add_back =[&](const vi&v) {
if(v.size()==1)pths.push_back(v);
else pths.push_front(v);
};
for(int i=0;i<n;i++)add_back({i});
shuffle(pths.begin(),pths.end(), rnd);
while(pths.size()>2){
auto v1 = pths.back();
pths.pop_back();
auto v2 = pths.back();
pths.pop_back();
if(are_connected({v1[0]}, {v2[0]})) {
reverse(all(v1));
auto v = mrg(v1,v2);
add_back(v);
}
else {
int no1 = 1, no2 = 1;
if(v1.size()==1)no1=2;
if(v2.size()==1)no2=2;
while(no1&&no2&&pths.size()){
auto v3 = pths.front();
pths.pop_front();
if(are_connected({v1[0]}, {v3[0]})){
reverse(all(v1));
v1 = mrg(v1,v3);
no1--;
}
else {
reverse(all(v2));
v2 = mrg(v2,v3);
no2--;
}
}
add_back(v1);
add_back(v2);
}
}
if(pths.size()==1)return pths[0];
auto v1 = pths[0], v2 = pths[1];
if(are_connected({v1[0]},{v2[0]})){
reverse(all(v2));
return mrg(v2,v1);
}
if(are_connected({v1[0]},{v2.back()})){
return mrg(v2,v1);
}
auto bsearch = [](vi v1, vi v2) {
int l=0,r=v2.size()-1;
while(l<r){
// cerr << "bs " << l << " " << r << endl;
int mid = (l+r)/2;
vi sv {v2.begin()+l,v2.begin()+mid+1};
if(are_connected(v1,sv)){
r=mid;
}
else {
l=mid+1;
}
}
return l;
};
if(are_connected({v1[0]},v2)){
int ind2 = bsearch({v1[0]},v2);
// cerr << "R1 " << ind2 << endl;
rotate(v2.begin(),v2.begin()+ind2,v2.end());
reverse(all(v1));
return mrg(v1,v2);
}
if(are_connected({v1.back()},{v2[0]})){
return mrg(v1,v2);
}
swap(v1,v2);
if(are_connected({v1[0]},v2)){
int ind2 = bsearch({v1[0]},v2);
rotate(v2.begin(),v2.begin()+ind2,v2.end());
reverse(all(v1));
return mrg(v1,v2);
}
if(are_connected(v1,v2)){
int ind2 = bsearch(v1,v2);
int ind1 = bsearch({v2[ind2]}, v1);
// cerr << ind1 << " " << ind2 << endl;
rotate(v1.begin(),v1.begin()+ind1,v1.end());
// cerr << "rotate1" << endl;
rotate(v2.begin(),v2.begin()+ind2,v2.end());
reverse(all(v1));
return mrg(v1,v2);
}
return v1.size() > v2.size() ? v1 : v2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
460 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
856 KB |
Output is correct |
10 |
Correct |
4 ms |
716 KB |
Output is correct |
11 |
Correct |
4 ms |
720 KB |
Output is correct |
12 |
Correct |
4 ms |
716 KB |
Output is correct |
13 |
Correct |
5 ms |
608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
460 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
852 KB |
Output is correct |
10 |
Correct |
5 ms |
860 KB |
Output is correct |
11 |
Correct |
5 ms |
968 KB |
Output is correct |
12 |
Correct |
5 ms |
716 KB |
Output is correct |
13 |
Correct |
5 ms |
464 KB |
Output is correct |
14 |
Correct |
15 ms |
420 KB |
Output is correct |
15 |
Correct |
10 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
344 KB |
Output is correct |
17 |
Correct |
5 ms |
600 KB |
Output is correct |
18 |
Correct |
5 ms |
596 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
5 ms |
608 KB |
Output is correct |
21 |
Correct |
6 ms |
720 KB |
Output is correct |
22 |
Correct |
6 ms |
984 KB |
Output is correct |
23 |
Correct |
5 ms |
864 KB |
Output is correct |
24 |
Correct |
6 ms |
1112 KB |
Output is correct |
25 |
Correct |
14 ms |
344 KB |
Output is correct |
26 |
Correct |
11 ms |
344 KB |
Output is correct |
27 |
Correct |
9 ms |
344 KB |
Output is correct |
28 |
Correct |
8 ms |
344 KB |
Output is correct |
29 |
Correct |
12 ms |
344 KB |
Output is correct |
30 |
Correct |
7 ms |
444 KB |
Output is correct |
31 |
Correct |
6 ms |
704 KB |
Output is correct |
32 |
Correct |
6 ms |
444 KB |
Output is correct |
33 |
Correct |
7 ms |
600 KB |
Output is correct |
34 |
Correct |
8 ms |
600 KB |
Output is correct |
35 |
Correct |
6 ms |
344 KB |
Output is correct |
36 |
Correct |
7 ms |
520 KB |
Output is correct |
37 |
Correct |
6 ms |
728 KB |
Output is correct |
38 |
Correct |
6 ms |
720 KB |
Output is correct |
39 |
Correct |
8 ms |
708 KB |
Output is correct |
40 |
Correct |
6 ms |
860 KB |
Output is correct |
41 |
Correct |
6 ms |
600 KB |
Output is correct |
42 |
Correct |
7 ms |
724 KB |
Output is correct |
43 |
Correct |
7 ms |
728 KB |
Output is correct |
44 |
Correct |
6 ms |
1236 KB |
Output is correct |
45 |
Correct |
9 ms |
344 KB |
Output is correct |
46 |
Correct |
10 ms |
344 KB |
Output is correct |
47 |
Correct |
8 ms |
344 KB |
Output is correct |
48 |
Correct |
9 ms |
344 KB |
Output is correct |
49 |
Correct |
10 ms |
344 KB |
Output is correct |
50 |
Correct |
6 ms |
440 KB |
Output is correct |
51 |
Correct |
6 ms |
444 KB |
Output is correct |
52 |
Correct |
7 ms |
440 KB |
Output is correct |
53 |
Correct |
6 ms |
1056 KB |
Output is correct |
54 |
Correct |
7 ms |
856 KB |
Output is correct |
55 |
Correct |
7 ms |
716 KB |
Output is correct |
56 |
Correct |
6 ms |
1008 KB |
Output is correct |
57 |
Correct |
6 ms |
728 KB |
Output is correct |
58 |
Correct |
6 ms |
984 KB |
Output is correct |
59 |
Correct |
6 ms |
960 KB |
Output is correct |
60 |
Correct |
7 ms |
740 KB |
Output is correct |
61 |
Correct |
6 ms |
992 KB |
Output is correct |
62 |
Correct |
8 ms |
1112 KB |
Output is correct |
63 |
Correct |
7 ms |
724 KB |
Output is correct |
64 |
Correct |
6 ms |
968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
600 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
11 ms |
344 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
600 KB |
Output is correct |
9 |
Correct |
6 ms |
448 KB |
Output is correct |
10 |
Correct |
4 ms |
460 KB |
Output is correct |
11 |
Correct |
4 ms |
456 KB |
Output is correct |
12 |
Correct |
4 ms |
712 KB |
Output is correct |
13 |
Correct |
5 ms |
456 KB |
Output is correct |
14 |
Correct |
11 ms |
344 KB |
Output is correct |
15 |
Correct |
14 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
608 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
5 ms |
856 KB |
Output is correct |
20 |
Correct |
5 ms |
600 KB |
Output is correct |
21 |
Correct |
10 ms |
344 KB |
Output is correct |
22 |
Correct |
16 ms |
344 KB |
Output is correct |
23 |
Correct |
11 ms |
344 KB |
Output is correct |
24 |
Correct |
9 ms |
344 KB |
Output is correct |
25 |
Correct |
9 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
692 KB |
Output is correct |
27 |
Correct |
7 ms |
444 KB |
Output is correct |
28 |
Correct |
6 ms |
448 KB |
Output is correct |
29 |
Correct |
8 ms |
600 KB |
Output is correct |
30 |
Correct |
6 ms |
600 KB |
Output is correct |
31 |
Correct |
7 ms |
600 KB |
Output is correct |
32 |
Correct |
9 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
344 KB |
Output is correct |
34 |
Correct |
7 ms |
344 KB |
Output is correct |
35 |
Correct |
6 ms |
344 KB |
Output is correct |
36 |
Correct |
7 ms |
344 KB |
Output is correct |
37 |
Correct |
5 ms |
696 KB |
Output is correct |
38 |
Correct |
7 ms |
692 KB |
Output is correct |
39 |
Correct |
7 ms |
440 KB |
Output is correct |
40 |
Correct |
5 ms |
856 KB |
Output is correct |
41 |
Correct |
6 ms |
600 KB |
Output is correct |
42 |
Correct |
6 ms |
864 KB |
Output is correct |
43 |
Correct |
5 ms |
872 KB |
Output is correct |
44 |
Correct |
6 ms |
980 KB |
Output is correct |
45 |
Correct |
6 ms |
720 KB |
Output is correct |
46 |
Correct |
5 ms |
724 KB |
Output is correct |
47 |
Correct |
7 ms |
612 KB |
Output is correct |
48 |
Correct |
6 ms |
1368 KB |
Output is correct |
49 |
Correct |
6 ms |
980 KB |
Output is correct |
50 |
Correct |
6 ms |
728 KB |
Output is correct |
51 |
Correct |
8 ms |
736 KB |
Output is correct |
52 |
Correct |
6 ms |
856 KB |
Output is correct |
53 |
Correct |
6 ms |
732 KB |
Output is correct |
54 |
Correct |
6 ms |
620 KB |
Output is correct |
55 |
Correct |
6 ms |
972 KB |
Output is correct |
56 |
Correct |
6 ms |
1236 KB |
Output is correct |
57 |
Correct |
6 ms |
732 KB |
Output is correct |
58 |
Correct |
6 ms |
728 KB |
Output is correct |
59 |
Correct |
7 ms |
976 KB |
Output is correct |
60 |
Correct |
6 ms |
472 KB |
Output is correct |
61 |
Correct |
7 ms |
608 KB |
Output is correct |
62 |
Correct |
7 ms |
1368 KB |
Output is correct |
63 |
Correct |
6 ms |
980 KB |
Output is correct |
64 |
Correct |
8 ms |
608 KB |
Output is correct |
65 |
Correct |
7 ms |
720 KB |
Output is correct |
66 |
Correct |
6 ms |
712 KB |
Output is correct |
67 |
Correct |
7 ms |
1232 KB |
Output is correct |
68 |
Correct |
7 ms |
860 KB |
Output is correct |
69 |
Correct |
7 ms |
724 KB |
Output is correct |
70 |
Correct |
6 ms |
980 KB |
Output is correct |
71 |
Correct |
7 ms |
868 KB |
Output is correct |
72 |
Correct |
7 ms |
720 KB |
Output is correct |
73 |
Correct |
7 ms |
612 KB |
Output is correct |
74 |
Correct |
7 ms |
484 KB |
Output is correct |
75 |
Correct |
6 ms |
728 KB |
Output is correct |
76 |
Correct |
7 ms |
876 KB |
Output is correct |
77 |
Correct |
6 ms |
740 KB |
Output is correct |
78 |
Correct |
8 ms |
860 KB |
Output is correct |
79 |
Correct |
9 ms |
980 KB |
Output is correct |
80 |
Correct |
8 ms |
860 KB |
Output is correct |
81 |
Correct |
8 ms |
604 KB |
Output is correct |
82 |
Correct |
8 ms |
1116 KB |
Output is correct |