#include "shortcut.h"
#include <bits/stdc++.h>
#define x first
#define d second
using namespace std;
typedef pair<long long, long long> ii;
long long inf = (1LL << 62LL);
int n, C;
///ii(x, d) x - distance on main line, d is distance to side station
long long X[1000005]; ///sorted by increasing x
ii arr[1000005]; ///sorted by increasing x + d
ii arr2[1000005]; ///sorted by increasing x - d
///Vefify if it is possible to achieve diameter k
///For any pair of stations i and j (i < j), if x[j] - x[i] + d[i] + d[j], then it's fine
///Else let y and z be the positions of the shortcut edge
///d[i] + d[j] + |x[i] - y| + |x[i] - z| + C <= k must hold true
///Hence, the following conditions must be met:
///y + z >= (x[i] + d[i]) + (x[j] + d[j]) + C - k ... Let RHS = P
///y - z >= (x[i] + d[i]) - (x[j] - d[j]) + C - k ... Let RHS = Q
///y - z <= (x[i] - d[i]) - (x[j] + d[j]) - C + k ... Let RHS = R
///y + z <= (x[i] - d[i]) + (x[j] - d[j]) - C + k ... Let RHS = S
bool check(long long k){
long long P = 0, Q = 0, R = inf, S = inf; ///maximise R & Q, minimise R & S.
int i = 0;
long long maxXplusD = 0, minXminusD = inf; ///the possible values from arr2
for(int j = 0;j < n;j++){
ii J = arr[j];
while(i < n){
ii I = arr2[i];
if((J.x + J.d) - (I.x - I.d) <= k) break;
maxXplusD = max(I.x + I.d, maxXplusD);
minXminusD = min(I.x - I.d, minXminusD);
i++;
}
P = max(P, maxXplusD + (J.x + J.d));
Q = max(Q, maxXplusD - (J.x - J.d));
R = min(R, minXminusD - (J.x + J.d));
S = min(S, minXminusD + (J.x - J.d));
}
P += (C - k); Q += (C - k); R += (k - C); S += (k - C);
///If we fix z, then max(P-z,Q+z) <= y <= min(R+z,S-z)
///[Note, in the code, z refers to index not value]
for(int z = 1;z < n;z++){
long long lower = max(P - X[z], Q + X[z]);
long long upper = min(R + X[z], S - X[z]);
///check if there exist a y between lower and upper
if(upper_bound(X, X + z, upper) - lower_bound(X, X + z, lower) > 0) return true;
}
return false;
}
bool comp(ii a, ii b) { return a.x + a.d < b.x + b.d; }
bool comp2(ii a, ii b) { return a.x - a.d < b.x - b.d; }
long long find_shortcut(int _n, vector<int> l, vector<int> d, int _C){
n = _n, C = _C;
arr[0].x = 0;
arr[0].d = d[0];
for(int i = 1;i < n;i++){
arr[i] = ii(arr[i-1].x + l[i-1], d[i]);
}
for(int i = 0;i < n;i++){
X[i] = arr[i].x;
arr2[i] = arr[i];
}
sort(arr,arr+n,comp);
sort(arr2,arr2+n,comp2);
long long low = 0, high = inf;
while(true){
if(low == high - 1) break;
long long s = (low + high) / 2;
if(check(s)) high = s;
else low = s;
}
return high;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
5 ms |
376 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
5 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Incorrect |
5 ms |
376 KB |
n = 3, incorrect answer: jury 4 vs contestant 5 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
5 ms |
376 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
5 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Incorrect |
5 ms |
376 KB |
n = 3, incorrect answer: jury 4 vs contestant 5 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
5 ms |
376 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
5 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Incorrect |
5 ms |
376 KB |
n = 3, incorrect answer: jury 4 vs contestant 5 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
5 ms |
376 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
5 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Incorrect |
5 ms |
376 KB |
n = 3, incorrect answer: jury 4 vs contestant 5 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
5 ms |
376 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
5 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Incorrect |
5 ms |
376 KB |
n = 3, incorrect answer: jury 4 vs contestant 5 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
5 ms |
376 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
5 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Incorrect |
5 ms |
376 KB |
n = 3, incorrect answer: jury 4 vs contestant 5 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
5 ms |
376 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
5 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Incorrect |
5 ms |
376 KB |
n = 3, incorrect answer: jury 4 vs contestant 5 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
5 ms |
376 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
5 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Incorrect |
5 ms |
376 KB |
n = 3, incorrect answer: jury 4 vs contestant 5 |