// partially_correct/sol_na_D1_stable.cpp
#include "longesttrip.h"
#include <deque>
#include <functional>
#include <algorithm>
#include <iostream>
std::vector<int> longest_trip(int N, int D)
{
srand(time(0));
std::vector<int> ids(N);
for (int i = 0; i < N; i++)
ids[i] = i;
random_shuffle(ids.begin(), ids.end());
std::deque<int> A = {ids[0]}, B;
for (int j = 1; j < N; ++j)
{
int i = ids[j];
if (A.size() < B.size())
A.swap(B);
if (are_connected({A.back()}, {i}))
{
A.push_back(i);
}
else if (B.empty() || are_connected({B.back()}, {i}))
{
B.push_back(i);
}
else
{
while (!B.empty())
{
A.push_back(B.back());
B.pop_back();
}
B = {i};
}
}
if (A.size() > B.size())
A.swap(B);
std::function<std::vector<int>(std::deque<int>, std::deque<int>)> solve;
solve = [&](std::deque<int> A, std::deque<int> B) -> std::vector<int>
{
if (A.size() > B.size())
A.swap(B);
if (B.size() == N)
return std::vector<int>(B.begin(), B.end());
bool x = are_connected({A.back()}, {B.front()});
bool y = are_connected({A.back()}, {B.back()});
if (x)
{
while (!A.empty())
{
B.push_front(A.back());
A.pop_back();
}
return std::vector<int>(B.begin(), B.end());
}
else if (y)
{
while (!A.empty())
{
B.push_back(A.back());
A.pop_back();
}
return std::vector<int>(B.begin(), B.end());
}
else
{
if (are_connected(std::vector<int>(A.begin(), A.end()), std::vector<int>(B.begin(), B.end())))
{
int L = 0, R = A.size() - 1;
auto BB = std::vector<int>(B.begin(), B.end());
while (L < R)
{
int mid = (L + R) / 2;
if (are_connected(std::vector<int>(A.begin(), A.begin() + mid + 1), BB))
{
R = mid;
}
else
{
L = mid + 1;
}
}
int firstA = L;
L = 0;
R = B.size() - 1;
auto AA = std::vector<int>(A.begin(), A.begin() + firstA + 1);
while (L < R)
{
int mid = (L + R) / 2;
if (are_connected(AA, std::vector<int>(B.begin(), B.begin() + mid + 1)))
{
R = mid;
}
else
{
L = mid + 1;
}
}
int firstB = L;
while (firstB > 0)
{
B.push_back(B.front());
B.pop_front();
firstB--;
}
std::deque<int> lst;
if (firstA >= (int)A.size() / 2)
{
while (firstA >= 0)
{
lst.push_back(A.front());
A.pop_front();
firstA--;
}
}
else
{
while (firstA < (int)A.size())
{
lst.push_front(A.back());
A.pop_back();
}
reverse(lst.begin(), lst.end());
}
while (!lst.empty())
{
B.push_front(lst.back());
lst.pop_back();
}
return solve(A, B);
}
else
{
return std::vector<int>(B.begin(), B.end());
}
}
};
return solve(A, B);
}
Compilation message
longesttrip.cpp: In lambda function:
longesttrip.cpp:49:22: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
49 | if (B.size() == N)
| ~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
3 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
256 KB |
Output is correct |
2 |
Correct |
7 ms |
208 KB |
Output is correct |
3 |
Correct |
9 ms |
336 KB |
Output is correct |
4 |
Correct |
7 ms |
208 KB |
Output is correct |
5 |
Correct |
7 ms |
300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
236 KB |
Output is correct |
2 |
Correct |
7 ms |
256 KB |
Output is correct |
3 |
Correct |
8 ms |
208 KB |
Output is correct |
4 |
Correct |
9 ms |
208 KB |
Output is correct |
5 |
Correct |
8 ms |
208 KB |
Output is correct |
6 |
Correct |
14 ms |
208 KB |
Output is correct |
7 |
Correct |
10 ms |
208 KB |
Output is correct |
8 |
Correct |
10 ms |
208 KB |
Output is correct |
9 |
Correct |
6 ms |
256 KB |
Output is correct |
10 |
Correct |
9 ms |
208 KB |
Output is correct |
11 |
Correct |
4 ms |
208 KB |
Output is correct |
12 |
Correct |
9 ms |
208 KB |
Output is correct |
13 |
Correct |
9 ms |
300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
208 KB |
Output is correct |
2 |
Correct |
12 ms |
208 KB |
Output is correct |
3 |
Correct |
7 ms |
300 KB |
Output is correct |
4 |
Correct |
9 ms |
296 KB |
Output is correct |
5 |
Correct |
11 ms |
208 KB |
Output is correct |
6 |
Correct |
11 ms |
208 KB |
Output is correct |
7 |
Correct |
12 ms |
208 KB |
Output is correct |
8 |
Correct |
7 ms |
208 KB |
Output is correct |
9 |
Correct |
8 ms |
220 KB |
Output is correct |
10 |
Correct |
6 ms |
208 KB |
Output is correct |
11 |
Correct |
7 ms |
208 KB |
Output is correct |
12 |
Correct |
7 ms |
208 KB |
Output is correct |
13 |
Correct |
6 ms |
208 KB |
Output is correct |
14 |
Correct |
14 ms |
208 KB |
Output is correct |
15 |
Correct |
9 ms |
256 KB |
Output is correct |
16 |
Correct |
17 ms |
296 KB |
Output is correct |
17 |
Correct |
9 ms |
208 KB |
Output is correct |
18 |
Correct |
7 ms |
300 KB |
Output is correct |
19 |
Correct |
10 ms |
208 KB |
Output is correct |
20 |
Correct |
10 ms |
208 KB |
Output is correct |
21 |
Correct |
9 ms |
208 KB |
Output is correct |
22 |
Correct |
11 ms |
208 KB |
Output is correct |
23 |
Correct |
11 ms |
312 KB |
Output is correct |
24 |
Correct |
11 ms |
300 KB |
Output is correct |
25 |
Correct |
8 ms |
208 KB |
Output is correct |
26 |
Correct |
18 ms |
208 KB |
Output is correct |
27 |
Correct |
10 ms |
208 KB |
Output is correct |
28 |
Correct |
12 ms |
208 KB |
Output is correct |
29 |
Correct |
6 ms |
208 KB |
Output is correct |
30 |
Correct |
10 ms |
208 KB |
Output is correct |
31 |
Correct |
12 ms |
256 KB |
Output is correct |
32 |
Correct |
14 ms |
296 KB |
Output is correct |
33 |
Correct |
9 ms |
208 KB |
Output is correct |
34 |
Correct |
11 ms |
208 KB |
Output is correct |
35 |
Correct |
7 ms |
208 KB |
Output is correct |
36 |
Correct |
14 ms |
292 KB |
Output is correct |
37 |
Correct |
6 ms |
308 KB |
Output is correct |
38 |
Correct |
10 ms |
208 KB |
Output is correct |
39 |
Correct |
13 ms |
208 KB |
Output is correct |
40 |
Correct |
7 ms |
308 KB |
Output is correct |
41 |
Correct |
9 ms |
288 KB |
Output is correct |
42 |
Correct |
12 ms |
292 KB |
Output is correct |
43 |
Correct |
14 ms |
248 KB |
Output is correct |
44 |
Correct |
8 ms |
288 KB |
Output is correct |
45 |
Correct |
10 ms |
208 KB |
Output is correct |
46 |
Correct |
9 ms |
208 KB |
Output is correct |
47 |
Correct |
13 ms |
228 KB |
Output is correct |
48 |
Correct |
14 ms |
292 KB |
Output is correct |
49 |
Correct |
12 ms |
208 KB |
Output is correct |
50 |
Correct |
13 ms |
208 KB |
Output is correct |
51 |
Correct |
11 ms |
208 KB |
Output is correct |
52 |
Correct |
17 ms |
296 KB |
Output is correct |
53 |
Correct |
7 ms |
208 KB |
Output is correct |
54 |
Correct |
10 ms |
256 KB |
Output is correct |
55 |
Correct |
9 ms |
296 KB |
Output is correct |
56 |
Correct |
7 ms |
300 KB |
Output is correct |
57 |
Correct |
11 ms |
348 KB |
Output is correct |
58 |
Correct |
12 ms |
208 KB |
Output is correct |
59 |
Correct |
13 ms |
300 KB |
Output is correct |
60 |
Correct |
12 ms |
304 KB |
Output is correct |
61 |
Correct |
14 ms |
304 KB |
Output is correct |
62 |
Correct |
17 ms |
208 KB |
Output is correct |
63 |
Correct |
14 ms |
208 KB |
Output is correct |
64 |
Correct |
15 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
208 KB |
Output is correct |
2 |
Correct |
10 ms |
208 KB |
Output is correct |
3 |
Correct |
8 ms |
208 KB |
Output is correct |
4 |
Correct |
4 ms |
208 KB |
Output is correct |
5 |
Correct |
6 ms |
304 KB |
Output is correct |
6 |
Correct |
10 ms |
208 KB |
Output is correct |
7 |
Correct |
9 ms |
208 KB |
Output is correct |
8 |
Correct |
5 ms |
208 KB |
Output is correct |
9 |
Correct |
8 ms |
208 KB |
Output is correct |
10 |
Correct |
8 ms |
304 KB |
Output is correct |
11 |
Correct |
9 ms |
304 KB |
Output is correct |
12 |
Correct |
6 ms |
208 KB |
Output is correct |
13 |
Correct |
9 ms |
208 KB |
Output is correct |
14 |
Correct |
13 ms |
208 KB |
Output is correct |
15 |
Correct |
17 ms |
208 KB |
Output is correct |
16 |
Correct |
7 ms |
304 KB |
Output is correct |
17 |
Correct |
10 ms |
208 KB |
Output is correct |
18 |
Correct |
15 ms |
208 KB |
Output is correct |
19 |
Correct |
9 ms |
296 KB |
Output is correct |
20 |
Correct |
8 ms |
256 KB |
Output is correct |
21 |
Correct |
10 ms |
208 KB |
Output is correct |
22 |
Correct |
11 ms |
208 KB |
Output is correct |
23 |
Correct |
9 ms |
208 KB |
Output is correct |
24 |
Correct |
9 ms |
208 KB |
Output is correct |
25 |
Correct |
13 ms |
208 KB |
Output is correct |
26 |
Correct |
5 ms |
336 KB |
Output is correct |
27 |
Correct |
13 ms |
208 KB |
Output is correct |
28 |
Correct |
14 ms |
208 KB |
Output is correct |
29 |
Correct |
10 ms |
208 KB |
Output is correct |
30 |
Correct |
7 ms |
300 KB |
Output is correct |
31 |
Correct |
12 ms |
300 KB |
Output is correct |
32 |
Correct |
13 ms |
336 KB |
Output is correct |
33 |
Correct |
14 ms |
208 KB |
Output is correct |
34 |
Correct |
12 ms |
208 KB |
Output is correct |
35 |
Correct |
10 ms |
208 KB |
Output is correct |
36 |
Correct |
14 ms |
300 KB |
Output is correct |
37 |
Correct |
9 ms |
208 KB |
Output is correct |
38 |
Correct |
13 ms |
356 KB |
Output is correct |
39 |
Correct |
8 ms |
296 KB |
Output is correct |
40 |
Correct |
11 ms |
300 KB |
Output is correct |
41 |
Correct |
5 ms |
296 KB |
Output is correct |
42 |
Correct |
11 ms |
208 KB |
Output is correct |
43 |
Correct |
9 ms |
292 KB |
Output is correct |
44 |
Correct |
10 ms |
208 KB |
Output is correct |
45 |
Correct |
9 ms |
400 KB |
Output is correct |
46 |
Correct |
7 ms |
256 KB |
Output is correct |
47 |
Correct |
12 ms |
208 KB |
Output is correct |
48 |
Correct |
8 ms |
300 KB |
Output is correct |
49 |
Correct |
12 ms |
208 KB |
Output is correct |
50 |
Correct |
10 ms |
308 KB |
Output is correct |
51 |
Correct |
13 ms |
344 KB |
Output is correct |
52 |
Correct |
14 ms |
308 KB |
Output is correct |
53 |
Partially correct |
14 ms |
300 KB |
Output is partially correct |
54 |
Correct |
13 ms |
208 KB |
Output is correct |
55 |
Correct |
15 ms |
304 KB |
Output is correct |
56 |
Correct |
9 ms |
208 KB |
Output is correct |
57 |
Correct |
9 ms |
296 KB |
Output is correct |
58 |
Correct |
13 ms |
304 KB |
Output is correct |
59 |
Correct |
14 ms |
292 KB |
Output is correct |
60 |
Correct |
13 ms |
208 KB |
Output is correct |
61 |
Correct |
8 ms |
296 KB |
Output is correct |
62 |
Correct |
9 ms |
208 KB |
Output is correct |
63 |
Correct |
10 ms |
300 KB |
Output is correct |
64 |
Correct |
11 ms |
316 KB |
Output is correct |
65 |
Correct |
10 ms |
304 KB |
Output is correct |
66 |
Correct |
19 ms |
300 KB |
Output is correct |
67 |
Partially correct |
10 ms |
208 KB |
Output is partially correct |
68 |
Partially correct |
11 ms |
312 KB |
Output is partially correct |
69 |
Partially correct |
17 ms |
308 KB |
Output is partially correct |
70 |
Partially correct |
19 ms |
300 KB |
Output is partially correct |
71 |
Correct |
11 ms |
304 KB |
Output is correct |
72 |
Correct |
8 ms |
360 KB |
Output is correct |
73 |
Correct |
10 ms |
208 KB |
Output is correct |
74 |
Correct |
11 ms |
312 KB |
Output is correct |
75 |
Partially correct |
11 ms |
208 KB |
Output is partially correct |
76 |
Partially correct |
11 ms |
208 KB |
Output is partially correct |
77 |
Correct |
8 ms |
316 KB |
Output is correct |
78 |
Correct |
11 ms |
284 KB |
Output is correct |
79 |
Correct |
15 ms |
300 KB |
Output is correct |
80 |
Correct |
14 ms |
360 KB |
Output is correct |
81 |
Partially correct |
15 ms |
304 KB |
Output is partially correct |
82 |
Partially correct |
11 ms |
300 KB |
Output is partially correct |