#include "longesttrip.h"
#include<bits/stdc++.h>
#define pb push_back
#define all(a) (a).begin(), (a).end()
using namespace std;
vector<int> longest_trip(int n, int D) {
if(D == 3) {
vector<int> p;
for(int i = 0;i < n;i++) p.pb(i);
return p;
}
if(D == 2) {
deque<int> dq;
if(!are_connected({0}, {1}))
dq.pb(0), dq.pb(2), dq.pb(1);
else if(!are_connected({1}, {2}))
dq.pb(1), dq.pb(0), dq.pb(2);
else dq.pb(0), dq.pb(1), dq.pb(2);
for(int i = 3;i < n;i++) {
if(are_connected({i}, {dq.front()}))
dq.push_front(i);
else dq.pb(i);
}
return vector<int>(all(dq));
}
if(D == 1) {
vector<int> p1, p2;
if(n & 1) p1.pb(n - 1);
for(int i = 1;i < n;i += 2) {
int a = i - 1, b = i;
if(p2.size()) {
int c = p1.back(), d = p2.back();
if(are_connected({a}, {b})) {
if(are_connected({a}, {c})) {
p1.pb(a), p1.pb(b);
if(are_connected({b}, {d})) {
while(p2.size())
p1.pb(p2.back()), p2.pop_back();
}
} else {
p2.pb(a), p2.pb(b);
if(are_connected({b}, {c})) {
while(p1.size())
p2.pb(p1.size()), p1.pop_back();
}
}
} else {
if(are_connected({a}, {c})) {
if(are_connected({b}, {d}))
p1.pb(a), p2.pb(b);
else p1.pb(b), p2.pb(a);
} else p1.pb(b), p2.pb(a);
}
} else {
if(are_connected({a}, {b})) {
if(p1.size() == 0)
p1.pb(a), p1.pb(b);
else if(are_connected({a}, {p1.back()})) {
p1.pb(a), p1.pb(b);
} else {
if(are_connected({b}, {p1.back()}))
p1.pb(b), p1.pb(a);
else p2.pb(a), p2.pb(b);
}
} else {
if(p1.size() == 0)
p1.pb(a), p2.pb(b);
else if(are_connected({a}, {p1.back()}))
p1.pb(a), p2.pb(b);
else p1.pb(b), p2.pb(a);
}
}
if(p1.size() < p2.size()) swap(p1, p2);
}
assert(p1.size() >= (n + 1) / 2);
for(int i = 1;i < (int)p1.size();i++)
assert(are_connected({p1[i - 1]}, {p1[i]}));
return p1;
}
return {};
}
Compilation message
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from longesttrip.cpp:2:
longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:83:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
83 | assert(p1.size() >= (n + 1) / 2);
| ~~~~~~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
2 ms |
600 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 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 |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
600 KB |
Output is correct |
6 |
Correct |
7 ms |
344 KB |
Output is correct |
7 |
Correct |
4 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
344 KB |
Output is correct |
9 |
Correct |
4 ms |
344 KB |
Output is correct |
10 |
Correct |
4 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 |
4 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
344 KB |
Output is correct |
3 |
Correct |
10 ms |
344 KB |
Output is correct |
4 |
Correct |
9 ms |
344 KB |
Output is correct |
5 |
Correct |
9 ms |
344 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Correct |
8 ms |
344 KB |
Output is correct |
8 |
Correct |
8 ms |
344 KB |
Output is correct |
9 |
Correct |
10 ms |
344 KB |
Output is correct |
10 |
Correct |
9 ms |
344 KB |
Output is correct |
11 |
Correct |
9 ms |
600 KB |
Output is correct |
12 |
Correct |
8 ms |
344 KB |
Output is correct |
13 |
Correct |
8 ms |
344 KB |
Output is correct |
14 |
Correct |
6 ms |
344 KB |
Output is correct |
15 |
Incorrect |
4 ms |
344 KB |
Incorrect |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
344 KB |
Output is correct |
2 |
Correct |
10 ms |
504 KB |
Output is correct |
3 |
Correct |
8 ms |
344 KB |
Output is correct |
4 |
Correct |
10 ms |
344 KB |
Output is correct |
5 |
Partially correct |
8 ms |
344 KB |
Output is partially correct |
6 |
Incorrect |
0 ms |
344 KB |
Incorrect |
7 |
Halted |
0 ms |
0 KB |
- |