#include <algorithm>
#include "shortcut.h"
using namespace std;
typedef long long ll;
ll minCost(vector<int> &v){
auto it = min_element(v.begin(), v.end());
ll t1 = (it == v.begin() ? 1e18 : *min_element(v.begin(), it));
ll t2 = (it == prev(v.end()) ? 1e18 : *min_element(next(it), v.end()));
return *it + min(t1, t2);
}
inline ll min(ll x, ll y){
return x < y ? x : y;
}
inline ll max(ll x, ll y){
return x > y ? x : y;
}
int n;
ll cost;
ll arr[1000002];
ll loc[1000002];
pair<ll, int> vec[1000002];
pair<ll, int> vec2[1000002];
ll maxXpd, maxXpd2;
inline bool able(ll);
ll find_shortcut(int N, vector<int> l, vector<int> d, int C){
n = N;
for(int i=1; i<=n; i++) arr[i] = d[i-1];
for(int i=2; i<=n; i++) loc[i] = loc[i-1] + l[i-2];
cost = C;
for(int i=1; i<=n; i++) vec[i] = make_pair(arr[i]+loc[i], i), vec2[i] = make_pair(loc[i]-arr[i], i);
sort(vec+1, vec+n+1);
sort(vec2+1, vec2+n+1);
reverse(vec+1, vec+n+1);
reverse(vec2+1, vec2+n+1);
maxXpd = vec[1].first, maxXpd2 = vec[1].second;
ll MIN = minCost(d), MAX = 1e18, ANS = 1e18;
while(MIN <= MAX){
ll MID = (MIN + MAX) / 2;
if(able(MID)){
ANS = MID;
MAX = MID-1;
}
else MIN = MID+1;
}
return ANS;
}
inline bool able(ll X){
ll minPlus = -1e18, maxPlus = 1e18;
ll minMinus = -1e18, maxMinus = 1e18;
int pnt = 1;
ll minXmd = 1e18, minXmd2 = 1e18;
int minXmdLoc = -1;
for(int i=1; i<=n; i++){
int x = vec2[i].second;
while(pnt <= n && vec[pnt].first > X + loc[x] - arr[x]){
ll tmp = loc[vec[pnt].second] - arr[vec[pnt].second];
if(minXmd > tmp){
minXmd2 = minXmd;
minXmd = tmp;
minXmdLoc = vec[pnt].second;
}
else if(minXmd2 > tmp){
minXmd2 = tmp;
}
pnt++;
}
if(pnt==1 || (pnt==2 && x==vec[1].second)) continue;
ll tMax = (x==vec[1].second ? maxXpd2 : maxXpd);
ll tMin = (x==minXmdLoc ? minXmd2 : minXmd);
maxPlus = min(maxPlus, loc[x] - arr[x] + X + tMin - cost);
minPlus = max(minPlus, loc[x] + arr[x] - X + tMax + cost);
maxMinus = min(maxMinus, -loc[x] - arr[x] + X + tMin - cost);
minMinus = max(minMinus, -loc[x] + arr[x] - X + tMax + cost);
}
if(minPlus > maxPlus || minMinus > maxMinus) return false;
pnt = 1;
for(int i=1; i<=n; i++){
ll llim = max(minPlus - loc[i], loc[i] - maxMinus);
ll rlim = min(maxPlus - loc[i], loc[i] - minMinus);
while(pnt <= n && llim > loc[pnt]) pnt++;
while(pnt > 1 && llim <= loc[pnt-1]) pnt--;
if(pnt <= n && loc[pnt] <= rlim) return true;
}
return false;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
204 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
204 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
204 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
204 KB |
n = 2, incorrect answer: jury 62 vs contestant 61 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
204 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
204 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
204 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
204 KB |
n = 2, incorrect answer: jury 62 vs contestant 61 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
204 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
204 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
204 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
204 KB |
n = 2, incorrect answer: jury 62 vs contestant 61 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
204 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
204 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
204 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
204 KB |
n = 2, incorrect answer: jury 62 vs contestant 61 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
204 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
204 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
204 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
204 KB |
n = 2, incorrect answer: jury 62 vs contestant 61 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
204 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
204 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
204 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
204 KB |
n = 2, incorrect answer: jury 62 vs contestant 61 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
204 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
204 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
204 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
204 KB |
n = 2, incorrect answer: jury 62 vs contestant 61 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
1 ms |
204 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
1 ms |
204 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
1 ms |
204 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
1 ms |
204 KB |
n = 2, incorrect answer: jury 62 vs contestant 61 |
6 |
Halted |
0 ms |
0 KB |
- |