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 SCC {
    size: usize,
    edge: Vec<(u32, u32)>,
    id: Vec<usize>,
}

impl SCC {
    pub fn new(size: usize) -> Self {
        SCC {
            size: size,
            edge: vec![],
            id: vec![0; size],
        }
    }
    pub fn add_edge(&mut self, from: usize, to: usize) {
        assert!(from < self.size && to < self.size);
        self.edge.push((from as u32, to as u32));
    }
    #[inline(never)]
    fn fill_buffer(&self, buf: &mut [u32], cnt: &[u32]) {
        let mut index = vec![0; self.size];
        let mut sum = 0;
        for (index, c) in index.iter_mut().zip(cnt.iter()) {
            *index = sum;
            sum += *c;
        }
        for &(f, t) in self.edge.iter() {
            let f = f as usize;
            buf[index[f] as usize] = t;
            index[f] += 1;
        }
    }
    #[inline(never)]
    fn build_graph<'a>(&self, buf: &'a mut [u32]) -> Vec<&'a mut [u32]> {
        let size = self.size;
        let mut cnt = vec![0; size];
        for e in self.edge.iter() {
            cnt[e.0 as usize] += 1;
        }
        self.fill_buffer(buf, &cnt);
        let mut buf = Some(buf);
        let mut ans = Vec::with_capacity(size);
        for &len in cnt.iter() {
            let len = len as usize;
            let x = buf.take().unwrap();
            let (x, y) = x.split_at_mut(len);
            ans.push(x);
            buf = Some(y);
        }
        ans
    }
    #[inline(never)]
    pub fn build(&mut self) {
        let size = self.size;
        let mut buf = vec![0; self.edge.len()];
        let buf = buf.as_mut_slice();
        let graph = self.build_graph(buf);
        let empty = size as u32;
        let mut index = 0;
        let mut ord = vec![empty; size];
        let mut assigned = vec![false; size];
        let mut id = 0;
        let mut stack_s = vec![];
        let mut stack_p = vec![];
        let mut call = vec![];
        for i in 0..size {
            if ord[i] != empty {
                continue;
            }
            enum Operation {
                Call(usize),
                Iterate(usize, usize),
                Eval(usize),
            }
            call.push(Operation::Call(i));
            while let Some(op) = call.pop() {
                match op {
                    Operation::Call(v) => {
                        ord[v] = index;
                        index += 1;
                        stack_s.push(v);
                        stack_p.push(v);
                        call.push(Operation::Eval(v));
                        call.push(Operation::Iterate(v, 0));
                    }
                    Operation::Iterate(v, k) => {
                        for i in k..graph[v].len() {
                            let u = graph[v][i] as usize;
                            if ord[u] == empty {
                                call.push(Operation::Iterate(v, i + 1));
                                call.push(Operation::Call(u));
                                break;
                            } else if !assigned[u] {
                                while let Some(&x) = stack_p.last() {
                                    if ord[x] <= ord[u] {
                                        break;
                                    } else {
                                        stack_p.pop();
                                    }
                                }
                            }
                        }
                    }
                    Operation::Eval(v) => {
                        if *stack_p.last().unwrap() == v {
                            while let Some(u) = stack_s.pop() {
                                assigned[u] = true;
                                self.id[u] = id;
                                if u == v {
                                    break;
                                }
                            }
                            stack_p.pop();
                            id += 1;
                        }
                    }
                }
            }
        }
        for x in self.id.iter_mut() {
            *x = id - 1 - *x;
        }
    }
    pub fn get_array(&self) -> Vec<usize> {
        self.id.clone()
    }
}
// ---------- end Strongly Connected Components ----------
// ---------- begin 2-SAT ----------
struct SAT2 {
    size: usize,
    scc: SCC,
}

impl SAT2 {
    fn new(size: usize) -> Self {
        SAT2 {
            size: size,
            scc: SCC::new(2 * size),
        }
    }
    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 can(m: i32, p: &[(i32, i32)]) -> bool {
    let mut z = vec![];
    for (i, &(x, y)) in p.iter().enumerate() {
        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 ng_seg: Vec<usize> = seg.iter().map(|p| !*p).collect();
    let mut index = vec![];
    for i in 0..p.len() {
        index.push((2 * size + i, 2 * size + p.len() + i));
    }
    let mut sat = SAT2::new(2 * size + 2 * p.len());
    for i in 1..size {
        sat.add(seg[i], ng_seg[2 * i]);
        sat.add(seg[i], ng_seg[2 * i + 1]);
    }
    for (i, &(x, y)) in p.iter().enumerate() {
        sat.add(index[i].0, index[i].1);
        let a = z.binary_search(&(x, i + 1)).unwrap();
        let b = z.binary_search(&(y, i + 1)).unwrap();
        sat.add(!index[i].0, seg[a + size]);
        sat.add(!index[i].1, seg[b + size]);
        for &(x, a, q) in [(x, a, index[i].0), (y, b, index[i].1)].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, ng_seg[l]);
                        l += 1;
                    }
                    if r & 1 == 1 {
                        r -= 1;
                        sat.add(!q, ng_seg[r]);
                    }
                    l >>= 1;
                    r >>= 1;
                }
            }
        }
    }
    sat.solve().is_some()
}

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![];
    for _ in 0..n {
        let x: i32 = it.next().unwrap().parse().unwrap();
        let y: i32 = it.next().unwrap().parse().unwrap();
        p.push((x, y));
    }
    let mut ok = 0;
    let mut ng = (999_999_999 / (n - 1)) as i32 + 1;
    while ng - ok > 1 {
        let m = (ok + ng) / 2;
        if can(m, &p) {
            ok = m;
        } else {
            ng = m;
        }
    }
    println!("{}", ok);
}

fn main() {
    run();
}