#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;
deque<deque<int>> combine(deque<deque<int>> p, int a, int b) {
deque<deque<int>> erg;
for (int i = 0; i < p.size(); i++)
if (i != a && i != b) erg.push_back(p[i]);
for (int i: p[a]) p[b].push_front(i);
erg.push_back(p[b]);
return erg;
}
vector<int> v(deque<int> d) {
vector<int> erg;
for (int i: d)
erg.push_back(i);
return erg;
}
int search(deque<int> a, deque<int> b) {
deque<int> l, r;
while (a.size() > 1) {
for (int i = 0; i < a.size(); i++)
{
(i % 2 ? l : r).push_back(a[i]);
}
a = are_connected(v(l), v(b)) ? l : r;
l.clear(); r.clear();
}
return a[0];
}
void push(vector<int> & v, deque<int> d, int ind) {
for (int i = 0; i < d.size(); i++)
{
v.push_back(d[(ind + i) % d.size()]);
}
}
int find(deque<int> d, int v) {
for (int i = 0; i < d.size(); i++)
if (d[i] == v) return i;
}
vector<int> longest_trip(int N, int D)
{
deque<int> unused;
for (int i = 0; i < N; i++)
unused.push_back(i);
deque<deque<int>> paths;
while (unused.size() >= 4) {
random_shuffle(unused.begin(), unused.end());
if (are_connected({unused[0]}, {unused[1]})) {
paths.push_back({unused[0], unused[1]});
for (int i = 0; i < 2; i++)
unused.pop_front();
} else {
deque<int> p0{unused[0]}, p1{unused[1]};
(are_connected({unused[0]}, {unused[2]}) ? p0 : p1).push_front(unused[2]);
(are_connected({unused[0]}, {unused[3]}) ? p0 : p1).push_back(unused[3]);
int u = -1;
if (p0.size() == 1) u = unused[0]; else paths.push_back(p0);
if (p1.size() == 1) u = unused[1]; else paths.push_back(p1);
for (int i = 0; i < 4; i++)
unused.pop_front();
if (u != -1) unused.push_front(u);
}
}
for (int i: unused) paths.push_back({i});
while (paths.size() > 2) {
random_shuffle(paths.begin(), paths.end());
if (are_connected({paths[0].front()}, {paths[1].front()})) {
paths = combine(paths, 0, 1);
} else if (are_connected({paths[0].front()}, {paths[2].front()})) {
paths = combine(paths, 0, 2);
} else {
paths = combine(paths, 1, 2);
}
}
if (are_connected(v(paths[0]), v(paths[1]))) {
if (are_connected({paths[0].front()}, {paths[1].front()})) {
return v(combine({paths[0], paths[1]}, 0, 1)[0]);
}
if (are_connected({paths[0].front()}, {paths[1].back()})) {
reverse(paths[1].begin(), paths[1].end());
return v(combine({paths[0], paths[1]}, 0, 1)[0]);
}
if (are_connected({paths[0].back()}, {paths[1].front()})) {
reverse(paths[0].begin(), paths[0].end());
return v(combine({paths[0], paths[1]}, 0, 1)[0]);
}
int c0 = find(paths[0], search(paths[0], paths[1]));
int c1 = find(paths[1], search(paths[1], {paths[0][c0]}));
vector<int> erg;
push(erg, paths[0], c0 + 1);
push(erg, paths[1], c1);
assert(erg.size() == N);
return erg;
} else {
assert(paths[0].size() + paths[1].size() == N);
return v(paths[0].size() > paths[1].size() ? paths[0] : paths[1]);
}
}
Compilation message
longesttrip.cpp: In function 'std::deque<std::deque<int> > combine(std::deque<std::deque<int> >, int, int)':
longesttrip.cpp:8:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<std::deque<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | for (int i = 0; i < p.size(); i++)
| ~~^~~~~~~~~~
longesttrip.cpp: In function 'int search(std::deque<int>, std::deque<int>)':
longesttrip.cpp:25:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int i = 0; i < a.size(); i++)
| ~~^~~~~~~~~~
longesttrip.cpp: In function 'void push(std::vector<int>&, std::deque<int>, int)':
longesttrip.cpp:36:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int i = 0; i < d.size(); i++)
| ~~^~~~~~~~~~
longesttrip.cpp: In function 'int find(std::deque<int>, int)':
longesttrip.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int i = 0; i < d.size(); i++)
| ~~^~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from longesttrip.cpp:2:
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:99:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
99 | assert(erg.size() == N);
| ~~~~~~~~~~~^~~~
longesttrip.cpp:102:50: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
102 | assert(paths[0].size() + paths[1].size() == N);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
longesttrip.cpp: In function 'int find(std::deque<int>, int)':
longesttrip.cpp:45:1: warning: control reaches end of non-void function [-Wreturn-type]
45 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
7 ms |
344 KB |
Output is correct |
4 |
Correct |
9 ms |
344 KB |
Output is correct |
5 |
Correct |
12 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
8 ms |
600 KB |
Output is correct |
4 |
Correct |
9 ms |
344 KB |
Output is correct |
5 |
Correct |
13 ms |
600 KB |
Output is correct |
6 |
Correct |
12 ms |
344 KB |
Output is correct |
7 |
Correct |
8 ms |
344 KB |
Output is correct |
8 |
Correct |
7 ms |
344 KB |
Output is correct |
9 |
Correct |
9 ms |
548 KB |
Output is correct |
10 |
Correct |
13 ms |
600 KB |
Output is correct |
11 |
Correct |
17 ms |
712 KB |
Output is correct |
12 |
Correct |
13 ms |
596 KB |
Output is correct |
13 |
Correct |
18 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
8 ms |
344 KB |
Output is correct |
3 |
Correct |
8 ms |
600 KB |
Output is correct |
4 |
Correct |
9 ms |
344 KB |
Output is correct |
5 |
Correct |
13 ms |
600 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
9 ms |
344 KB |
Output is correct |
8 |
Correct |
7 ms |
488 KB |
Output is correct |
9 |
Correct |
9 ms |
344 KB |
Output is correct |
10 |
Correct |
13 ms |
600 KB |
Output is correct |
11 |
Correct |
12 ms |
708 KB |
Output is correct |
12 |
Correct |
16 ms |
600 KB |
Output is correct |
13 |
Correct |
14 ms |
708 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
8 ms |
344 KB |
Output is correct |
16 |
Correct |
11 ms |
344 KB |
Output is correct |
17 |
Correct |
7 ms |
344 KB |
Output is correct |
18 |
Correct |
8 ms |
652 KB |
Output is correct |
19 |
Correct |
9 ms |
344 KB |
Output is correct |
20 |
Correct |
9 ms |
544 KB |
Output is correct |
21 |
Correct |
13 ms |
600 KB |
Output is correct |
22 |
Correct |
13 ms |
600 KB |
Output is correct |
23 |
Correct |
13 ms |
852 KB |
Output is correct |
24 |
Correct |
13 ms |
600 KB |
Output is correct |
25 |
Correct |
8 ms |
344 KB |
Output is correct |
26 |
Correct |
6 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 |
6 ms |
344 KB |
Output is correct |
30 |
Correct |
8 ms |
508 KB |
Output is correct |
31 |
Correct |
12 ms |
500 KB |
Output is correct |
32 |
Correct |
13 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
344 KB |
Output is correct |
34 |
Correct |
9 ms |
344 KB |
Output is correct |
35 |
Correct |
14 ms |
600 KB |
Output is correct |
36 |
Correct |
14 ms |
600 KB |
Output is correct |
37 |
Correct |
13 ms |
600 KB |
Output is correct |
38 |
Correct |
19 ms |
924 KB |
Output is correct |
39 |
Correct |
22 ms |
684 KB |
Output is correct |
40 |
Correct |
13 ms |
600 KB |
Output is correct |
41 |
Correct |
14 ms |
600 KB |
Output is correct |
42 |
Correct |
15 ms |
600 KB |
Output is correct |
43 |
Correct |
14 ms |
680 KB |
Output is correct |
44 |
Correct |
18 ms |
848 KB |
Output is correct |
45 |
Correct |
9 ms |
344 KB |
Output is correct |
46 |
Correct |
7 ms |
344 KB |
Output is correct |
47 |
Correct |
10 ms |
344 KB |
Output is correct |
48 |
Correct |
9 ms |
340 KB |
Output is correct |
49 |
Correct |
9 ms |
344 KB |
Output is correct |
50 |
Correct |
9 ms |
344 KB |
Output is correct |
51 |
Correct |
13 ms |
504 KB |
Output is correct |
52 |
Correct |
10 ms |
344 KB |
Output is correct |
53 |
Correct |
9 ms |
344 KB |
Output is correct |
54 |
Correct |
14 ms |
532 KB |
Output is correct |
55 |
Correct |
10 ms |
344 KB |
Output is correct |
56 |
Correct |
20 ms |
600 KB |
Output is correct |
57 |
Correct |
14 ms |
688 KB |
Output is correct |
58 |
Correct |
14 ms |
688 KB |
Output is correct |
59 |
Correct |
14 ms |
600 KB |
Output is correct |
60 |
Correct |
15 ms |
600 KB |
Output is correct |
61 |
Correct |
21 ms |
600 KB |
Output is correct |
62 |
Correct |
14 ms |
600 KB |
Output is correct |
63 |
Correct |
15 ms |
600 KB |
Output is correct |
64 |
Correct |
16 ms |
680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
7 ms |
492 KB |
Output is correct |
4 |
Correct |
8 ms |
344 KB |
Output is correct |
5 |
Correct |
17 ms |
600 KB |
Output is correct |
6 |
Correct |
10 ms |
344 KB |
Output is correct |
7 |
Correct |
9 ms |
344 KB |
Output is correct |
8 |
Correct |
7 ms |
344 KB |
Output is correct |
9 |
Correct |
8 ms |
344 KB |
Output is correct |
10 |
Correct |
13 ms |
600 KB |
Output is correct |
11 |
Correct |
12 ms |
708 KB |
Output is correct |
12 |
Correct |
14 ms |
600 KB |
Output is correct |
13 |
Correct |
17 ms |
712 KB |
Output is correct |
14 |
Correct |
10 ms |
344 KB |
Output is correct |
15 |
Correct |
6 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
344 KB |
Output is correct |
17 |
Correct |
8 ms |
344 KB |
Output is correct |
18 |
Correct |
8 ms |
600 KB |
Output is correct |
19 |
Correct |
9 ms |
344 KB |
Output is correct |
20 |
Correct |
10 ms |
532 KB |
Output is correct |
21 |
Correct |
5 ms |
340 KB |
Output is correct |
22 |
Correct |
6 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 |
8 ms |
416 KB |
Output is correct |
26 |
Correct |
9 ms |
600 KB |
Output is correct |
27 |
Correct |
11 ms |
344 KB |
Output is correct |
28 |
Correct |
9 ms |
600 KB |
Output is correct |
29 |
Correct |
9 ms |
548 KB |
Output is correct |
30 |
Correct |
11 ms |
344 KB |
Output is correct |
31 |
Correct |
9 ms |
344 KB |
Output is correct |
32 |
Correct |
8 ms |
344 KB |
Output is correct |
33 |
Correct |
9 ms |
344 KB |
Output is correct |
34 |
Correct |
9 ms |
344 KB |
Output is correct |
35 |
Correct |
12 ms |
344 KB |
Output is correct |
36 |
Correct |
8 ms |
344 KB |
Output is correct |
37 |
Correct |
9 ms |
344 KB |
Output is correct |
38 |
Correct |
10 ms |
500 KB |
Output is correct |
39 |
Correct |
10 ms |
496 KB |
Output is correct |
40 |
Correct |
11 ms |
600 KB |
Output is correct |
41 |
Correct |
10 ms |
344 KB |
Output is correct |
42 |
Correct |
10 ms |
344 KB |
Output is correct |
43 |
Correct |
13 ms |
600 KB |
Output is correct |
44 |
Correct |
13 ms |
600 KB |
Output is correct |
45 |
Correct |
15 ms |
856 KB |
Output is correct |
46 |
Correct |
18 ms |
696 KB |
Output is correct |
47 |
Correct |
13 ms |
700 KB |
Output is correct |
48 |
Correct |
13 ms |
600 KB |
Output is correct |
49 |
Correct |
13 ms |
600 KB |
Output is correct |
50 |
Correct |
13 ms |
600 KB |
Output is correct |
51 |
Correct |
17 ms |
660 KB |
Output is correct |
52 |
Correct |
18 ms |
676 KB |
Output is correct |
53 |
Correct |
14 ms |
600 KB |
Output is correct |
54 |
Correct |
16 ms |
924 KB |
Output is correct |
55 |
Correct |
22 ms |
600 KB |
Output is correct |
56 |
Correct |
14 ms |
600 KB |
Output is correct |
57 |
Correct |
14 ms |
600 KB |
Output is correct |
58 |
Correct |
15 ms |
600 KB |
Output is correct |
59 |
Correct |
14 ms |
600 KB |
Output is correct |
60 |
Correct |
19 ms |
684 KB |
Output is correct |
61 |
Correct |
14 ms |
600 KB |
Output is correct |
62 |
Correct |
17 ms |
720 KB |
Output is correct |
63 |
Correct |
13 ms |
600 KB |
Output is correct |
64 |
Correct |
15 ms |
600 KB |
Output is correct |
65 |
Correct |
14 ms |
680 KB |
Output is correct |
66 |
Correct |
14 ms |
680 KB |
Output is correct |
67 |
Correct |
21 ms |
692 KB |
Output is correct |
68 |
Correct |
14 ms |
600 KB |
Output is correct |
69 |
Correct |
15 ms |
600 KB |
Output is correct |
70 |
Correct |
15 ms |
600 KB |
Output is correct |
71 |
Correct |
14 ms |
600 KB |
Output is correct |
72 |
Correct |
14 ms |
856 KB |
Output is correct |
73 |
Correct |
14 ms |
672 KB |
Output is correct |
74 |
Correct |
16 ms |
684 KB |
Output is correct |
75 |
Correct |
15 ms |
600 KB |
Output is correct |
76 |
Correct |
16 ms |
672 KB |
Output is correct |
77 |
Correct |
16 ms |
696 KB |
Output is correct |
78 |
Correct |
16 ms |
1104 KB |
Output is correct |
79 |
Correct |
15 ms |
600 KB |
Output is correct |
80 |
Correct |
15 ms |
600 KB |
Output is correct |
81 |
Correct |
14 ms |
676 KB |
Output is correct |
82 |
Correct |
15 ms |
600 KB |
Output is correct |