Round Marriage
Contest: Avito Code Challenge 2018
Difficulty rating: 2500
Tags: binary search, graph matchings, greedy

It's marriage season in Ringland!

Ringland has a form of a circle's boundary of length $$$L$$$. There are $$$n$$$ bridegrooms and $$$n$$$ brides, and bridegrooms decided to marry brides.

Of course, each bridegroom should choose exactly one bride, and each bride should be chosen by exactly one bridegroom.

All objects in Ringland are located on the boundary of the circle, including the capital, bridegrooms' castles and brides' palaces. The castle of the $$$i$$$-th bridegroom is located at the distance $$$a_i$$$ from the capital in clockwise direction, and the palace of the $$$i$$$-th bride is located at the distance $$$b_i$$$ from the capital in clockwise direction.

Let's define the inconvenience of a marriage the maximum distance that some bride should walk along the circle from her palace to her bridegroom's castle in the shortest direction (in clockwise or counter-clockwise direction).

Help the bridegrooms of Ringland to choose brides in such a way that the inconvenience of the marriage is the smallest possible.

Input format:
The first line contains two integers $$$n$$$ and $$$L$$$ ($$$1 \leq n \leq 2 \cdot 10^{5}$$$, $$$1 \leq L \leq 10^{9}$$$) — the number of bridegrooms and brides and the length of Ringland.

The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i < L$$$) — the distances from the capital to the castles of bridegrooms in clockwise direction.

The next line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$0 \leq b_i < L$$$) — the distances from the capital to the palaces of brides in clockwise direction.

Output format:
In the only line print the smallest possible inconvenience of the wedding, where the inconvenience is the largest distance traveled by a bride.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first example the first bridegroom should marry the second bride, the second bridegroom should marry the first bride. This way, the second bride should walk the distance of $$$1$$$, and the first bride should also walk the same distance. Thus, the inconvenience is equal to $$$1$$$.

In the second example let $$$p_i$$$ be the bride the $$$i$$$-th bridegroom will marry. One of optimal $$$p$$$ is the following: $$$(6,8,1,4,5,10,3,2,7,9)$$$.

Example input:
2 4
0 1
2 3

Example output:
1

Example input:
10 100
3 14 15 92 65 35 89 79 32 38
2 71 82 81 82 84 5 90 45 23

Example output:
27

Editorial:
Key idea is that there there is a cut which is not crossed by anyoner (in other words, it is not profitable if brides will travel whole circle)
So we can solve the problem in $$$O(n \cdot L)$$$ — fix the cut, and match $$$i$$$-th bridegrom (in increasing order) with $$$i$$$-th bride (if we will order brides in the traversal order starting from border), and relax answer with current inconvenience.
Later we can note, that cuts not at brides are useless, so we can just choose cut from brides coordinates and get solution in $$$O(n^2)$$$.
Later we can note, that we are just searching for optimal cyclical shift of brides coordinates (of course if we will sort everything in increasing order).
Alright, then we want to find such $$$x < n$$$, so that $$$max(min(|a_i - b_{i + x}|, L - |a_i - b_{i + x}|))$$$ is minimal. (Where $$$i+x$$$ is by modulo $$$n$$$, of course).
Let's make binary search on $$$ans$$$, then we want to check is there such $$$x$$$, so that $$$min(|a_i - b_{i + x}|, L - |a_i - b_{i + x}|) \leq ans$$$? If we will fix $$$i$$$, then set of good $$$x$$$ is just cyclical segment (you can find it with binary search or two pointers), and then we want to check that intersection of these segments is not empty. How to check it? We can split cyclic segment into two non-cyclic, and then just add on the segment offline, and then you need to check that there are some point with value $$$n$$$.
So we can solve the problem in $$$O(n \cdot log L)$$$.
Bonus: how to solve in $$$O(n \cdot log n)$$$?
300iq's nlogLlogn