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>
#define NO_DEBUG
#define debug(x) cerr << #x << " is " << x << "\n"
using namespace std;
using lint = long long;
const lint INF = 1e18;
int N, C;
vector<lint> X;
vector<lint> D;
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
for (int j = 0; j < N; j++) {
for (int i = 0; i < j; i++) {
if (X[j] - X[i] + D[j] + D[i] > T) { // minimize X[i] - D[i]
max_sum = min(max_sum, + T - (- X[i] - X[j] + D[i] + D[j] + C));
min_sum = max(min_sum, - T + (+ X[i] + X[j] + D[i] + D[j] + C));
max_dif = min(max_dif, + T - (- X[i] + X[j] + D[i] + D[j] + C));
min_dif = max(min_dif, - T + (+ X[i] - X[j] + D[i] + D[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);
}
#ifndef NO_DEBUG
cerr << "====================== ";
debug(T);
debug(max_sum);
debug(min_sum);
debug(max_dif);
debug(min_dif);
cerr << "\n\n";
#endif
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);
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];
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:58:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < l_.size(); i++)
~~^~~~~~~~~~~
shortcut.cpp:60:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < d_.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... |