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;
const long long inf = 1ll << 60;
long long find_shortcut(int n, vector<int> l, vector<int> d, int c) {
vector<long long> x(n);
for (int i = 0; i < n - 1; ++i) {
x[i + 1] = x[i] + l[i];
}
auto check = [&](long long limit) {
long long lsum = -inf, rsum = inf;
long long ldiff = -inf, rdiff = inf;
long long max_sum = -inf, min_diff = inf;
deque<int> q;
for (int i = 0; i < n; ++i) {
while (!q.empty() && x[i] - x[q.front()] + d[i] + d[q.front()] > limit) {
max_sum = max(max_sum, x[q.front()] + d[q.front()]);
min_diff = min(min_diff, x[q.front()] - d[q.front()]);
q.pop_front();
}
if (max_sum >= 0) {
lsum = max(lsum, x[i] + d[i] + max_sum + c - limit);
rsum = min(rsum, x[i] - d[i] + min_diff + limit - c);
rdiff = min(rdiff, x[i] - d[i] - max_sum + limit - c);
ldiff = max(ldiff, x[i] + d[i] - min_diff + c - limit);
}
while (!q.empty() && x[i] - d[i] < x[q.back()] - d[q.back()]) {
q.pop_back();
}
q.push_back(i);
}
if (lsum > rsum || ldiff > rdiff) {
return false;
}
for (int i = 0, j = n, k = 0; i < n; ++i) {
while (j && x[j - 1] + x[i] >= lsum) {
--j;
}
while (k < n && x[k] - x[i] < ldiff) {
++k;
}
int p = max(i + 1, max(j, k));
if (p < n && x[p] + x[i] <= rsum && x[p] - x[i] <= rdiff) {
return true;
}
}
return false;
};
long long low = 0, high = inf;
while (low < high) {
long long mid = low + high >> 1;
if (check(mid)) {
high = mid;
} else {
low = mid + 1;
}
}
return high;
}
Compilation message (stderr)
shortcut.cpp: In function 'long long int find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:54:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
long long mid = low + high >> 1;
~~~~^~~~~~
# | 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... |