This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
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) {
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];
}
}
}
}
}
for (int p = 0; p < n; ++p) {
int L = 0, R = n;
for (int i: prefix) {
ll D = dist(p, i) + d[i];
int l = -1, r = n;
while (l + 1 < r) {
int m = (l + r) >> 1;
auto it = lower_bound(cant[i].begin(), cant[i].end(), m) - cant[i].begin();
if (it == cant[i].size() || D + c + dist(m, mx_suf[i][it]) + d[mx_suf[i][it]] <= mid) {
r = m;
} else {
l = m;
}
}
L = max(L, r);
l = -1, r = n;
while (l + 1 < r) {
int m = (l + r) >> 1;
auto it = upper_bound(cant[i].begin(), cant[i].end(), m) - cant[i].begin();
if (it == 0 || D + c + dist(m, mx_pref[i][it - 1]) + d[mx_pref[i][it - 1]] <= mid) {
l = m;
} else {
r = m;
}
}
R = min(R, r);
if (L >= R) {
break;
}
}
if (L < R) {
return true;
}
}
return false;
};
if (check()) {
high = mid;
} else {
low = mid;
}
}
return high;
}
Compilation message (stderr)
shortcut.cpp: In lambda function:
shortcut.cpp:38:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int j = 0; j < cant[i].size(); ++j) {
| ~~^~~~~~~~~~~~~~~~
shortcut.cpp:46:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | if (j == size(cant[i]) - 1 ||
| ~~^~~~~~~~~~~~~~~~~~~~
shortcut.cpp:64:32: warning: comparison of integer expressions of different signedness: 'long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | 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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |