Problem:
Score : 1200 points

Problem Statement

Snuke loves flags.

Snuke is placing N flags on a line.

The i-th flag can be placed at either coordinate x_i or coordinate y_i.

Snuke thinks that the flags look nicer when the smallest distance between two of them, d, is larger. Find the maximum possible value of d.

Constraints

2 ≤ N ≤ 10^{4}

1 ≤ x_i, y_i ≤ 10^{9}

x_i and y_i are integers.

Input

The input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the answer.

Sample Input 1

3
1 3
2 5
1 9

Sample Output 1

4

The optimal solution is to place the first flag at coordinate 1, the second flag at coordinate 5 and the third flag at coordinate 9. The smallest distance between two of the flags is 4 in this case.

Sample Input 2

5
2 2
2 2
2 2
2 2
2 2

Sample Output 2

0

There can be more than one flag at the same position.

Sample Input 3

22
93 6440
78 6647
862 11
8306 9689
798 99
801 521
188 206
6079 971
4559 209
50 94
92 6270
5403 560
803 83
1855 99
42 504
75 484
629 11
92 122
3359 37
28 16
648 14
11 269

Sample Output 3

17

Solution in C++ (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
typedef long long LL;
const int N = 105001;
const int M = 5000;
const int inf = 500000000;
#ifndef __wfx
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
#define debug printf("fuck %d\n", __LINE__);
inline void _(char ch){*O++ = ch;} void print(int x) {if(x < 0) _('-'), x = -x; if(x>9) print(x/10);*O++ = x % 10 ^ 48;}
inline int read(){
	int res = 0; char ch = getchar(); bool bo = false;
	while(ch < '0' || ch > '9') bo = (ch == '-'), ch = getchar();
	while(ch >= '0' && ch <= '9') res = (res << 1) + (res << 3) + (ch ^ 48), ch = getchar();
	return bo ? -res : res;
}

struct Edge {
    int to;
    Edge *nxt;
    Edge (int to = 0, Edge *nxt = NULL) : to(to), nxt(nxt) {}
} *head[N << 3], e[N << 5], *tail;

struct node {
    int val, id;
    friend bool operator < (const node &a, const node &b) { return a.val < b.val; }
} v[N << 1];

int n, val[N][2], tot, root, dfn[N << 3], low[N << 3], scc[N << 3], dfntot, scctot, s[N << 3], Top, lson[N << 3], rson[N << 3];
bool vis[N << 3];

void add(int x, int y) {
    // printf("%d %d\n", x, y);
    head[x] = new (tail++) Edge(y, head[x]); 
} 

void build (int &o, int l, int r) {
    o = ++tot;
    if(l == r) {
        add(o, v[l].id <= n ? v[l].id + n : v[l].id - n);
        return;
    }
    build(lson[o], l, mid);
    build(rson[o], mid + 1, r);
    add(o, lson[o]), add(o, rson[o]);
}

void add (int o, int l, int r, int L, int R, int x) {
    if(l >= L && r <= R) {
        add(x, o);
        return;
    }
    if(mid >= L) add(lson[o], l, mid, L, R, x);
    if(mid < R) add(rson[o], mid + 1, r, L, R, x);
}

#undef mid

void tarjan(int x) {
    low[x] = dfn[x] = ++dfntot; vis[x] = true; s[++Top] = x;
    for (Edge *i = head[x]; i; i = i->nxt) {
        if(!dfn[i->to]) tarjan(i->to), low[x] = std::min(low[x], low[i->to]);
        else if(vis[i->to]) low[x] = std::min(low[x], dfn[i->to]);
    }
    if(dfn[x] == low[x]) {
        ++scctot; int k;
        do {
            k = s[Top --];
            scc[k] = scctot;
            vis[k] = false;
        } while(Top && k != x);
    }
}

bool check(int mid) {
    tot = n << 1; tail = e; scctot = dfntot = 0;
    memset(head, 0, sizeof (head));
    build(root, 1, n << 1);
    for (int i = 1; i <= n << 1; ++i) {
        int x = v[i].id;
        int l = std::lower_bound(v + 1, v + (n << 1) + 1, (node){v[i].val - mid + 1, 0}) - v;
        int r = std::upper_bound(v + 1, v + (n << 1) + 1, (node){v[i].val + mid - 1, 0}) - v - 1;
        if (l <= i - 1) add(root, 1, n << 1, l, i - 1, x);
        if (r >= i + 1) add(root, 1, n << 1, i + 1, r, x);
    }
    memset(dfn, 0, sizeof (dfn));
    tarjan(root);
    for (int i = 1; i <= n; ++i) {
        if (scc[i] == scc[i + n]) return false;
    }
    return true;
}

int main() {
    n = read();
    int l = 0, r = 0;
    for (int i = 1; i <= n; ++i) {
        val[i][0] = read(), val[i][1] = read(), r = std::max(r, std::max(val[i][0], val[i][1]));
        v[i] = (node) {val[i][0], i};
        v[i + n] = (node) {val[i][1], i + n};
    }
    std::sort(v + 1, v + (n << 1) + 1);
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (check(mid)) l = mid + 1;
        else r = mid - 1;
    }
    printf("%d\n", l - 1);
    return 0;
}
/*
3
1 2 3 4 5 6
*/