#include "longesttrip.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <random>
#include <vector>
typedef long long llong;
const int MAXN = 256 + 10;
const llong INF = 1e18;
const int INTINF = 1e9;
int n;
std::mt19937 rng(420);
std::vector <int> perm;
std::vector <int> convert(const std::vector <int> &v)
{
std::vector <int> res;
for (const int &i : v) res.push_back(perm[i]);
return res;
}
bool areConnected(std::vector <int> a, std::vector <int> b)
{
std::vector <int> A, B;
for (const int &i : a) A.push_back(perm[i]);
for (const int &i : b) B.push_back(perm[i]);
return are_connected(A, B);
}
bool areConnected(int a, int b)
{
std::vector <int> A, B;
A.push_back(a);
B.push_back(b);
return areConnected(A, B);
}
std::vector <int> longest_trip(int N, int D)
{
n = N;
std::vector <int> a, b;
perm.resize(n);
std::iota(perm.begin(), perm.end(), 0);
std::shuffle(perm.begin(), perm.end(), rng);
a.push_back(0);
b.push_back(1);
for (int i = 2 ; i < n ; i += 2)
{
assert(a.size() && b.size());
if (i == n - 1)
{
if (areConnected(a.back(), i))
{
a.push_back(i);
} else if (areConnected(b.back(), i))
{
b.push_back(i);
} else
{
std::reverse(b.begin(), b.end());
for (const int &u : b)
{
a.push_back(u);
}
b.clear();
b.push_back(i);
}
break;
}
if (areConnected(i, i + 1))
{
if (areConnected(a.back(), i))
{
a.push_back(i);
a.push_back(i + 1);
continue;
}
if (areConnected(b.back(), i))
{
b.push_back(i);
b.push_back(i + 1);
continue;
}
std::reverse(b.begin(), b.end());
for (const int &u : b)
{
a.push_back(u);
}
b.clear();
b.push_back(i);
b.push_back(i + 1);
continue;
}
int edgeA = i;
int edgeB = i;
if (!areConnected(a.back(), i))
{
edgeA = i + 1;
}
if (!areConnected(b.back(), i))
{
edgeB = i + 1;
}
if (edgeA != edgeB)
{
a.push_back(edgeA);
b.push_back(edgeB);
continue;
}
a.push_back(edgeA);
std::reverse(b.begin(), b.end());
for (const int &u : b)
{
a.push_back(u);
}
b.clear();
b.push_back(edgeB ^ 1);
}
if (a.size() < b.size())
{
std::swap(a, b);
}
if (b.empty())
{
return convert(a);
}
if (!areConnected(a, b))
{
return convert(a);
}
if (areConnected({a[0]}, {b[0]}))
{
std::reverse(a.begin(), a.end());
for (int i = 0 ; i < b.size() ; ++i)
{
a.push_back(b[i]);
}
return convert(a);
}
if (b.size() > 1 && areConnected({a[0]}, {b.back()}))
{
std::reverse(b.begin(), b.end());
std::reverse(a.begin(), a.end());
for (int i = 0 ; i < b.size() ; ++i)
{
a.push_back(b[i]);
}
return convert(a);
}
if (areConnected({b[0]}, {a.back()}))
{
for (int i = 0 ; i < b.size() ; ++i)
{
a.push_back(b[i]);
}
return convert(a);
}
int aPos, bPos;
int l = -1, r = a.size() - 1, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
std::vector <int> prefix(a.begin(), a.begin() + mid + 1);
if (!areConnected(prefix, b)) l = mid;
else r = mid;
}
aPos = r;
std::vector <int> prefixA(a.begin(), a.begin() + aPos + 1);
l = -1; r = b.size() - 1;
while (l < r - 1)
{
mid = (l + r) / 2;
std::vector <int> prefixB(b.begin(), b.begin() + mid + 1);
if (!areConnected(prefixA, prefixB)) l = mid;
else r = mid;
}
bPos = r;
for (int i = 0 ; i < aPos ; ++i)
{
a.push_back(a[0]);
a.erase(a.begin());
}
for (int i = 0 ; i < bPos ; ++i)
{
b.push_back(b[0]);
b.erase(b.begin());
}
std::reverse(a.begin(), a.end());
for (int i = 0 ; i < b.size() ; ++i)
{
a.push_back(b[i]);
}
return convert(a);
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:153:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
153 | for (int i = 0 ; i < b.size() ; ++i)
| ~~^~~~~~~~~~
longesttrip.cpp:165:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
165 | for (int i = 0 ; i < b.size() ; ++i)
| ~~^~~~~~~~~~
longesttrip.cpp:175:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
175 | for (int i = 0 ; i < b.size() ; ++i)
| ~~^~~~~~~~~~
longesttrip.cpp:218:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
218 | for (int i = 0 ; i < b.size() ; ++i)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
756 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
6 ms |
852 KB |
Output is correct |
6 |
Correct |
10 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
504 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
6 ms |
600 KB |
Output is correct |
12 |
Correct |
6 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 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 |
6 ms |
600 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
8 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
600 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
600 KB |
Output is correct |
12 |
Correct |
5 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
14 |
Correct |
7 ms |
344 KB |
Output is correct |
15 |
Correct |
7 ms |
344 KB |
Output is correct |
16 |
Correct |
8 ms |
500 KB |
Output is correct |
17 |
Correct |
7 ms |
440 KB |
Output is correct |
18 |
Correct |
6 ms |
600 KB |
Output is correct |
19 |
Correct |
6 ms |
852 KB |
Output is correct |
20 |
Correct |
5 ms |
344 KB |
Output is correct |
21 |
Correct |
5 ms |
344 KB |
Output is correct |
22 |
Correct |
7 ms |
440 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
5 ms |
600 KB |
Output is correct |
25 |
Correct |
6 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 |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
344 KB |
Output is correct |
30 |
Correct |
5 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
344 KB |
Output is correct |
32 |
Correct |
6 ms |
344 KB |
Output is correct |
33 |
Correct |
5 ms |
344 KB |
Output is correct |
34 |
Correct |
5 ms |
600 KB |
Output is correct |
35 |
Correct |
6 ms |
344 KB |
Output is correct |
36 |
Correct |
5 ms |
600 KB |
Output is correct |
37 |
Correct |
6 ms |
344 KB |
Output is correct |
38 |
Correct |
6 ms |
344 KB |
Output is correct |
39 |
Correct |
7 ms |
600 KB |
Output is correct |
40 |
Correct |
8 ms |
600 KB |
Output is correct |
41 |
Correct |
7 ms |
344 KB |
Output is correct |
42 |
Correct |
6 ms |
600 KB |
Output is correct |
43 |
Correct |
6 ms |
344 KB |
Output is correct |
44 |
Correct |
6 ms |
600 KB |
Output is correct |
45 |
Correct |
7 ms |
344 KB |
Output is correct |
46 |
Correct |
8 ms |
344 KB |
Output is correct |
47 |
Correct |
8 ms |
344 KB |
Output is correct |
48 |
Correct |
7 ms |
344 KB |
Output is correct |
49 |
Correct |
8 ms |
344 KB |
Output is correct |
50 |
Correct |
6 ms |
692 KB |
Output is correct |
51 |
Correct |
7 ms |
436 KB |
Output is correct |
52 |
Correct |
10 ms |
692 KB |
Output is correct |
53 |
Correct |
5 ms |
452 KB |
Output is correct |
54 |
Correct |
5 ms |
452 KB |
Output is correct |
55 |
Correct |
7 ms |
344 KB |
Output is correct |
56 |
Correct |
5 ms |
876 KB |
Output is correct |
57 |
Correct |
6 ms |
708 KB |
Output is correct |
58 |
Correct |
6 ms |
628 KB |
Output is correct |
59 |
Correct |
6 ms |
860 KB |
Output is correct |
60 |
Correct |
7 ms |
972 KB |
Output is correct |
61 |
Correct |
7 ms |
1112 KB |
Output is correct |
62 |
Correct |
7 ms |
704 KB |
Output is correct |
63 |
Correct |
8 ms |
708 KB |
Output is correct |
64 |
Correct |
7 ms |
868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 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 |
6 ms |
344 KB |
Output is correct |
6 |
Correct |
10 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
600 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Correct |
6 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
600 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
5 ms |
344 KB |
Output is correct |
20 |
Correct |
5 ms |
600 KB |
Output is correct |
21 |
Correct |
6 ms |
344 KB |
Output is correct |
22 |
Correct |
6 ms |
596 KB |
Output is correct |
23 |
Correct |
6 ms |
344 KB |
Output is correct |
24 |
Correct |
5 ms |
344 KB |
Output is correct |
25 |
Correct |
7 ms |
504 KB |
Output is correct |
26 |
Correct |
5 ms |
596 KB |
Output is correct |
27 |
Correct |
6 ms |
440 KB |
Output is correct |
28 |
Correct |
6 ms |
344 KB |
Output is correct |
29 |
Correct |
5 ms |
344 KB |
Output is correct |
30 |
Correct |
5 ms |
344 KB |
Output is correct |
31 |
Correct |
5 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 |
8 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 |
5 ms |
692 KB |
Output is correct |
38 |
Correct |
7 ms |
440 KB |
Output is correct |
39 |
Correct |
7 ms |
692 KB |
Output is correct |
40 |
Correct |
5 ms |
604 KB |
Output is correct |
41 |
Correct |
6 ms |
604 KB |
Output is correct |
42 |
Correct |
6 ms |
344 KB |
Output is correct |
43 |
Correct |
5 ms |
600 KB |
Output is correct |
44 |
Correct |
6 ms |
344 KB |
Output is correct |
45 |
Correct |
5 ms |
344 KB |
Output is correct |
46 |
Correct |
5 ms |
344 KB |
Output is correct |
47 |
Correct |
5 ms |
600 KB |
Output is correct |
48 |
Correct |
5 ms |
600 KB |
Output is correct |
49 |
Correct |
5 ms |
600 KB |
Output is correct |
50 |
Correct |
7 ms |
344 KB |
Output is correct |
51 |
Correct |
6 ms |
600 KB |
Output is correct |
52 |
Correct |
6 ms |
600 KB |
Output is correct |
53 |
Correct |
6 ms |
344 KB |
Output is correct |
54 |
Correct |
6 ms |
344 KB |
Output is correct |
55 |
Correct |
7 ms |
596 KB |
Output is correct |
56 |
Correct |
4 ms |
344 KB |
Output is correct |
57 |
Correct |
5 ms |
344 KB |
Output is correct |
58 |
Correct |
6 ms |
600 KB |
Output is correct |
59 |
Correct |
6 ms |
344 KB |
Output is correct |
60 |
Correct |
7 ms |
600 KB |
Output is correct |
61 |
Correct |
6 ms |
600 KB |
Output is correct |
62 |
Correct |
5 ms |
1128 KB |
Output is correct |
63 |
Correct |
5 ms |
708 KB |
Output is correct |
64 |
Correct |
6 ms |
716 KB |
Output is correct |
65 |
Correct |
7 ms |
700 KB |
Output is correct |
66 |
Correct |
8 ms |
868 KB |
Output is correct |
67 |
Correct |
7 ms |
864 KB |
Output is correct |
68 |
Correct |
7 ms |
604 KB |
Output is correct |
69 |
Correct |
7 ms |
704 KB |
Output is correct |
70 |
Correct |
8 ms |
616 KB |
Output is correct |
71 |
Correct |
6 ms |
464 KB |
Output is correct |
72 |
Correct |
6 ms |
624 KB |
Output is correct |
73 |
Correct |
7 ms |
708 KB |
Output is correct |
74 |
Correct |
6 ms |
964 KB |
Output is correct |
75 |
Correct |
7 ms |
612 KB |
Output is correct |
76 |
Correct |
8 ms |
712 KB |
Output is correct |
77 |
Correct |
6 ms |
876 KB |
Output is correct |
78 |
Correct |
7 ms |
704 KB |
Output is correct |
79 |
Correct |
6 ms |
616 KB |
Output is correct |
80 |
Correct |
7 ms |
964 KB |
Output is correct |
81 |
Correct |
9 ms |
860 KB |
Output is correct |
82 |
Correct |
7 ms |
604 KB |
Output is correct |