#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()}) ||
are_connected(vector<int>{big[0]}, vector<int>{small[0]}) ) {
big.insert(big.end(), small.rbegin(), small.rend());
return big;
}
if (are_connected(vector<int>{big.back()}, vector<int>{small[0]}) ||
are_connected(vector<int>{big[0]}, vector<int>{small.back()})) {
big.insert(big.end(), small.begin(), small.end());
return big;
}
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;
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:102:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for (int i = smallInd + 1; i < small.size(); i++) {
| ~~^~~~~~~~~~~~~~
longesttrip.cpp:108:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for (int i = bigInd; i <= big.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
344 KB |
Output is correct |
2 |
Correct |
9 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
344 KB |
Output is correct |
2 |
Correct |
8 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 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
600 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
596 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
4 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 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |