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 "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 20;
const long long inf = 1e15L + 20;
long long pos[maxn];
int depth[maxn];
int order1[maxn];
int order2[maxn];
int n, c;
bool cmp1(int i, int j) {
return pos[i] + depth[i] < pos[j] + depth[j];
}
bool cmp2(int i, int j) {
return pos[i] - depth[i] < pos[j] - depth[j];
}
bool check(long long lim) {
long long sum_lt = -inf;
long long sum_rt = inf;
long long diff_lt = -inf;
long long diff_rt = inf;
int pt = 0;
for (int i = 1; i <= n; i++) {
while (pt < n && pos[order2[pt + 1]] - depth[order2[pt + 1]] < pos[order1[i]] + depth[order1[i]] - lim) {
pt++;
}
if (pt >= 1 && order2[1] != order1[i]) {
sum_rt = min(sum_rt, (pos[order1[i]] - depth[order1[i]]) + (pos[order2[1]] - depth[order2[1]]) + (lim - c));
diff_rt = min(diff_rt, (-pos[order1[i]] - depth[order1[i]]) + (pos[order2[1]] - depth[order2[1]]) + (lim - c));
}
else if (pt >= 2) {
sum_rt = min(sum_rt, (pos[order1[i]] - depth[order1[i]]) + (pos[order2[2]] - depth[order2[2]]) + (lim - c));
diff_rt = min(diff_rt, (-pos[order1[i]] - depth[order1[i]]) + (pos[order2[2]] - depth[order2[2]]) + (lim - c));
}
for (int j = 1; j <= pt; j++) {
if (order1[i] != order2[j]) {
sum_lt = max(sum_lt, (pos[order1[i]] + depth[order1[i]]) + (pos[order2[j]] + depth[order2[j]]) - (lim - c));
diff_lt = max(diff_lt, (-pos[order1[i]] + depth[order1[i]] + (pos[order2[j]] + depth[order2[j]]) - (lim - c)));
}
}
}
swap(diff_lt, diff_rt);
diff_lt = -diff_lt;
diff_rt = -diff_rt;
if (sum_lt > sum_rt || diff_lt > diff_rt) {
return false;
}
int lt1 = n;
int lt2 = 1;
for (int i = 1; i <= n; i++) {
while (lt1 > 1 && pos[i] + pos[lt1 - 1] >= sum_lt) {
lt1--;
}
while (lt2 <= n && pos[lt2] < pos[i] - diff_rt) {
lt2++;
}
int lt = max(lt1, lt2);
if (lt < i && pos[lt] <= sum_rt - pos[i] && pos[lt] <= pos[i] - diff_lt) {
return true;
}
}
return false;
}
long long find_shortcut(int _n, vector<int> len, vector<int> _depth, int _c) {
n = _n;
c = _c;
pos[1] = 0;
for (int i = 2; i <= n; i++) {
pos[i] = pos[i - 1] + len[i - 2];
}
for (int i = 1; i <= n; i++) {
order1[i] = i;
order2[i] = i;
depth[i] = _depth[i - 1];
}
sort(order1 + 1, order1 + n + 1, cmp1);
sort(order2 + 1, order2 + n + 1, cmp2);
long long lt = 0;
long long rt = inf;
long long res = -1;
while (lt <= rt) {
long long mt = (lt + rt) / 2;
if (check(mt)) {
res = mt;
rt = mt - 1;
}
else {
lt = mt + 1;
}
}
return res;
}
# | 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... |