#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<long long, long long>> 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;
// 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)
xpy.first = max(xpy.first, sum[i] - lim + c + d[i] + spd[q].second);
xpy.second = min(xpy.second, sum[i] + lim - c - d[i] + smd[q].first);
xmy.first = max(xmy.first, sum[i] - lim + c + d[i] - smd[q].first);
xmy.second = min(xmy.second, sum[i] + lim - c - d[i] - spd[q].second);
}
for (int p = 0; p < n; p++) {
for (int q = p + 1; q < n; q++) {
if (xpy.first <= sum[p] + sum[q] && sum[p] + sum[q] <= xpy.second) {
if (xmy.first <= sum[p] - sum[q] && sum[p] - sum[q] <= xmy.second) {
return true;
}
}
}
}
return false;
}
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] = {sum[j] + d[j], sum[j] + d[j]};
else {
spd[p].first = min(spd[p+1].first, sum[j] + d[j]);
spd[p].second = max(spd[p+1].second, sum[j] + d[j]);
}
}
smd.resize(n);
for (int p = n - 1; p >= 0; p--) {
int j = idx[p];
if (p == n-1) smd[p] = {sum[j] - d[j], sum[j] - d[j]};
else {
smd[p].first = min(smd[p+1].first, sum[j] - d[j]);
smd[p].second = max(smd[p+1].second, sum[j] - d[j]);
}
}
long long st = 1, 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... |