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 Rust (1.15.1) (judge verdict: Accepted)

// ---------- begin Strongly Connected Components ----------
struct SCCforSAT2 {
    graph: Vec<Vec<usize>>,
    edge: Vec<(usize, usize)>,
    id: Vec<usize>,
}

impl SCCforSAT2 {
    pub fn new(size: usize) -> Self {
        SCCforSAT2 {
            graph: vec![vec![]; size],
            edge: vec![],
            id: Vec::with_capacity(size),
        }
    }
    pub fn clear(&mut self) {
        self.edge.clear();
        self.id.clear();
    }
    pub fn add_edge(&mut self, from: usize, to: usize) {
        self.edge.push((from, to));
    }
    pub fn build_graph(&mut self, inv: bool) {
        for g in self.graph.iter_mut() {
            g.clear();
        }
        for &(f, t) in self.edge.iter() {
            if inv {
                self.graph[t].push(f);
            } else {
                self.graph[f].push(t);
            }
        }
    }
    fn dfs1(&mut self) -> Vec<usize> {
        self.build_graph(false);
        let size = self.graph.len();
        let mut q = vec![];
        let mut visited = vec![false; size];
        let mut stack = vec![];
        for v in 0..size {
            if visited[v] {
                continue;
            }
            stack.clear();
            visited[v] = true;
            stack.push((v, self.graph[v].iter()));
            while let Some((v, mut it)) = stack.pop() {
                let mut finish = true;
                while let Some(&u) = it.next() {
                    if !visited[u] {
                        finish = false;
                        visited[u] = true;
                        stack.push((v, it));
                        stack.push((u, self.graph[u].iter()));
                        break;
                    }
                }
                if finish {
                    q.push(v);
                }
            }
        }
        q
    }
    pub fn build(&mut self) {
        let q = self.dfs1();
        let size = self.graph.len();
        self.build_graph(true);
        self.id.clear();
        self.id.resize(size, size);
        let mut counter = 0;
        let mut stack = vec![];
        for &v in q.iter().rev() {
            if self.id[v] < size {
                continue;
            }
            self.id[v] = counter;
            stack.clear();
            stack.push(v);
            while let Some(v) = stack.pop() {
                self.id[v] = counter;
                for &u in self.graph[v].iter() {
                    if self.id[u] == size {
                        self.id[u] = counter;
                        stack.push(u);
                    }
                }
            }
            counter += 1;
        }
    }
    pub fn get_array(&self) -> Vec<usize> {
        self.id.clone()
    }
}
// ---------- end Strongly Connected Components ----------
// ---------- begin 2-SAT ----------
struct SAT2 {
    size: usize,
    scc: SCCforSAT2,
}

impl SAT2 {
    fn new(size: usize) -> Self {
        SAT2 {
            size: size,
            scc: SCCforSAT2::new(2 * size),
        }
    }
    fn clear(&mut self) {
        self.scc.clear();
    }
    fn add(&mut self, a: usize, b: usize) {
        let size = self.size;
        let (x, ix) = if a >= size {
            (!a + size, !a)
        } else {
            (a, a + size)
        };
        let (y, iy) = if b >= size {
            (!b + size, !b)
        } else {
            (b, b + size)
        };
        self.scc.add_edge(ix, y);
        self.scc.add_edge(iy, x);
    }
    fn solve(&mut self) -> Option<Vec<bool>> {
        self.scc.build();
        let id = self.scc.get_array();
        let mut ans = vec![];
        for i in 0..self.size {
            if id[i] == id[i + self.size] {
                return None;
            } else if id[i] < id[i + self.size] {
                ans.push(false);
            } else {
                ans.push(true);
            }
        }
        Some(ans)
    }
}
// ---------- end 2-SAT ----------

use std::io::Read;

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let mut p = vec![];
    let mut z = vec![];
    for i in 0..n {
        let x: i32 = it.next().unwrap().parse().unwrap();
        let y: i32 = it.next().unwrap().parse().unwrap();
        p.push((x, y));
        z.push((x, i + 1));
        z.push((y, i + 1));
    }
    z.sort();
    z.dedup();
    let size = z.len().next_power_of_two();
    let seg: Vec<usize> = (0..(2 * size)).collect();
    let sat = &mut SAT2::new(2 * size);
    let mut ok = 0;
    let mut ng = (1_000_000_000 / (n - 1)) as i32 + 1;
    while ng - ok > 1 {
        let m = (ok + ng) / 2;
        sat.clear();
        for i in 1..size {
            sat.add(seg[i], !seg[2 * i]);
            sat.add(seg[i], !seg[2 * i + 1]);
        }
        for (i, &(x, y)) in p.iter().enumerate() {
            let a = z.binary_search(&(x, i + 1)).unwrap();
            let b = z.binary_search(&(y, i + 1)).unwrap();
            sat.add(seg[a + size], seg[b + size]);
            for &(x, a, q) in [(x, a, seg[a + size]), (y, b, seg[b + size])].iter() {
                let l = z.binary_search(&(x - (m - 1), 0)).unwrap_err();
                let r = z.binary_search(&(x + (m - 1), p.len() + 1)).unwrap_err();
                for &(mut l, mut r) in [(l, a), (a + 1, r)].iter() {
                    l += size;
                    r += size;
                    while l < r {
                        if l & 1 == 1 {
                            sat.add(!q, !seg[l]);
                            l += 1;
                        }
                        if r & 1 == 1 {
                            r -= 1;
                            sat.add(!q, !seg[r]);
                        }
                        l >>= 1;
                        r >>= 1;
                    }
                }
            }
        }
        if sat.solve().is_some() {
            ok = m;
        } else {
            ng = m;
        }
    }
    println!("{}", ok);
}

fn main() {
    run();
}