#include "bits/stdc++.h"
#include "longesttrip.h"
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define sz(x) ((int)(x).size())
#define pb push_back
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N=300;
int n, d;
map<pii, bool> mp;
bool con(int a, int b) {
if(mp.count({a,b})) return mp[{a,b}];
if(mp.count({b,a})) return mp[{b,a}];
return mp[{a,b}]=are_connected({a},{b});
}
void mrg(vi &a, vi &b) {
while(!b.empty()) {
a.pb(b.back());
b.pop_back();
}
}
void dbg(vi &a, vi&b) {
cout << "a: "; for(auto it : a) cout << it << " "; cout << endl;
cout << "b: "; for(auto it : b) cout << it << " "; cout << endl;
}
std::vector<int> longest_trip(int N, int D) {
mp.clear();
n=N; d=D;
vi in; FOR(i, 0, n) in.pb(i);
shuffle(in.begin(), in.end(), rng);
bool conn=true;
vi a, b; a.pb(in[0]);
FOR(i, 1, n) {
if(b.empty()) {
b.pb(in[i]);
continue;
}
if(con(a.back(), in[i])) a.pb(in[i]), conn=true;
else if(!conn) b.pb(in[i]);
else if(con(b.back(), in[i])) b.pb(in[i]), conn=false;
else mrg(a, b), b.pb(in[i]), conn=true;
}
if(b.empty()) return a;
if(con(a[0], b[0])) {
reverse(a.begin(), a.end());
for(auto it : b) a.pb(it);
return a;
}
if(con(a[0], b.back())) {
for(auto it : a) b.pb(it);
return b;
}
if(con(a.back(), b[0])) {
for(auto it : b) a.pb(it);
return a;
}
if(con(a.back(), b.back())) {
while(!b.empty()) a.pb(b.back()), b.pop_back();
return a;
}
if(sz(b)>sz(a)) swap(a, b);
if(!are_connected(a, b)) return a;
int l=0, r=sz(b)-1;
while(l<r) {
int m=(l+r)/2;
vi tmp; FOR(i, l, m+1) tmp.pb(b[i]);
if(are_connected(a, tmp)) r=m;
else l=m+1;
}
assert(are_connected({b[l]}, a));
int bin=l;
l=0; r=sz(a)-1;
while(l<r) {
int m=(l+r)/2;
vi tmp; FOR(i, l, m+1) tmp.pb(a[i]);
if(are_connected({b[bin]}, tmp)) r=m;
else l=m+1;
}
int ain=l;
// cout << ":\n";
// for(auto it : a) cout << it << " "; cout << endl;
// for(auto it : b) cout << it << " ";
// cout << "ins: " << ain << " " << bin << endl;
vi ans;
FOR(i, 1, sz(a)+1) {
int id=(ain+i)%sz(a);
ans.pb(a[id]);
}
FOR(i, 0, sz(b)) {
int id=(bin+i)%sz(b);
ans.pb(b[id]);
}
return ans;
}
/*
1
5 1
1
1 1
0 0 1
0 0 0 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 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 |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
8 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
344 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
8 ms |
456 KB |
Output is correct |
11 |
Correct |
6 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
344 KB |
Output is correct |
13 |
Correct |
8 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 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 |
344 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
6 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
9 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
440 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
6 ms |
344 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
6 ms |
596 KB |
Output is correct |
22 |
Correct |
7 ms |
344 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
6 ms |
344 KB |
Output is correct |
25 |
Correct |
10 ms |
344 KB |
Output is correct |
26 |
Correct |
10 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
7 ms |
344 KB |
Output is correct |
29 |
Correct |
7 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
344 KB |
Output is correct |
31 |
Correct |
9 ms |
344 KB |
Output is correct |
32 |
Correct |
7 ms |
344 KB |
Output is correct |
33 |
Correct |
6 ms |
344 KB |
Output is correct |
34 |
Correct |
8 ms |
344 KB |
Output is correct |
35 |
Correct |
9 ms |
344 KB |
Output is correct |
36 |
Correct |
6 ms |
440 KB |
Output is correct |
37 |
Correct |
6 ms |
344 KB |
Output is correct |
38 |
Correct |
9 ms |
352 KB |
Output is correct |
39 |
Correct |
7 ms |
344 KB |
Output is correct |
40 |
Correct |
9 ms |
344 KB |
Output is correct |
41 |
Correct |
7 ms |
344 KB |
Output is correct |
42 |
Correct |
7 ms |
344 KB |
Output is correct |
43 |
Correct |
9 ms |
344 KB |
Output is correct |
44 |
Correct |
8 ms |
344 KB |
Output is correct |
45 |
Correct |
6 ms |
344 KB |
Output is correct |
46 |
Correct |
7 ms |
344 KB |
Output is correct |
47 |
Correct |
12 ms |
344 KB |
Output is correct |
48 |
Correct |
8 ms |
344 KB |
Output is correct |
49 |
Correct |
11 ms |
344 KB |
Output is correct |
50 |
Correct |
8 ms |
344 KB |
Output is correct |
51 |
Correct |
8 ms |
344 KB |
Output is correct |
52 |
Correct |
7 ms |
600 KB |
Output is correct |
53 |
Correct |
6 ms |
344 KB |
Output is correct |
54 |
Correct |
7 ms |
344 KB |
Output is correct |
55 |
Correct |
7 ms |
344 KB |
Output is correct |
56 |
Correct |
6 ms |
344 KB |
Output is correct |
57 |
Correct |
6 ms |
344 KB |
Output is correct |
58 |
Correct |
7 ms |
344 KB |
Output is correct |
59 |
Correct |
9 ms |
344 KB |
Output is correct |
60 |
Correct |
7 ms |
344 KB |
Output is correct |
61 |
Correct |
8 ms |
344 KB |
Output is correct |
62 |
Correct |
8 ms |
344 KB |
Output is correct |
63 |
Correct |
8 ms |
344 KB |
Output is correct |
64 |
Correct |
12 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
8 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
7 ms |
344 KB |
Output is correct |
10 |
Correct |
7 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
6 ms |
344 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
10 ms |
344 KB |
Output is correct |
16 |
Correct |
9 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
340 KB |
Output is correct |
19 |
Correct |
6 ms |
344 KB |
Output is correct |
20 |
Correct |
6 ms |
344 KB |
Output is correct |
21 |
Correct |
11 ms |
344 KB |
Output is correct |
22 |
Correct |
8 ms |
344 KB |
Output is correct |
23 |
Correct |
7 ms |
344 KB |
Output is correct |
24 |
Correct |
6 ms |
344 KB |
Output is correct |
25 |
Correct |
9 ms |
344 KB |
Output is correct |
26 |
Correct |
6 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
344 KB |
Output is correct |
28 |
Correct |
8 ms |
700 KB |
Output is correct |
29 |
Correct |
5 ms |
344 KB |
Output is correct |
30 |
Correct |
6 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
344 KB |
Output is correct |
32 |
Correct |
10 ms |
344 KB |
Output is correct |
33 |
Correct |
7 ms |
344 KB |
Output is correct |
34 |
Correct |
11 ms |
344 KB |
Output is correct |
35 |
Correct |
8 ms |
344 KB |
Output is correct |
36 |
Correct |
8 ms |
344 KB |
Output is correct |
37 |
Correct |
10 ms |
344 KB |
Output is correct |
38 |
Correct |
11 ms |
344 KB |
Output is correct |
39 |
Correct |
13 ms |
344 KB |
Output is correct |
40 |
Correct |
7 ms |
344 KB |
Output is correct |
41 |
Correct |
6 ms |
344 KB |
Output is correct |
42 |
Correct |
5 ms |
344 KB |
Output is correct |
43 |
Correct |
6 ms |
384 KB |
Output is correct |
44 |
Correct |
8 ms |
344 KB |
Output is correct |
45 |
Correct |
9 ms |
344 KB |
Output is correct |
46 |
Correct |
7 ms |
344 KB |
Output is correct |
47 |
Correct |
8 ms |
344 KB |
Output is correct |
48 |
Correct |
6 ms |
344 KB |
Output is correct |
49 |
Correct |
6 ms |
344 KB |
Output is correct |
50 |
Correct |
9 ms |
344 KB |
Output is correct |
51 |
Correct |
7 ms |
344 KB |
Output is correct |
52 |
Correct |
7 ms |
344 KB |
Output is correct |
53 |
Correct |
7 ms |
344 KB |
Output is correct |
54 |
Correct |
7 ms |
344 KB |
Output is correct |
55 |
Correct |
7 ms |
344 KB |
Output is correct |
56 |
Correct |
6 ms |
344 KB |
Output is correct |
57 |
Correct |
6 ms |
444 KB |
Output is correct |
58 |
Correct |
7 ms |
344 KB |
Output is correct |
59 |
Correct |
8 ms |
344 KB |
Output is correct |
60 |
Correct |
9 ms |
460 KB |
Output is correct |
61 |
Correct |
7 ms |
344 KB |
Output is correct |
62 |
Correct |
6 ms |
344 KB |
Output is correct |
63 |
Correct |
7 ms |
344 KB |
Output is correct |
64 |
Correct |
7 ms |
344 KB |
Output is correct |
65 |
Correct |
7 ms |
344 KB |
Output is correct |
66 |
Correct |
9 ms |
600 KB |
Output is correct |
67 |
Correct |
8 ms |
344 KB |
Output is correct |
68 |
Correct |
8 ms |
344 KB |
Output is correct |
69 |
Correct |
9 ms |
600 KB |
Output is correct |
70 |
Correct |
7 ms |
344 KB |
Output is correct |
71 |
Correct |
6 ms |
344 KB |
Output is correct |
72 |
Correct |
8 ms |
600 KB |
Output is correct |
73 |
Correct |
7 ms |
344 KB |
Output is correct |
74 |
Correct |
11 ms |
444 KB |
Output is correct |
75 |
Correct |
8 ms |
344 KB |
Output is correct |
76 |
Correct |
7 ms |
344 KB |
Output is correct |
77 |
Correct |
7 ms |
344 KB |
Output is correct |
78 |
Correct |
7 ms |
344 KB |
Output is correct |
79 |
Correct |
7 ms |
344 KB |
Output is correct |
80 |
Correct |
7 ms |
344 KB |
Output is correct |
81 |
Correct |
8 ms |
344 KB |
Output is correct |
82 |
Correct |
8 ms |
344 KB |
Output is correct |