#include "shortcut.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll INF = 1e13;
ll find_shortcut(int n, vector<int> l, vector<int> d, int c) {
vector<ll> p(n, 0);
for (int i = 1; i < n; i++) p[i] = p[i - 1] + l[i - 1];
// Order the stations by their inequality stuff
vector<pair<ll, int>> iot(n);
// Order by d[i] + p[i]
for (int i = 0; i < n; i++) iot[i] = {d[i] + p[i], i};
sort(iot.begin(), iot.end());
// Binary search for the answer
ll lo = 0, hi = INF;
while (lo != hi) {
ll mid = (lo + hi) >> 1;
vector<ll> half_plane(4, -INF);
ll mx1 = -INF, mx2 = -INF;
for (int i = n - 1, lptr = n - 1; ~i; i--) {
while (lptr > i && mid - d[iot[i].second] + p[iot[i].second] < iot[lptr].first) {
mx1 = max(mx1, d[iot[lptr].second] + p[iot[lptr].second]);
mx2 = max(mx2, d[iot[lptr].second] - p[iot[lptr].second]);
lptr--;
}
// Update the half planes
half_plane[0] = max(half_plane[0], c - mid + d[iot[i].second] + p[iot[i].second] + mx1);
half_plane[1] = max(half_plane[1], c - mid + d[iot[i].second] - p[iot[i].second] + mx1);
half_plane[2] = max(half_plane[2], c - mid + d[iot[i].second] + p[iot[i].second] + mx2);
half_plane[3] = max(half_plane[3], c - mid + d[iot[i].second] - p[iot[i].second] + mx2);
}
// Check that the half-planes have a common intersection
bool good = false;
// Point (x, y) is below the half-plane intersection if y < max(-x + half_plane[0], x + half_plane[1])
// Point (x, y) is above the half-plane intersection if y > min(-x - half_plane[3], x - half_plane[2])
for (int i = 0, lower = n - 1, upper = 0; i < n; i++) {
while (lower >= 0 && p[lower] >= max(-p[i] + half_plane[0], p[i] + half_plane[1]))
lower--;
while (lower + 1 < n && p[lower + 1] < max(-p[i] + half_plane[0], p[i] + half_plane[1]))
lower++;
while (upper < n && p[upper] <= min(-p[i] - half_plane[3], p[i] - half_plane[2]))
upper++;
while (upper > 0 && p[upper - 1] > min(-p[i] - half_plane[3], p[i] - half_plane[2]))
upper--;
if (upper - lower > 1) {
good = true;
break;
}
}
if (good) hi = mid;
else lo = mid + 1;
}
return lo;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
384 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
364 KB |
n = 2, incorrect answer: jury 62 vs contestant 51 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
384 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
364 KB |
n = 2, incorrect answer: jury 62 vs contestant 51 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
384 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
364 KB |
n = 2, incorrect answer: jury 62 vs contestant 51 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
384 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
364 KB |
n = 2, incorrect answer: jury 62 vs contestant 51 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
384 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
364 KB |
n = 2, incorrect answer: jury 62 vs contestant 51 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
384 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
364 KB |
n = 2, incorrect answer: jury 62 vs contestant 51 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
384 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
364 KB |
n = 2, incorrect answer: jury 62 vs contestant 51 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
384 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
364 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
364 KB |
n = 2, incorrect answer: jury 62 vs contestant 51 |
6 |
Halted |
0 ms |
0 KB |
- |