#include "longesttrip.h"
#include <algorithm>
#include <random>
#include <stack>
#include <iostream>
using namespace std;
vector<int> longest_trip(int N, int D)
{
vector<int> big, small, nodes;
for (int i = 0; i < N; i++)
nodes.push_back(i);
random_device rd;
mt19937 g(rd());
shuffle(nodes.begin(), nodes.end(), g);
// for (int node : nodes) cout << node << ' ';
// cout << '\n';
// cout.flush();
big.push_back(nodes[0]);
small.push_back(nodes[1]);
bool guarantee = false;
for (int i = 2; i < N; i++) {
if (are_connected(vector<int>{big.back()}, vector<int>{nodes[i]})) {
big.push_back(nodes[i]);
guarantee = false;
} else if (guarantee) {
small.push_back(nodes[i]);
} else if (are_connected(vector<int>{small.back()}, vector<int>{nodes[i]})) {
small.push_back(nodes[i]);
guarantee = true;
} else {
if (small.size() == 1) guarantee = true;
while (!small.empty()) {
big.push_back(small.back());
small.pop_back();
}
small.push_back(nodes[i]);
}
}
// cout << "Big: ";
// for (int i : big) cout << i << ' ';
// cout << '\n';
// cout << "Small: ";
// for (int i : small) cout << i << ' ';
// cout << '\n';
// cout.flush();
// return nodes;
if (are_connected(big, small)) {
if (are_connected(vector<int>{big.back()}, vector<int>{small.back()})) {
big.insert(big.end(), small.rbegin(), small.rend());
return big;
}
if (are_connected(vector<int>{big[0]}, vector<int>{small[0]})) {
reverse(small.begin(), small.end());
small.insert(small.end(), big.begin(), big.end());
return small;
}
if (are_connected(vector<int>{big.back()}, vector<int>{small[0]})) {
big.insert(big.end(), small.begin(), small.end());
return big;
}
if (are_connected(vector<int>{big[0]}, vector<int>{small.back()})) {
small.insert(small.end(), big.begin(), big.end());
return small;
}
int l = 0, r = big.size() - 1;
while (l < r) {
int mid = (l + r) / 2;
std::vector<int>::iterator begin = big.begin() + l; // Start from index 1
std::vector<int>::iterator end = big.begin() + mid + 1;
vector<int> tempBig(begin, end);
if (are_connected(small, tempBig)) {
r = mid;
} else {
l = mid + 1;
}
}
int bigInd = l;
l = 0, r = small.size() - 1;
while (l < r) {
int mid = (l + r) / 2;
std::vector<int>::iterator begin = small.begin() + l; // Start from index 1
std::vector<int>::iterator end = small.begin() + mid + 1;
vector<int> tempSmall(begin, end);
if (are_connected(tempSmall, vector<int>{big[bigInd]})) {
r = mid;
} else {
l = mid + 1;
}
}
int smallInd = l;
// cout << "Indices: " << bigInd << ' ' << smallInd << '\n';
// cout.flush();
vector<int> ans;
for (int i = smallInd + 1; i < small.size(); i++) {
ans.push_back(small[i]);
}
for (int i = 0; i <= smallInd; i++) {
ans.push_back(small[i]);
}
for (int i = bigInd; i < big.size(); i++) {
ans.push_back(big[i]);
}
for (int i = 0; i < bigInd; i++) {
ans.push_back(big[i]);
}
return ans;
} else {
if (big.size() < small.size()) return small;
else return big;
}
}
Compilation message
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:111:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for (int i = smallInd + 1; i < small.size(); i++) {
| ~~^~~~~~~~~~~~~~
longesttrip.cpp:117:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for (int i = bigInd; i < big.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
340 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 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 |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
12 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
600 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
4 ms |
344 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 |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
12 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 |
4 ms |
608 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
344 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 |
8 ms |
504 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
344 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
5 ms |
344 KB |
Output is correct |
21 |
Correct |
4 ms |
448 KB |
Output is correct |
22 |
Correct |
6 ms |
600 KB |
Output is correct |
23 |
Correct |
5 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 |
12 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
7 ms |
344 KB |
Output is correct |
29 |
Correct |
9 ms |
344 KB |
Output is correct |
30 |
Correct |
4 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
344 KB |
Output is correct |
32 |
Correct |
5 ms |
344 KB |
Output is correct |
33 |
Correct |
5 ms |
344 KB |
Output is correct |
34 |
Correct |
4 ms |
344 KB |
Output is correct |
35 |
Correct |
5 ms |
612 KB |
Output is correct |
36 |
Correct |
5 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
344 KB |
Output is correct |
38 |
Correct |
5 ms |
600 KB |
Output is correct |
39 |
Correct |
6 ms |
344 KB |
Output is correct |
40 |
Correct |
5 ms |
340 KB |
Output is correct |
41 |
Correct |
7 ms |
340 KB |
Output is correct |
42 |
Correct |
5 ms |
344 KB |
Output is correct |
43 |
Correct |
5 ms |
344 KB |
Output is correct |
44 |
Correct |
5 ms |
344 KB |
Output is correct |
45 |
Correct |
10 ms |
344 KB |
Output is correct |
46 |
Correct |
15 ms |
344 KB |
Output is correct |
47 |
Correct |
8 ms |
344 KB |
Output is correct |
48 |
Correct |
11 ms |
540 KB |
Output is correct |
49 |
Correct |
8 ms |
344 KB |
Output is correct |
50 |
Correct |
5 ms |
344 KB |
Output is correct |
51 |
Correct |
6 ms |
444 KB |
Output is correct |
52 |
Correct |
7 ms |
344 KB |
Output is correct |
53 |
Correct |
5 ms |
344 KB |
Output is correct |
54 |
Correct |
5 ms |
344 KB |
Output is correct |
55 |
Correct |
5 ms |
344 KB |
Output is correct |
56 |
Correct |
6 ms |
600 KB |
Output is correct |
57 |
Correct |
5 ms |
444 KB |
Output is correct |
58 |
Correct |
7 ms |
444 KB |
Output is correct |
59 |
Correct |
7 ms |
852 KB |
Output is correct |
60 |
Correct |
7 ms |
600 KB |
Output is correct |
61 |
Correct |
6 ms |
344 KB |
Output is correct |
62 |
Correct |
6 ms |
608 KB |
Output is correct |
63 |
Correct |
6 ms |
448 KB |
Output is correct |
64 |
Correct |
6 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
596 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 |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
11 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 |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
5 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 |
9 ms |
344 KB |
Output is correct |
15 |
Correct |
9 ms |
344 KB |
Output is correct |
16 |
Correct |
10 ms |
344 KB |
Output is correct |
17 |
Correct |
6 ms |
344 KB |
Output is correct |
18 |
Correct |
6 ms |
600 KB |
Output is correct |
19 |
Correct |
5 ms |
344 KB |
Output is correct |
20 |
Correct |
5 ms |
344 KB |
Output is correct |
21 |
Correct |
9 ms |
344 KB |
Output is correct |
22 |
Correct |
9 ms |
344 KB |
Output is correct |
23 |
Correct |
8 ms |
344 KB |
Output is correct |
24 |
Correct |
6 ms |
344 KB |
Output is correct |
25 |
Correct |
8 ms |
344 KB |
Output is correct |
26 |
Correct |
5 ms |
344 KB |
Output is correct |
27 |
Correct |
5 ms |
344 KB |
Output is correct |
28 |
Correct |
6 ms |
600 KB |
Output is correct |
29 |
Correct |
5 ms |
600 KB |
Output is correct |
30 |
Correct |
6 ms |
600 KB |
Output is correct |
31 |
Correct |
5 ms |
600 KB |
Output is correct |
32 |
Correct |
12 ms |
344 KB |
Output is correct |
33 |
Correct |
11 ms |
344 KB |
Output is correct |
34 |
Correct |
11 ms |
540 KB |
Output is correct |
35 |
Correct |
7 ms |
344 KB |
Output is correct |
36 |
Correct |
8 ms |
344 KB |
Output is correct |
37 |
Correct |
5 ms |
344 KB |
Output is correct |
38 |
Correct |
7 ms |
344 KB |
Output is correct |
39 |
Correct |
7 ms |
344 KB |
Output is correct |
40 |
Correct |
5 ms |
616 KB |
Output is correct |
41 |
Correct |
6 ms |
344 KB |
Output is correct |
42 |
Correct |
5 ms |
600 KB |
Output is correct |
43 |
Correct |
5 ms |
344 KB |
Output is correct |
44 |
Correct |
6 ms |
448 KB |
Output is correct |
45 |
Correct |
5 ms |
444 KB |
Output is correct |
46 |
Correct |
5 ms |
456 KB |
Output is correct |
47 |
Correct |
4 ms |
344 KB |
Output is correct |
48 |
Correct |
5 ms |
344 KB |
Output is correct |
49 |
Correct |
5 ms |
344 KB |
Output is correct |
50 |
Correct |
6 ms |
600 KB |
Output is correct |
51 |
Correct |
5 ms |
344 KB |
Output is correct |
52 |
Correct |
5 ms |
344 KB |
Output is correct |
53 |
Correct |
6 ms |
600 KB |
Output is correct |
54 |
Correct |
5 ms |
344 KB |
Output is correct |
55 |
Correct |
5 ms |
344 KB |
Output is correct |
56 |
Correct |
5 ms |
600 KB |
Output is correct |
57 |
Correct |
5 ms |
344 KB |
Output is correct |
58 |
Correct |
5 ms |
344 KB |
Output is correct |
59 |
Correct |
6 ms |
344 KB |
Output is correct |
60 |
Correct |
5 ms |
344 KB |
Output is correct |
61 |
Correct |
6 ms |
344 KB |
Output is correct |
62 |
Correct |
4 ms |
600 KB |
Output is correct |
63 |
Correct |
6 ms |
444 KB |
Output is correct |
64 |
Correct |
5 ms |
448 KB |
Output is correct |
65 |
Correct |
6 ms |
600 KB |
Output is correct |
66 |
Correct |
7 ms |
600 KB |
Output is correct |
67 |
Correct |
6 ms |
344 KB |
Output is correct |
68 |
Correct |
7 ms |
344 KB |
Output is correct |
69 |
Correct |
6 ms |
448 KB |
Output is correct |
70 |
Correct |
6 ms |
620 KB |
Output is correct |
71 |
Correct |
5 ms |
452 KB |
Output is correct |
72 |
Correct |
5 ms |
620 KB |
Output is correct |
73 |
Correct |
7 ms |
600 KB |
Output is correct |
74 |
Correct |
7 ms |
600 KB |
Output is correct |
75 |
Correct |
7 ms |
344 KB |
Output is correct |
76 |
Correct |
7 ms |
344 KB |
Output is correct |
77 |
Correct |
5 ms |
600 KB |
Output is correct |
78 |
Correct |
8 ms |
600 KB |
Output is correct |
79 |
Correct |
6 ms |
600 KB |
Output is correct |
80 |
Correct |
7 ms |
444 KB |
Output is correct |
81 |
Correct |
7 ms |
344 KB |
Output is correct |
82 |
Correct |
7 ms |
344 KB |
Output is correct |