#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint INF = 1e18;
int N, C;
vector<lint> X;
vector<lint> D;
vector<int> SUM; // index of increasing X[i] + D[i]
vector<int> DIF; // index of decreasing X[i] - D[i]
bool is_valid(lint T) {
bool valid = false;
lint max_sum = +INF; // maximum of S1 + S2 that is valid
lint min_sum = -INF; // minimum of S1 + S2 that is valid
lint max_dif = +INF; // maximum of S1 - S2 that is valid
lint min_dif = -INF; // minimum of S1 - S2 that is valid
lint sum = -INF; // maximum X[i] + D[i], for all valid i
lint dif = +INF; // minimum X[i] - D[i], for all valid i
for (int j = 0, pos = 0; j < N; j++) {
while (pos < N && (X[SUM[j]] + D[SUM[j]]) - (X[DIF[pos]] - D[DIF[pos]]) > T) {
sum = max(sum, X[DIF[pos]] + D[DIF[pos]]);
dif = min(dif, X[DIF[pos]] - D[DIF[pos]]);
pos++;
}
max_sum = min(max_sum, + T + dif + (X[SUM[j]] - D[SUM[j]]) - C);
min_sum = max(min_sum, - T + sum + (X[SUM[j]] + D[SUM[j]]) + C);
max_dif = min(max_dif, + T + dif - (X[SUM[j]] + D[SUM[j]]) - C);
min_dif = max(min_dif, - T + sum - (X[SUM[j]] - D[SUM[j]]) + C);
}
for (int i = 0, ptl = 0, ptr = N - 1; i < N && !valid; i++) {
while (ptl < N && X[ptl] - X[i] < min_dif) ptl++;
while (ptr >= 0 && X[ptr] + X[i] >= min_sum) ptr--;
int pos = max(ptl, ptr + 1);
valid |= (pos < N && min_sum <= X[pos] + X[i] && X[pos] + X[i] <= max_sum
&& min_dif <= X[pos] - X[i] && X[pos] - X[i] <= max_dif);
}
return valid;
}
lint find_shortcut(int n_, vector<int> l_, vector<int> d_, int c_) {
N = n_, C = c_;
X.resize(N), D.resize(N), SUM.resize(N), DIF.resize(N);
for (int i = 0; i < l_.size(); i++)
X[i + 1] = X[i] + l_[i];
for (int i = 0; i < d_.size(); i++)
D[i] = d_[i];
iota(SUM.begin(), SUM.end(), 0), iota(DIF.begin(), DIF.end(), 0);
sort(SUM.begin(), SUM.end(), [&] (int l, int r) { return X[l] + D[l] < X[r] + D[r]; });
sort(DIF.begin(), DIF.end(), [&] (int l, int r) { return X[l] - D[l] < X[r] - D[r]; });
lint res = 0;
for (lint L = 0, R = INF, M = (L + R) / 2; L <= R; M = (L + R) / 2)
if (is_valid(M))
R = M - 1, res = M;
else
L = M + 1;
return res;
}
Compilation message
shortcut.cpp: In function 'lint find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:50:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < l_.size(); i++)
~~^~~~~~~~~~~
shortcut.cpp:52:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < d_.size(); i++)
~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
2 ms |
256 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
2 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
2 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
2 ms |
256 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
2 ms |
256 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
2 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
2 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
2 ms |
256 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
2 ms |
256 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
2 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
2 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
2 ms |
256 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
2 ms |
256 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
2 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
2 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
2 ms |
256 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
2 ms |
256 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
2 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
2 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
2 ms |
256 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
2 ms |
256 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
2 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
2 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
2 ms |
256 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
2 ms |
256 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
2 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
2 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
2 ms |
256 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 4, 80 is a correct answer |
2 |
Correct |
2 ms |
256 KB |
n = 9, 110 is a correct answer |
3 |
Correct |
2 ms |
376 KB |
n = 4, 21 is a correct answer |
4 |
Correct |
2 ms |
256 KB |
n = 3, 4 is a correct answer |
5 |
Incorrect |
2 ms |
256 KB |
n = 2, incorrect answer: jury 62 vs contestant 72 |
6 |
Halted |
0 ms |
0 KB |
- |