#include <bits/stdc++.h>
#include "shortcut.h"
using namespace std;
using ll = long long;
ll find_shortcut(int n, vector<int> l, vector<int> d, int c) {
vector<ll> pref(n);
for (int i = 0; i < n - 1; ++i) {
pref[i + 1] = pref[i] + l[i];
}
auto dist = [&](int i, int j) -> long long {
if (i > j) swap(i, j);
return pref[j] - pref[i];
};
ll low = 0, high = pref[n - 1] + (*max_element(d.begin(), d.end())) * 2;
while (low + 1 < high) {
ll mid = (low + high) >> 1;
auto check = [&]() -> bool {
vector<vector<int>> cant(n), mx_pref(n), mx_suf(n), nxt(n);
vector<int> prefix;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != j && dist(i, j) + d[i] + d[j] > mid) {
cant[i].push_back(j);
}
}
if (!cant[i].empty()) {
if (cant[i][0] < i && cant[i].back() > i) {
return false;
}
if (cant[i][0] > i) {
nxt[i].assign(n + 1, cant[i].size());
for (int j = 0; j < cant[i].size(); ++j) {
nxt[i][cant[i][j]] = j;
}
for (int j = n - 1; j > -1; --j) {
if (nxt[i][j] == cant[i].size()) {
nxt[i][j] = nxt[i][j + 1];
}
}
prefix.push_back(i);
mx_pref[i].resize(cant[i].size());
mx_suf[i].resize(cant[i].size());
for (int j = 0; j < cant[i].size(); ++j) {
if (j == 0 || d[cant[i][j]] > dist(cant[i][j], mx_pref[i][j - 1]) + d[mx_pref[i][j - 1]]) {
mx_pref[i][j] = cant[i][j];
} else {
mx_pref[i][j] = mx_pref[i][j - 1];
}
}
for (int j = cant[i].size() - 1; j > -1; --j) {
if (j == size(cant[i]) - 1 ||
d[cant[i][j]] > dist(cant[i][j], mx_suf[i][j + 1]) + d[mx_suf[i][j + 1]]) {
mx_suf[i][j] = cant[i][j];
} else {
mx_suf[i][j] = mx_suf[i][j + 1];
}
}
}
}
}
vector<int> pnt(n);
for (int p = prefix[0]; p < n; ++p) {
int L = 0;
for (int i: prefix) {
ll D = dist(p, i) + d[i];
auto check = [&](int m) -> bool {
auto it = nxt[i][m];
if (it == cant[i].size() || D + c + dist(m, mx_suf[i][it]) + d[mx_suf[i][it]] <= mid) {
return true;
} else {
return false;
}
};
while (pnt[i] < n && !check(pnt[i])) {
++pnt[i];
}
while (pnt[i] > 0 && check(pnt[i] - 1)) {
--pnt[i];
}
L = max(L, pnt[i]);
if (L == n) {
break;
}
}
if (L == n) {
continue;
}
bool yay = true;
for (int i: prefix) {
int m = L;
auto it = nxt[i][m + 1];
if (it != 0 && dist(p, i) + d[i] + c + dist(m, mx_pref[i][it - 1]) + d[mx_pref[i][it - 1]] > mid) {
yay = false;
break;
}
}
if (yay) {
return true;
}
}
return false;
};
if (check()) {
high = mid;
} else {
low = mid;
}
}
return high;
}
Compilation message
shortcut.cpp: In lambda function:
shortcut.cpp:36:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int j = 0; j < cant[i].size(); ++j) {
| ~~^~~~~~~~~~~~~~~~
shortcut.cpp:40:43: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | if (nxt[i][j] == cant[i].size()) {
shortcut.cpp:47:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int j = 0; j < cant[i].size(); ++j) {
| ~~^~~~~~~~~~~~~~~~
shortcut.cpp:55:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | if (j == size(cant[i]) - 1 ||
| ~~^~~~~~~~~~~~~~~~~~~~
shortcut.cpp: In lambda function:
shortcut.cpp:72:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | if (it == cant[i].size() || D + c + dist(m, mx_suf[i][it]) + d[mx_suf[i][it]] <= mid) {
| ~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
0 ms |
212 KB |
n = 9, 110 is a correct answer |
3 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |