This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 300005;
int n;
int a[MAXN * 2], b[2][MAXN];
int inl[MAXN * 2], inr[MAXN * 2], outl[MAXN * 2], outr[MAXN * 2];
int pl[MAXN * 2], pr[MAXN * 2], sl[MAXN * 2], sr[MAXN * 2];
bool check(int x) {
REP (z, 0, 2) {
if (abs(a[n] - b[z][n - 1]) > x || abs(a[0] - b[z ^ 1][0]) > x) {
continue;
}
REP (t, 0, 2) {
int bcnt = n;
int uptr = 0, dptr = 0;
REP (ti, 0, n) {
int i = t ? 2 * n - ti - 1 : ti;
while (bcnt && a[t ? n - bcnt : n + bcnt - 1] < a[i] + t) {
bcnt--;
}
int lo = max(0, ti - bcnt), hi = ti;
while (uptr < n && b[z][uptr] <= a[i] + x) {
uptr++;
}
while (dptr < n && b[z][dptr] < a[i] - x) {
dptr++;
}
int up = uptr - 1;
int down = dptr;
mnto(hi, up);
mxto(lo, down);
if (lo > hi) {
inl[i] = INF;
inr[i] = -INF;
continue;
}
int lft = ti - hi, rht = ti - lo;
if (lo == ti - bcnt) {
rht = ti;
}
if (t) {
inl[i] = 2 * n - rht - n;
inr[i] = 2 * n - lft - n;
} else {
inl[i] = lft;
inr[i] = rht;
}
}
uptr = 0, dptr = 0;
int scnt = 0;
REP (ti, 0, n) {
int i = t ? 2 * n - ti - 1 : ti;
while (scnt < n && a[t ? scnt : 2 * n - scnt - 1] < a[i] + t) {
scnt++;
}
int lo = ti, hi = min(n - 1, ti + scnt);
while (uptr < n && b[z ^ 1][uptr] <= a[i] + x) {
uptr++;
}
while (dptr < n && b[z ^ 1][dptr] < a[i] - x) {
dptr++;
}
int up = uptr - 1;
int down = dptr;
mnto(hi, up);
mxto(lo, down);
if (lo > hi) {
outl[i] = INF;
outr[i] = -INF;
continue;
}
int lft = n - hi + ti, rht = n - (lo - ti);
if (hi == ti + scnt) {
lft = ti + 1;
}
if (t) {
outl[i] = 2 * n - rht - n;
outr[i] = 2 * n - lft - n;
} else {
outl[i] = lft;
outr[i] = rht;
}
}
}
REP (i, 0, 2 * n) {
pl[i] = outl[i], pr[i] = outr[i];
if (i) {
mxto(pl[i], pl[i - 1]);
mnto(pr[i], pr[i - 1]);
}
}
sl[2 * n] = -INF; sr[2 * n] = INF;
RREP (i, 2 * n - 1, 0) {
sl[i] = outl[i], sr[i] = outr[i];
if (i) {
mxto(sl[i], sl[i + 1]);
mnto(sr[i], sr[i + 1]);
}
}
deque<int> dql, dqr;
auto addl = [&] (int i) {
while (!dql.empty() && dql.back() >= i + n) {
dql.pop_back();
}
while (!dql.empty() && inl[dql.front()] <= inl[i]) {
dql.pop_front();
}
dql.push_front(i);
};
auto addr = [&] (int i) {
while (!dqr.empty() && dqr.back() >= i + n) {
dqr.pop_back();
}
while (!dqr.empty() && inr[dqr.front()] >= inr[i]) {
dqr.pop_front();
}
dqr.push_front(i);
};
RREP (i, 2 * n - 1, n) {
addl(i);
addr(i);
}
RREP (i, n, 1) {
int l = max(pl[i - 1], sl[i + n]),
r = min(pr[i - 1], sr[i + n]);
mxto(l, inl[dql.back()]);
mnto(r, inr[dqr.back()]);
addl(i - 1);
addr(i - 1);
/*
REP (j, i, i + n) {
mxto(l, inl[j]);
mnto(r, inr[j]);
}
*/
if (l <= i && i <= r) {
return 1;
}
}
}
return 0;
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n;
REP (i, 0, 2 * n) {
cin >> a[i];
}
REP (z, 0, 2) {
REP (i, 0, n) {
cin >> b[z][i];
}
sort(b[z], b[z] + n);
}
int lo = 0, hi = INF;
while (lo < hi) {
int mid = lo + hi >> 1;
if (check(mid)) {
hi = mid;
} else {
lo = mid + 1;
}
}
cout << hi << '\n';
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:197:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
197 | int mid = lo + hi >> 1;
| ~~~^~~~
# | 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... |