#include "longesttrip.h"
#include <algorithm>
#include <random>
std::vector<int> longest_trip(int N, int D)
{
std::vector<int> pos = {};
for (int i = 0; i < N; i++)
{
pos.push_back(i);
}
// First create an instance of an engine.
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(begin(pos), end(pos), g);
std::vector<int> an1 = {};
std::vector<int> an2 = {};
an1.push_back(pos[0]);
std::vector<int> temp1 = {};
std::vector<int> temp2 = {};
int size = pos.size();
bool endnoconnect = false;
for (int ii = 1; ii < size; ii++)
{
int i = pos[ii];
temp1.clear();
temp1.push_back(i);
temp2.clear();
temp2.push_back(an1.back());
if (are_connected(temp1, temp2))
{
an1.push_back(i);
endnoconnect = false;
}
else
{
if (an2.size() == 0)
{
an2.push_back(i);
endnoconnect = true;
}
else
{
if (endnoconnect)
{
an2.push_back(i);
if (an2.size() > an1.size())
{
std::vector<int> an3 = an1;
an1 = an2;
an2 = an3;
}
endnoconnect = true;
}
else
{
temp2.clear();
temp2.push_back(an2.back());
if (are_connected(temp1, temp2))
{
an2.push_back(i);
if (an2.size() > an1.size())
{
std::vector<int> an3 = an1;
an1 = an2;
an2 = an3;
}
endnoconnect = true;
}
else
{
while (an2.size() > 0)
{
an1.push_back(an2.back());
an2.pop_back();
}
an2.push_back(i);
endnoconnect = false;
}
}
}
}
}
if (an2.size() > 0)
{
if (an2.size() > an1.size())
{
std::vector<int> an3 = an1;
an1 = an2;
an2 = an3;
}
temp1.clear();
temp1.push_back(an1.back());
temp2.clear();
temp2.push_back(an2.back());
if (are_connected(temp1, temp2))
{
while (an2.size() > 0)
{
an1.push_back(an2.back());
an2.pop_back();
}
}
else
{
temp2.clear();
temp2.push_back(an2.front());
if (are_connected(temp1, temp2))
{
int l = an2.size();
for (int i = 0; i < l; i++)
{
an1.push_back(an2[i]);
}
}
else
{
temp1.clear();
temp1.push_back(an1.front());
temp2.clear();
temp2.push_back(an2.back());
if (are_connected(temp1, temp2))
{
int l = an1.size();
for (int i = 0; i < l; i++)
{
an2.push_back(an1[i]);
}
an1 = an2;
}
else
{
if (are_connected(an1, an2))
{
int l = 0, r = an1.size() - 1;
while (l < r)
{
int m = (l + r) / 2;
temp1.clear();
for (int i = l; i <= m; i++)
{
temp1.push_back(an1[i]);
}
if (are_connected(temp1, an2))
{
r = m;
}
else
{
l = m + 1;
}
}
int c1 = l;
temp1.clear();
temp1.push_back(an1[c1]);
l = 0, r = an2.size() - 1;
while (l < r)
{
int m = (l + r) / 2;
temp2.clear();
for (int i = l; i <= m; i++)
{
temp2.push_back(an2[i]);
}
if (are_connected(temp1, temp2))
{
r = m;
}
else
{
l = m + 1;
}
}
int c2 = l;
std::vector<int> an3 = {};
int l1 = an1.size();
int l2 = an2.size();
for (int i = c1+1; i < l1; i++)
{
an3.push_back(an1[i]);
}
for (int i = 0; i <= c1; i++)
{
an3.push_back(an1[i]);
}
for (int i = c2; i < l2; i++)
{
an3.push_back(an2[i]);
}
for (int i = 0; i < c2; i++)
{
an3.push_back(an2[i]);
}
an1 = an3;
}
}
}
}
}
return an1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
2 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
208 KB |
Output is correct |
2 |
Correct |
9 ms |
208 KB |
Output is correct |
3 |
Correct |
7 ms |
208 KB |
Output is correct |
4 |
Correct |
6 ms |
208 KB |
Output is correct |
5 |
Correct |
8 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
208 KB |
Output is correct |
2 |
Correct |
9 ms |
208 KB |
Output is correct |
3 |
Correct |
6 ms |
208 KB |
Output is correct |
4 |
Correct |
5 ms |
208 KB |
Output is correct |
5 |
Correct |
6 ms |
208 KB |
Output is correct |
6 |
Correct |
13 ms |
208 KB |
Output is correct |
7 |
Correct |
10 ms |
208 KB |
Output is correct |
8 |
Correct |
8 ms |
208 KB |
Output is correct |
9 |
Correct |
8 ms |
300 KB |
Output is correct |
10 |
Correct |
8 ms |
208 KB |
Output is correct |
11 |
Correct |
8 ms |
208 KB |
Output is correct |
12 |
Correct |
6 ms |
208 KB |
Output is correct |
13 |
Correct |
8 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
208 KB |
Output is correct |
2 |
Correct |
9 ms |
208 KB |
Output is correct |
3 |
Correct |
9 ms |
228 KB |
Output is correct |
4 |
Correct |
7 ms |
208 KB |
Output is correct |
5 |
Correct |
8 ms |
208 KB |
Output is correct |
6 |
Correct |
12 ms |
208 KB |
Output is correct |
7 |
Correct |
10 ms |
208 KB |
Output is correct |
8 |
Correct |
8 ms |
208 KB |
Output is correct |
9 |
Correct |
7 ms |
208 KB |
Output is correct |
10 |
Correct |
8 ms |
208 KB |
Output is correct |
11 |
Correct |
7 ms |
208 KB |
Output is correct |
12 |
Correct |
9 ms |
208 KB |
Output is correct |
13 |
Correct |
8 ms |
208 KB |
Output is correct |
14 |
Correct |
17 ms |
208 KB |
Output is correct |
15 |
Correct |
11 ms |
208 KB |
Output is correct |
16 |
Correct |
10 ms |
208 KB |
Output is correct |
17 |
Correct |
8 ms |
208 KB |
Output is correct |
18 |
Correct |
9 ms |
208 KB |
Output is correct |
19 |
Correct |
8 ms |
208 KB |
Output is correct |
20 |
Correct |
7 ms |
208 KB |
Output is correct |
21 |
Correct |
8 ms |
208 KB |
Output is correct |
22 |
Correct |
8 ms |
296 KB |
Output is correct |
23 |
Correct |
9 ms |
208 KB |
Output is correct |
24 |
Correct |
9 ms |
208 KB |
Output is correct |
25 |
Correct |
16 ms |
208 KB |
Output is correct |
26 |
Correct |
21 ms |
208 KB |
Output is correct |
27 |
Correct |
11 ms |
208 KB |
Output is correct |
28 |
Correct |
10 ms |
208 KB |
Output is correct |
29 |
Correct |
12 ms |
208 KB |
Output is correct |
30 |
Correct |
5 ms |
208 KB |
Output is correct |
31 |
Correct |
12 ms |
208 KB |
Output is correct |
32 |
Correct |
12 ms |
292 KB |
Output is correct |
33 |
Correct |
8 ms |
208 KB |
Output is correct |
34 |
Correct |
8 ms |
208 KB |
Output is correct |
35 |
Correct |
7 ms |
208 KB |
Output is correct |
36 |
Correct |
9 ms |
208 KB |
Output is correct |
37 |
Correct |
10 ms |
208 KB |
Output is correct |
38 |
Correct |
6 ms |
208 KB |
Output is correct |
39 |
Correct |
11 ms |
208 KB |
Output is correct |
40 |
Correct |
5 ms |
332 KB |
Output is correct |
41 |
Correct |
9 ms |
304 KB |
Output is correct |
42 |
Correct |
10 ms |
300 KB |
Output is correct |
43 |
Correct |
9 ms |
300 KB |
Output is correct |
44 |
Correct |
9 ms |
208 KB |
Output is correct |
45 |
Correct |
9 ms |
208 KB |
Output is correct |
46 |
Correct |
13 ms |
256 KB |
Output is correct |
47 |
Correct |
12 ms |
208 KB |
Output is correct |
48 |
Correct |
9 ms |
208 KB |
Output is correct |
49 |
Correct |
15 ms |
208 KB |
Output is correct |
50 |
Correct |
11 ms |
208 KB |
Output is correct |
51 |
Correct |
11 ms |
208 KB |
Output is correct |
52 |
Correct |
12 ms |
208 KB |
Output is correct |
53 |
Correct |
7 ms |
208 KB |
Output is correct |
54 |
Correct |
8 ms |
208 KB |
Output is correct |
55 |
Correct |
10 ms |
208 KB |
Output is correct |
56 |
Correct |
7 ms |
208 KB |
Output is correct |
57 |
Correct |
8 ms |
208 KB |
Output is correct |
58 |
Correct |
10 ms |
296 KB |
Output is correct |
59 |
Correct |
11 ms |
208 KB |
Output is correct |
60 |
Correct |
9 ms |
208 KB |
Output is correct |
61 |
Correct |
10 ms |
208 KB |
Output is correct |
62 |
Correct |
10 ms |
208 KB |
Output is correct |
63 |
Correct |
11 ms |
208 KB |
Output is correct |
64 |
Correct |
11 ms |
208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
228 KB |
Output is correct |
2 |
Correct |
8 ms |
208 KB |
Output is correct |
3 |
Correct |
8 ms |
208 KB |
Output is correct |
4 |
Correct |
7 ms |
208 KB |
Output is correct |
5 |
Correct |
7 ms |
208 KB |
Output is correct |
6 |
Correct |
11 ms |
208 KB |
Output is correct |
7 |
Correct |
9 ms |
208 KB |
Output is correct |
8 |
Correct |
6 ms |
208 KB |
Output is correct |
9 |
Correct |
8 ms |
208 KB |
Output is correct |
10 |
Correct |
8 ms |
208 KB |
Output is correct |
11 |
Correct |
7 ms |
208 KB |
Output is correct |
12 |
Correct |
6 ms |
208 KB |
Output is correct |
13 |
Correct |
6 ms |
208 KB |
Output is correct |
14 |
Correct |
15 ms |
208 KB |
Output is correct |
15 |
Correct |
13 ms |
208 KB |
Output is correct |
16 |
Correct |
10 ms |
208 KB |
Output is correct |
17 |
Correct |
9 ms |
208 KB |
Output is correct |
18 |
Correct |
10 ms |
208 KB |
Output is correct |
19 |
Correct |
9 ms |
208 KB |
Output is correct |
20 |
Correct |
9 ms |
300 KB |
Output is correct |
21 |
Correct |
15 ms |
208 KB |
Output is correct |
22 |
Correct |
17 ms |
208 KB |
Output is correct |
23 |
Correct |
12 ms |
208 KB |
Output is correct |
24 |
Correct |
10 ms |
332 KB |
Output is correct |
25 |
Correct |
9 ms |
208 KB |
Output is correct |
26 |
Correct |
7 ms |
208 KB |
Output is correct |
27 |
Correct |
9 ms |
208 KB |
Output is correct |
28 |
Correct |
10 ms |
208 KB |
Output is correct |
29 |
Correct |
7 ms |
208 KB |
Output is correct |
30 |
Correct |
9 ms |
260 KB |
Output is correct |
31 |
Correct |
9 ms |
208 KB |
Output is correct |
32 |
Correct |
10 ms |
208 KB |
Output is correct |
33 |
Correct |
13 ms |
208 KB |
Output is correct |
34 |
Correct |
15 ms |
208 KB |
Output is correct |
35 |
Correct |
11 ms |
260 KB |
Output is correct |
36 |
Correct |
16 ms |
208 KB |
Output is correct |
37 |
Correct |
6 ms |
208 KB |
Output is correct |
38 |
Correct |
9 ms |
208 KB |
Output is correct |
39 |
Correct |
10 ms |
208 KB |
Output is correct |
40 |
Correct |
9 ms |
300 KB |
Output is correct |
41 |
Correct |
8 ms |
208 KB |
Output is correct |
42 |
Correct |
8 ms |
208 KB |
Output is correct |
43 |
Correct |
8 ms |
208 KB |
Output is correct |
44 |
Correct |
7 ms |
208 KB |
Output is correct |
45 |
Correct |
9 ms |
208 KB |
Output is correct |
46 |
Correct |
7 ms |
256 KB |
Output is correct |
47 |
Correct |
8 ms |
208 KB |
Output is correct |
48 |
Correct |
6 ms |
256 KB |
Output is correct |
49 |
Correct |
8 ms |
208 KB |
Output is correct |
50 |
Correct |
9 ms |
208 KB |
Output is correct |
51 |
Correct |
10 ms |
332 KB |
Output is correct |
52 |
Correct |
8 ms |
300 KB |
Output is correct |
53 |
Correct |
6 ms |
304 KB |
Output is correct |
54 |
Correct |
7 ms |
296 KB |
Output is correct |
55 |
Correct |
11 ms |
292 KB |
Output is correct |
56 |
Correct |
8 ms |
208 KB |
Output is correct |
57 |
Correct |
8 ms |
208 KB |
Output is correct |
58 |
Correct |
11 ms |
300 KB |
Output is correct |
59 |
Correct |
9 ms |
208 KB |
Output is correct |
60 |
Correct |
9 ms |
208 KB |
Output is correct |
61 |
Correct |
11 ms |
304 KB |
Output is correct |
62 |
Correct |
9 ms |
208 KB |
Output is correct |
63 |
Correct |
8 ms |
208 KB |
Output is correct |
64 |
Correct |
7 ms |
208 KB |
Output is correct |
65 |
Correct |
10 ms |
208 KB |
Output is correct |
66 |
Correct |
9 ms |
208 KB |
Output is correct |
67 |
Correct |
10 ms |
304 KB |
Output is correct |
68 |
Correct |
10 ms |
208 KB |
Output is correct |
69 |
Correct |
9 ms |
208 KB |
Output is correct |
70 |
Correct |
10 ms |
208 KB |
Output is correct |
71 |
Correct |
7 ms |
296 KB |
Output is correct |
72 |
Correct |
11 ms |
208 KB |
Output is correct |
73 |
Correct |
8 ms |
208 KB |
Output is correct |
74 |
Correct |
9 ms |
208 KB |
Output is correct |
75 |
Correct |
8 ms |
260 KB |
Output is correct |
76 |
Correct |
9 ms |
208 KB |
Output is correct |
77 |
Correct |
7 ms |
208 KB |
Output is correct |
78 |
Correct |
9 ms |
208 KB |
Output is correct |
79 |
Correct |
9 ms |
208 KB |
Output is correct |
80 |
Correct |
9 ms |
296 KB |
Output is correct |
81 |
Correct |
12 ms |
296 KB |
Output is correct |
82 |
Correct |
9 ms |
208 KB |
Output is correct |