#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
int n, c;
vector<int> l, d;
vector<long long> sum;
vector<int> ord, idx;
vector<pair<int, int>> spd, smd;
bool check(long long lim) {
pair<long long, long long> xpy = {-1e18, 1e18}, xmy = {-1e18, 1e18};
int q = 0;
for (int p = 0; p < n; p++) {
int i = ord[p];
while (q < n && d[i] + d[idx[q]] + sum[idx[q]] - sum[i] <= lim) q++;
if (q == n) break;
int j1 = (spd[q].first == i ? spd[q].second : spd[q].first);
int j2 = (smd[q].first == i ? smd[q].second : smd[q].first);
// xpy.first <- sum[i] + sum[j] - lim + c + d[i] + d[j] (max)
// xpy.second <- sum[i] + sum[j] + lim - c - d[i] - d[j] (min)
// xmy.first <- sum[i] - sum[j] - lim + c + d[i] + d[j] (max)
// xmy.second <- sum[i] - sum[j] + lim - c - d[i] - d[j] (min)
if (j1 > i) xpy.first = max(xpy.first, sum[i] - lim + c + d[i] + sum[j1] + d[j1]);
if (j2 > i) xpy.second = min(xpy.second, sum[i] + lim - c - d[i] + sum[j2] - d[j2]);
if (j2 > i) xmy.first = max(xmy.first, sum[i] - lim + c + d[i] - sum[j2] + d[j2]);
if (j1 > i) xmy.second = min(xmy.second, sum[i] + lim - c - d[i] - sum[j1] - d[j1]);
}
q = 0;
for (int p = 0; p < n; p++) {
long long lb = max(xpy.first - sum[p], sum[p] - xmy.second);
long long ub = min(xpy.second - sum[p], sum[p] - xmy.first);
if (lb > ub) continue;
q = max(q, p+1);
while (q < n && sum[q] < lb) q++;
while (q > p+1 && sum[q-1] >= lb) q--;
if (q < n && sum[q] <= ub) return true;
}
return false;
}
long long get_lower_bound() {
long long ret = 0;
int j;
for (int i = 0; i < n; i++) {
if (ret < d[i]) {
ret = d[i];
j = i;
}
}
ret = 0;
for (int i = 0; i < n; i++) {
if (i == j) continue;
ret = max(ret, (long long)d[i]);
}
return ret + d[j];
}
long long find_shortcut(int _n, vector<int> _l, vector<int> _d, int _c) {
n = _n;
l = _l;
d = _d;
c = _c;
sum.resize(n);
sum[0] = 0;
for (int i = 1; i < n; i++) {
sum[i] = sum[i-1] + l[i-1];
}
idx.resize(n);
for (int i = 0; i < n; i++) idx[i] = i;
sort(idx.begin(), idx.end(), [](int a, int b) {
return sum[a] + d[a] < sum[b] + d[b];
});
ord.resize(n);
for (int i = 0; i < n; i++) ord[i] = i;
sort(ord.begin(), ord.end(), [](int a, int b) {
return sum[a] - d[a] < sum[b] - d[b];
});
spd.resize(n);
for (int p = n - 1; p >= 0; p--) {
int j = idx[p];
if (p == n-1) spd[p] = {j, j};
else if (p == n-2) spd[p] = sum[j] + d[j] > sum[idx[p+1]] + d[idx[p+1]] ? make_pair(j, idx[p+1]) : make_pair(idx[p+1], j);
else {
if (sum[j] + d[j] > sum[spd[p+1].first] + d[spd[p+1].first]) {
spd[p] = {j, spd[p+1].first};
}
else if (sum[j] + d[j] > sum[spd[p+1].second] + d[spd[p+1].second]) {
spd[p] = {spd[p+1].first, j};
}
else {
spd[p] = spd[p+1];
}
}
}
smd.resize(n);
for (int p = n - 1; p >= 0; p--) {
int j = idx[p];
if (p == n-1) smd[p] = {j, j};
else if (p == n-2) smd[p] = sum[j] - d[j] < sum[idx[p+1]] - d[idx[p+1]] ? make_pair(j, idx[p+1]) : make_pair(idx[p+1], j);
else {
if (sum[j] - d[j] < sum[smd[p+1].first] - d[smd[p+1].first]) {
smd[p] = {j, smd[p+1].first};
}
else if (sum[j] - d[j] < sum[smd[p+1].second] - d[smd[p+1].second]) {
smd[p] = {smd[p+1].first, j};
}
else {
smd[p] = smd[p+1];
}
}
}
long long st = get_lower_bound(), ed = 1e18;
while (st < ed) {
long long mid = (st + ed) / 2;
if (check(mid)) ed = mid;
else st = mid + 1;
}
return st;
}
Compilation message (stderr)
shortcut.h:1:9: warning: #pragma once in main file
1 | #pragma once
| ^~~~
shortcut_c.h:1:9: warning: #pragma once in main file
1 | #pragma once
| ^~~~| # | 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... |