This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "shortcut.h"
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint INF = 2e15;
struct SumDif {
lint sum, dif;
lint X, D;
int id;
};
int N, C;
SumDif SUM[1000000]; // increasing X[i] + D[i]
SumDif DIF[1000000]; // increasing X[i] - D[i]
lint X[1000000]; // location of main stations
bool is_valid(lint T) {
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_big = -INF; // maximum X[i] + D[i], for all valid i
lint sum_sml = -INF; // second maximum X[i] + D[i], for all valid i
int sum_idx = -1; // index of sum_big
lint dif_sml = +INF; // minimum X[i] - D[i], for all valid i
lint dif_big = +INF; // second minimum X[i] - D[i], for all valid i
int dif_idx = -1; // index of dif_sml
for (int j = 0, pos = 0; j < N; j++) {
while (pos < N && SUM[j].sum - DIF[pos].dif > T) {
lint sum_val = DIF[pos].sum, dif_val = DIF[pos].dif;
if (sum_big < sum_val) {
sum_sml = sum_big;
sum_big = sum_val;
sum_idx = DIF[pos].id;
} else if (sum_sml < sum_val) {
sum_sml = sum_val;
}
if (dif_sml > dif_val) {
dif_big = dif_sml;
dif_sml = dif_val;
dif_idx = DIF[pos].id;
} else if (dif_big > dif_val) {
dif_big = dif_val;
}
pos++;
}
lint valid_sum = ((SUM[j].id != sum_idx)? sum_big : sum_sml);
lint valid_dif = ((SUM[j].id != dif_idx)? dif_sml : dif_big);
lint sum_val = SUM[j].sum, dif_val = SUM[j].dif;
max_sum = min(max_sum, + T + valid_dif + dif_val - C);
min_sum = max(min_sum, - T + valid_sum + sum_val + C);
max_dif = min(max_dif, + T + valid_dif - sum_val - C);
min_dif = max(min_dif, - T + valid_sum - dif_val + C);
}
if (min_sum > max_sum || min_dif > max_dif)
return false;
for (int i = 0, ptl = 0, ptr = N - 1, pos; i < N; i++) {
while (ptl < N && X[ptl] - X[i] < min_dif) ptl++;
while (ptr >= 0 && X[ptr] + X[i] >= min_sum) ptr--;
pos = max(ptl, ptr + 1);
if (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 true;
}
return false;
}
lint find_shortcut(int n_, vector<int> l_, vector<int> D, int c_) {
N = n_, C = c_;
for (int i = 0; i < l_.size(); i++)
X[i + 1] = X[i] + l_[i];
for (int i = 0; i < N; i++) {
SUM[i] = SumDif{X[i] + D[i], X[i] - D[i], X[i], D[i], i};
DIF[i] = SumDif{X[i] + D[i], X[i] - D[i], X[i], D[i], i};
}
sort(SUM, SUM + N, [&] (SumDif &l, SumDif &r) { return l.sum < r.sum; });
sort(DIF, DIF + N, [&] (SumDif &l, SumDif &r) { return l.dif < r.dif; });
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 (stderr)
shortcut.cpp: In function 'lint find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:85:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < l_.size(); i++)
~~^~~~~~~~~~~
# | 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... |