/* upsolved using some inspiration from EmeraldBlock */
#include "longesttrip.h"
#include <vector>
using namespace std;
typedef vector<int> vi;
const int N = 256;
int qu1[N], qu2[N], cnt1, cnt2;
vi longest_trip(int n, int d) {
cnt1 = cnt2 = 0;
for (int i = 0; i < n; i++)
if (cnt1 == 0)
qu1[cnt1++] = i;
else if (cnt2 == 0)
qu2[cnt2++] = i;
else if (are_connected({ qu1[cnt1 - 1] }, { i }))
qu1[cnt1++] = i;
else if (i + 1 == n) {
if (!are_connected({ qu2[cnt2 - 1] }, { i }))
while (cnt2)
qu1[cnt1++] = qu2[--cnt2];
qu2[cnt2++] = i;
} else {
if (are_connected({ i }, { i + 1 })) {
if (!are_connected({ qu2[cnt2 - 1] }, { i }))
while (cnt2)
qu1[cnt1++] = qu2[--cnt2];
qu2[cnt2++] = i, qu2[cnt2++] = i + 1;
} else {
qu1[cnt1++] = i + 1;
if (!are_connected({ qu2[cnt2 - 1] }, { i }))
while (cnt2)
qu1[cnt1++] = qu2[--cnt2];
qu2[cnt2++] = i;
}
i++;
}
vi uu, vv, ww;
if (cnt2 == 0)
for (int h = 0; h < cnt1; h++)
ww.push_back(qu1[h]);
else if (cnt1 == 0)
for (int h = 0; h < cnt2; h++)
ww.push_back(qu2[h]);
else {
vi uu, vv;
int lower = 0, upper = cnt1 + 1;
while (upper - lower > 1) {
int k = (lower + upper) / 2;
uu.clear();
for (int h = 0; h < k; h++)
uu.push_back(qu1[(h - 1 + cnt1) % cnt1]);
vv.clear();
for (int h = 0; h < cnt2; h++)
vv.push_back(qu2[h]);
if (are_connected(uu, vv))
upper = k;
else
lower = k;
}
int h1 = lower;
if (h1 == cnt1) {
if (cnt1 > cnt2)
for (int h = 0; h < cnt1; h++)
ww.push_back(qu1[h]);
else
for (int h = 0; h < cnt2; h++)
ww.push_back(qu2[h]);
} else {
lower = 0, upper = cnt2;
while (upper - lower > 1) {
int k = (lower + upper) / 2;
uu.clear();
for (int h = 0; h < cnt1; h++)
uu.push_back(qu1[h]);
vv.clear();
for (int h = 0; h < k; h++)
vv.push_back(qu2[(h - 1 + cnt2) % cnt2]);
if (are_connected(uu, vv))
upper = k;
else
lower = k;
}
int h2 = lower;
if (h1 == 1)
for (int h = cnt1 - 1; h >= 0; h--)
ww.push_back(qu1[h]);
else
for (int h = 0; h < cnt1; h++)
ww.push_back(qu1[(h1 + h) % cnt1]);
if (h2 == 1)
for (int h = 0; h < cnt2; h++)
ww.push_back(qu2[h]);
else
for (int h = cnt2 - 1; h >= 0; h--)
ww.push_back(qu2[(h2 + h) % cnt2]);
}
}
return ww;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 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 |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
540 KB |
Output is correct |
2 |
Correct |
6 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 |
6 ms |
428 KB |
Output is correct |
6 |
Correct |
9 ms |
344 KB |
Output is correct |
7 |
Incorrect |
1 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
600 KB |
Output is correct |
6 |
Correct |
6 ms |
344 KB |
Output is correct |
7 |
Incorrect |
1 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 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 |
4 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
424 KB |
Output is correct |
6 |
Correct |
6 ms |
596 KB |
Output is correct |
7 |
Incorrect |
2 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |