// 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
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 |
1 |
Correct |
2 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
3 ms |
18780 KB |
Output is correct |
4 |
Correct |
2 ms |
18900 KB |
Output is correct |
5 |
Correct |
3 ms |
18776 KB |
Output is correct |
6 |
Correct |
2 ms |
18904 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18776 KB |
Output is correct |
10 |
Correct |
2 ms |
18900 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
3 ms |
18780 KB |
Output is correct |
4 |
Correct |
2 ms |
18900 KB |
Output is correct |
5 |
Correct |
3 ms |
18776 KB |
Output is correct |
6 |
Correct |
2 ms |
18904 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18776 KB |
Output is correct |
10 |
Correct |
2 ms |
18900 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
18788 KB |
Output is correct |
14 |
Correct |
3 ms |
18776 KB |
Output is correct |
15 |
Correct |
2 ms |
18776 KB |
Output is correct |
16 |
Correct |
3 ms |
18932 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
18 |
Correct |
4 ms |
18780 KB |
Output is correct |
19 |
Correct |
3 ms |
18780 KB |
Output is correct |
20 |
Correct |
3 ms |
18776 KB |
Output is correct |
21 |
Correct |
2 ms |
18780 KB |
Output is correct |
22 |
Correct |
2 ms |
18896 KB |
Output is correct |
23 |
Correct |
3 ms |
18776 KB |
Output is correct |
24 |
Correct |
3 ms |
18896 KB |
Output is correct |
25 |
Correct |
3 ms |
18892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
3 ms |
18780 KB |
Output is correct |
4 |
Correct |
2 ms |
18900 KB |
Output is correct |
5 |
Correct |
3 ms |
18776 KB |
Output is correct |
6 |
Correct |
2 ms |
18904 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18776 KB |
Output is correct |
10 |
Correct |
2 ms |
18900 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
18788 KB |
Output is correct |
14 |
Correct |
3 ms |
18776 KB |
Output is correct |
15 |
Correct |
2 ms |
18776 KB |
Output is correct |
16 |
Correct |
3 ms |
18932 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
18 |
Correct |
4 ms |
18780 KB |
Output is correct |
19 |
Correct |
3 ms |
18780 KB |
Output is correct |
20 |
Correct |
3 ms |
18776 KB |
Output is correct |
21 |
Correct |
2 ms |
18780 KB |
Output is correct |
22 |
Correct |
2 ms |
18896 KB |
Output is correct |
23 |
Correct |
3 ms |
18776 KB |
Output is correct |
24 |
Correct |
3 ms |
18896 KB |
Output is correct |
25 |
Correct |
3 ms |
18892 KB |
Output is correct |
26 |
Correct |
11 ms |
19040 KB |
Output is correct |
27 |
Correct |
14 ms |
18788 KB |
Output is correct |
28 |
Correct |
8 ms |
19032 KB |
Output is correct |
29 |
Correct |
3 ms |
18780 KB |
Output is correct |
30 |
Correct |
11 ms |
19036 KB |
Output is correct |
31 |
Correct |
10 ms |
18948 KB |
Output is correct |
32 |
Correct |
7 ms |
18776 KB |
Output is correct |
33 |
Correct |
4 ms |
18776 KB |
Output is correct |
34 |
Correct |
4 ms |
19032 KB |
Output is correct |
35 |
Correct |
4 ms |
18980 KB |
Output is correct |
36 |
Correct |
11 ms |
19028 KB |
Output is correct |
37 |
Correct |
5 ms |
19036 KB |
Output is correct |
38 |
Correct |
5 ms |
18996 KB |
Output is correct |
39 |
Correct |
6 ms |
19036 KB |
Output is correct |
40 |
Correct |
6 ms |
18776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2053 ms |
35900 KB |
Output is correct |
2 |
Correct |
1260 ms |
32864 KB |
Output is correct |
3 |
Correct |
1189 ms |
31092 KB |
Output is correct |
4 |
Correct |
1016 ms |
36484 KB |
Output is correct |
5 |
Correct |
978 ms |
32684 KB |
Output is correct |
6 |
Correct |
38 ms |
19032 KB |
Output is correct |
7 |
Correct |
151 ms |
37308 KB |
Output is correct |
8 |
Correct |
129 ms |
32048 KB |
Output is correct |
9 |
Correct |
829 ms |
37080 KB |
Output is correct |
10 |
Correct |
1063 ms |
36540 KB |
Output is correct |
11 |
Correct |
927 ms |
36676 KB |
Output is correct |
12 |
Correct |
904 ms |
34452 KB |
Output is correct |
13 |
Correct |
1590 ms |
33272 KB |
Output is correct |
14 |
Correct |
1505 ms |
36716 KB |
Output is correct |
15 |
Correct |
964 ms |
33240 KB |
Output is correct |
16 |
Correct |
1036 ms |
33272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
3 ms |
18780 KB |
Output is correct |
4 |
Correct |
2 ms |
18900 KB |
Output is correct |
5 |
Correct |
3 ms |
18776 KB |
Output is correct |
6 |
Correct |
2 ms |
18904 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
18780 KB |
Output is correct |
9 |
Correct |
3 ms |
18776 KB |
Output is correct |
10 |
Correct |
2 ms |
18900 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
12 |
Correct |
4 ms |
18780 KB |
Output is correct |
13 |
Correct |
3 ms |
18788 KB |
Output is correct |
14 |
Correct |
3 ms |
18776 KB |
Output is correct |
15 |
Correct |
2 ms |
18776 KB |
Output is correct |
16 |
Correct |
3 ms |
18932 KB |
Output is correct |
17 |
Correct |
3 ms |
18780 KB |
Output is correct |
18 |
Correct |
4 ms |
18780 KB |
Output is correct |
19 |
Correct |
3 ms |
18780 KB |
Output is correct |
20 |
Correct |
3 ms |
18776 KB |
Output is correct |
21 |
Correct |
2 ms |
18780 KB |
Output is correct |
22 |
Correct |
2 ms |
18896 KB |
Output is correct |
23 |
Correct |
3 ms |
18776 KB |
Output is correct |
24 |
Correct |
3 ms |
18896 KB |
Output is correct |
25 |
Correct |
3 ms |
18892 KB |
Output is correct |
26 |
Correct |
11 ms |
19040 KB |
Output is correct |
27 |
Correct |
14 ms |
18788 KB |
Output is correct |
28 |
Correct |
8 ms |
19032 KB |
Output is correct |
29 |
Correct |
3 ms |
18780 KB |
Output is correct |
30 |
Correct |
11 ms |
19036 KB |
Output is correct |
31 |
Correct |
10 ms |
18948 KB |
Output is correct |
32 |
Correct |
7 ms |
18776 KB |
Output is correct |
33 |
Correct |
4 ms |
18776 KB |
Output is correct |
34 |
Correct |
4 ms |
19032 KB |
Output is correct |
35 |
Correct |
4 ms |
18980 KB |
Output is correct |
36 |
Correct |
11 ms |
19028 KB |
Output is correct |
37 |
Correct |
5 ms |
19036 KB |
Output is correct |
38 |
Correct |
5 ms |
18996 KB |
Output is correct |
39 |
Correct |
6 ms |
19036 KB |
Output is correct |
40 |
Correct |
6 ms |
18776 KB |
Output is correct |
41 |
Correct |
2053 ms |
35900 KB |
Output is correct |
42 |
Correct |
1260 ms |
32864 KB |
Output is correct |
43 |
Correct |
1189 ms |
31092 KB |
Output is correct |
44 |
Correct |
1016 ms |
36484 KB |
Output is correct |
45 |
Correct |
978 ms |
32684 KB |
Output is correct |
46 |
Correct |
38 ms |
19032 KB |
Output is correct |
47 |
Correct |
151 ms |
37308 KB |
Output is correct |
48 |
Correct |
129 ms |
32048 KB |
Output is correct |
49 |
Correct |
829 ms |
37080 KB |
Output is correct |
50 |
Correct |
1063 ms |
36540 KB |
Output is correct |
51 |
Correct |
927 ms |
36676 KB |
Output is correct |
52 |
Correct |
904 ms |
34452 KB |
Output is correct |
53 |
Correct |
1590 ms |
33272 KB |
Output is correct |
54 |
Correct |
1505 ms |
36716 KB |
Output is correct |
55 |
Correct |
964 ms |
33240 KB |
Output is correct |
56 |
Correct |
1036 ms |
33272 KB |
Output is correct |
57 |
Correct |
1385 ms |
36628 KB |
Output is correct |
58 |
Correct |
882 ms |
33316 KB |
Output is correct |
59 |
Correct |
486 ms |
29236 KB |
Output is correct |
60 |
Correct |
964 ms |
36924 KB |
Output is correct |
61 |
Correct |
1055 ms |
33636 KB |
Output is correct |
62 |
Correct |
589 ms |
29836 KB |
Output is correct |
63 |
Correct |
35 ms |
19036 KB |
Output is correct |
64 |
Correct |
228 ms |
35392 KB |
Output is correct |
65 |
Correct |
224 ms |
32088 KB |
Output is correct |
66 |
Correct |
1490 ms |
35924 KB |
Output is correct |
67 |
Correct |
444 ms |
35444 KB |
Output is correct |
68 |
Correct |
478 ms |
34252 KB |
Output is correct |
69 |
Correct |
289 ms |
30664 KB |
Output is correct |
70 |
Correct |
543 ms |
34552 KB |
Output is correct |
71 |
Correct |
1261 ms |
35272 KB |
Output is correct |
72 |
Correct |
924 ms |
34828 KB |
Output is correct |
73 |
Correct |
1183 ms |
34920 KB |
Output is correct |
74 |
Correct |
2166 ms |
34256 KB |
Output is correct |
75 |
Correct |
1136 ms |
28284 KB |
Output is correct |
76 |
Correct |
2083 ms |
35076 KB |
Output is correct |
77 |
Correct |
681 ms |
31900 KB |
Output is correct |