#include "longesttrip.h"
#include <iostream>
#include <vector>
using namespace std;
//bool are_connected(vector<int> A, vector<int> B) { return true; }
void sub1(vector<int>& ans, int n) {
ans.resize(n);
for (int i = 0; i < n; i++) { ans[i] = i; }
}
void sub2(vector<int>& ans, int n) {
ans.reserve(n);
bool are_last_connected = are_connected({ n - 2 }, { n - 1 });
if (!are_last_connected) { ans.push_back(n - 1); }
ans.push_back(0);
for (int i = 0; i < n - 1; i++) {
if (are_connected({ i }, { i + 1 })) {
ans.push_back(i + 1);
}
else {
if (i == n - 2) { break; }
ans.push_back(i + 2);
ans.push_back(i + 1);
if (!(i == n - 3 || !are_last_connected && i == n - 4))ans.push_back(i + 3);
i += 2;
}
}
}
void sub3(vector<int>& ans, int n)
{
ans.reserve(n);
vector<int> path1;
vector<int> path2;
path1.reserve(n);
path2.reserve(n);
path1.push_back(0);
path2.push_back(1);
bool connectTo1 = true, connectTo2 = true, connectBetween = true;
for (int i = 2; i < n; i++)
{
int a = i;
bool query = are_connected({ a }, { path1.back() });
if (query)
{
path1.push_back(a);
connectBetween = true;
}
else if (!connectBetween)
{
path2.push_back(a);
connectBetween = false;
}
else
{
query = are_connected({ a }, { path2.back() });
if (query)
{
path2.push_back(a);
connectBetween = false;
}
else
{
if (path2.size() == 1)
{
connectBetween = false;
}
else
{
connectBetween = true;
}
for (int j = path2.size() - 1; j >= 0; j--)
{
path1.push_back(path2[j]);
}
path2.clear();
path2.push_back(a);
}
}
}
vector<int> shortPath, longPath;
if (path1.size() > path2.size())
{
longPath = path1;
shortPath = path2;
}
else
{
longPath = path2;
shortPath = path1;
}
ans = longPath; return;
bool endsConnected;
if (shortPath.size() == 1)
{
endsConnected = are_connected({ longPath[0], longPath.back() }, { shortPath[0] });
}
else
{
endsConnected = are_connected({ longPath[0], longPath.back() }, { shortPath[0], shortPath.back() });
}
if (endsConnected)
{
if (shortPath.size() == 1)
{
if (are_connected({ longPath[0] }, { shortPath[0] }))
{
ans.push_back(shortPath[0]);
for (int k = 0; k < longPath.size(); k++)
{
ans.push_back(longPath[k]);
}
return;
}
else
{
longPath.push_back(shortPath[0]);
ans = longPath;
return;
}
}
else
{
if (are_connected({ longPath[0], longPath.back() }, { shortPath[0] }))
{
if (are_connected({ longPath[0] }, { shortPath[0] }))
{
for (int k = longPath.size() - 1; k >= 0; k--)
{
ans.push_back(longPath[k]);
}
for (int k = 0; k < shortPath.size(); k++)
{
ans.push_back(shortPath[k]);
}
return;
}
else
{
for (int k = longPath.size() - 1; k >= 0; k--)
{
ans.push_back(longPath[k]);
}
for (int k = shortPath.size() - 1; k >= 0; k--)
{
ans.push_back(shortPath[k]);
}
return;
}
}
else if (are_connected({ longPath.back(), longPath.back() }, { shortPath[0] }))
{
for (int k = 0; k < longPath.size(); k++)
{
ans.push_back(longPath[k]);
}
for (int k = 0; k < shortPath.size(); k++)
{
ans.push_back(shortPath[k]);
}
return;
}
else
{
for (int k = 0; k < longPath.size(); k++)
{
ans.push_back(longPath[k]);
}
for (int k = shortPath.size() - 1; k >= 0; k--)
{
ans.push_back(shortPath[k]);
}
return;
}
}
}
else
{
bool areConnected = are_connected(longPath, shortPath);
if (areConnected)
{
int l = 0, r = longPath.size() - 1;
while (l != r)
{
int m = (l + r) >> 1;
vector<int> test_data(m - l + 1);
for (int k = l; k <= m; k++) {
test_data[k - l] = longPath[k];
}
if (are_connected(shortPath, test_data)) {
r = m;
}
else {
l = m + 1;
}
}
int pos = l;
l = 0, r = shortPath.size() - 1;
while (l != r)
{
int m = (l + r) >> 1;
vector<int> test_data(m - l + 1);
for (int k = l; k <= m; k++) {
test_data[k - l] = shortPath[k];
}
if (are_connected({ longPath[pos] }, test_data)) {
r = m;
}
else {
l = m + 1;
}
}
int pos2 = l;
for (int k = (pos2 + 1) % shortPath.size(); k != pos2; k = (k + 1) % shortPath.size()) {
ans.push_back(shortPath[k]);
}
ans.push_back(shortPath[pos2]);
ans.push_back(longPath[pos]);
for (int k = (pos + 1) % longPath.size(); k != pos; k = (k + 1) % longPath.size()) {
ans.push_back(longPath[k]);
}
return;
}
else
{
ans = longPath;
return;
}
}
}
vector<int> longest_trip(int n, int d) {
vector<int> ans;
if (d == 3) {
sub1(ans, n);
return ans;
}
if (d == 2) {
sub2(ans, n);
return ans;
}
if (d == 1)
{
sub3(ans, n);
return ans;
}
return { 0, 4, 5 ,2 };
}
//int main() { return 0; }
Compilation message
longesttrip.cpp: In function 'void sub2(std::vector<int>&, int)':
longesttrip.cpp:26:53: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
26 | if (!(i == n - 3 || !are_last_connected && i == n - 4))ans.push_back(i + 3);
| ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
longesttrip.cpp: In function 'void sub3(std::vector<int>&, int)':
longesttrip.cpp:112:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for (int k = 0; k < longPath.size(); k++)
| ~~^~~~~~~~~~~~~~~~~
longesttrip.cpp:135:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for (int k = 0; k < shortPath.size(); k++)
| ~~^~~~~~~~~~~~~~~~~~
longesttrip.cpp:156:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
156 | for (int k = 0; k < longPath.size(); k++)
| ~~^~~~~~~~~~~~~~~~~
longesttrip.cpp:160:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
160 | for (int k = 0; k < shortPath.size(); k++)
| ~~^~~~~~~~~~~~~~~~~~
longesttrip.cpp:168:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
168 | for (int k = 0; k < longPath.size(); k++)
| ~~^~~~~~~~~~~~~~~~~
longesttrip.cpp:41:10: warning: unused variable 'connectTo1' [-Wunused-variable]
41 | bool connectTo1 = true, connectTo2 = true, connectBetween = true;
| ^~~~~~~~~~
longesttrip.cpp:41:29: warning: unused variable 'connectTo2' [-Wunused-variable]
41 | bool connectTo1 = true, connectTo2 = true, connectBetween = true;
| ^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 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 |
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 |
4 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
6 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
344 KB |
Output is correct |
11 |
Correct |
6 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
344 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
5 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
344 KB |
Output is correct |
6 |
Correct |
4 ms |
344 KB |
Output is correct |
7 |
Correct |
4 ms |
344 KB |
Output is correct |
8 |
Correct |
6 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
344 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
344 KB |
Output is correct |
13 |
Correct |
6 ms |
344 KB |
Output is correct |
14 |
Correct |
3 ms |
344 KB |
Output is correct |
15 |
Correct |
6 ms |
344 KB |
Output is correct |
16 |
Correct |
5 ms |
344 KB |
Output is correct |
17 |
Correct |
7 ms |
344 KB |
Output is correct |
18 |
Correct |
7 ms |
344 KB |
Output is correct |
19 |
Correct |
7 ms |
344 KB |
Output is correct |
20 |
Correct |
5 ms |
344 KB |
Output is correct |
21 |
Correct |
5 ms |
436 KB |
Output is correct |
22 |
Correct |
6 ms |
444 KB |
Output is correct |
23 |
Correct |
7 ms |
444 KB |
Output is correct |
24 |
Correct |
5 ms |
444 KB |
Output is correct |
25 |
Correct |
6 ms |
504 KB |
Output is correct |
26 |
Correct |
4 ms |
344 KB |
Output is correct |
27 |
Correct |
6 ms |
344 KB |
Output is correct |
28 |
Correct |
4 ms |
344 KB |
Output is correct |
29 |
Correct |
4 ms |
344 KB |
Output is correct |
30 |
Correct |
4 ms |
344 KB |
Output is correct |
31 |
Correct |
7 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 |
428 KB |
Output is correct |
35 |
Correct |
5 ms |
344 KB |
Output is correct |
36 |
Correct |
5 ms |
344 KB |
Output is correct |
37 |
Correct |
6 ms |
424 KB |
Output is correct |
38 |
Correct |
4 ms |
344 KB |
Output is correct |
39 |
Correct |
6 ms |
344 KB |
Output is correct |
40 |
Correct |
7 ms |
344 KB |
Output is correct |
41 |
Correct |
8 ms |
344 KB |
Output is correct |
42 |
Correct |
4 ms |
344 KB |
Output is correct |
43 |
Correct |
7 ms |
344 KB |
Output is correct |
44 |
Correct |
4 ms |
344 KB |
Output is correct |
45 |
Correct |
6 ms |
344 KB |
Output is correct |
46 |
Correct |
4 ms |
344 KB |
Output is correct |
47 |
Correct |
4 ms |
344 KB |
Output is correct |
48 |
Correct |
4 ms |
344 KB |
Output is correct |
49 |
Correct |
6 ms |
344 KB |
Output is correct |
50 |
Correct |
4 ms |
344 KB |
Output is correct |
51 |
Correct |
7 ms |
344 KB |
Output is correct |
52 |
Correct |
7 ms |
344 KB |
Output is correct |
53 |
Correct |
4 ms |
344 KB |
Output is correct |
54 |
Correct |
5 ms |
344 KB |
Output is correct |
55 |
Correct |
6 ms |
344 KB |
Output is correct |
56 |
Correct |
6 ms |
344 KB |
Output is correct |
57 |
Correct |
6 ms |
344 KB |
Output is correct |
58 |
Correct |
7 ms |
344 KB |
Output is correct |
59 |
Correct |
8 ms |
344 KB |
Output is correct |
60 |
Correct |
6 ms |
344 KB |
Output is correct |
61 |
Correct |
6 ms |
344 KB |
Output is correct |
62 |
Correct |
6 ms |
344 KB |
Output is correct |
63 |
Correct |
5 ms |
344 KB |
Output is correct |
64 |
Correct |
6 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |