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 Java8 (OpenJDK 1.8.0) (judge verdict: Runtime Error)


import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Map.Entry;

import static java.util.Arrays.*;

public class Main {
	private static final int mod = (int)1e9+7;

	final Random random = new Random(0);
	final IOFast io = new IOFast();

	/// MAIN CODE
	public void run() throws IOException {
//		int TEST_CASE = Integer.parseInt(new String(io.nextLine()).trim());
		int TEST_CASE = 1;
		while(TEST_CASE-- != 0) {
			int n = io.nextInt();
			int[] x = new int[n];
			int[] y = new int[n];
			for (int i = 0; i < n; i++) {
				x[i] = io.nextInt();
				y[i] = io.nextInt();
			}
			
			Sat2 sat = new Sat2(n);
			int low = 0, high = (int)1e9;
			while (high - low > 1) {
				int mid = (low + high) / 2;
				sat.clear();
				for (int i = 0; i < n; i++) {
					for (int j = 0; j < i; j++) {
						if (Math.abs(x[i] - x[j]) < mid) sat.Or(sat.Not(i), sat.Not(j));
						if (Math.abs(x[i] - y[j]) < mid) sat.Or(sat.Not(i), j);
						if (Math.abs(y[i] - x[j]) < mid) sat.Or(i, sat.Not(j));
						if (Math.abs(y[i] - y[j]) < mid) sat.Or(i, j);
					}
				}
				if (sat.satisfy()) low = mid;
				else high = mid;
			}
			io.out.println(low);
		}
	}
	
	static public class Sat2 {
		final int n;
		final boolean[] ans;
		final StronglyConnectedComponent scc;

		void clear() {
			scc.clear();
		}

		public Sat2(int n) {
			this.n = n;
			ans = new boolean[n];
			scc = new StronglyConnectedComponent(n * 2);
		}

		private int Not(int a) {
			if(a < 0 || a >= 2 * n) throw new RuntimeException();
			return a >= n ? a - n : a + n;
		}

		public void Or(int a, int b) {
			if(a < 0 || a >= 2 * n || b < 0 || b >= 2 * n) throw new RuntimeException();
			scc.addEdge(Not(a), b);
			scc.addEdge(Not(b), a);
		}

		public boolean satisfy() {
			scc.scc();
			for(int i = 0; i < n; i++) {
				// 同じ強連結成分に含まれたら充足不可能
				if(scc.cmp[i] == scc.cmp[i + n]) {
					return false;
				}
			}

			for(int i = 0; i < n; i++) {
				ans[i] = scc.cmp[i] > scc.cmp[i + n];
			}

			return true;
		}

		static class SimpleAdjListGraph {
			int m, V, E;
			int[] head, next, to;
		
			public SimpleAdjListGraph(int V, int E) {
				head = new int[V];
				next = new int[E];
				to = new int[E];
				this.V = V;
				this.E = E;
				clear();
			}
		
			public void clear() {
				m = 0;
				Arrays.fill(head, -1);
			}
		
			public void addEdge(int s, int t) {
				next[m] = head[s];
				head[s] = m;
				to[m++] = t;
			}
		}

		static public class StronglyConnectedComponent {
			int V;
			SimpleAdjListGraph G;
			SimpleAdjListGraph rG;
			int len;
			int[] vs;
			boolean[] used;
			public int[] cmp;

			void clear() {
				G.clear();
				rG.clear();
			}

			public StronglyConnectedComponent(int n) {
				V = n;
				G = new SimpleAdjListGraph(n, n*n);
				rG = new SimpleAdjListGraph(n, n*n);
				vs = new int[n*2];
				used = new boolean[n];
				cmp = new int[n];
			}

			public void addEdge(int from, int to) {
				G.addEdge(from, to);
				rG.addEdge(to, from);
			}

			private void dfs(int v) {
				used[v] = true;
				for (int e = G.head[v]; e != -1; e = G.next[e])
					if (!used[G.to[e]])
						dfs(G.to[e]);
				vs[len++] = v;
			}

			private void rdfs(int v, int k) {
				used[v] = true;
				cmp[v] = k;
				for (int e = rG.head[v]; e != -1; e = rG.next[e])
					if (!used[rG.to[e]])
						rdfs(rG.to[e], k);
			}

			public int scc() {
				for (int i = 0; i < V; i++)
					used[i] = false;
				len = 0;
				for (int i = 0; i < V; i++)
					if (!used[i])
						dfs(i);
				for (int i = 0; i < V; i++)
					used[i] = false;
				int k = 0;
				for (int i = len - 1; i >= 0; i--)
					if (!used[vs[i]])
						rdfs(vs[i], k++);
				return k;
			}

		}
	}
	
	/// TEMPLATE
	static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r); }
	static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r); }
	
	static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; }
	static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; }

	void printArrayLn(int[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); }
	void printArrayLn(long[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); }
	
	static void dump(Object... o) { System.err.println(Arrays.deepToString(o)); } 
	
	void main() throws IOException {
		//		IOFast.setFileIO("rle-size.in", "rle-size.out");
		try { run(); }
		catch (EndOfFileRuntimeException e) { }
		io.out.flush();
	}
	public static void main(String[] args) throws IOException { new Main().main(); }
	
	static class EndOfFileRuntimeException extends RuntimeException {
		private static final long serialVersionUID = -8565341110209207657L; }

	static
	public class IOFast {
		private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		private PrintWriter out = new PrintWriter(System.out);

		void setFileIn(String ins) throws IOException { in.close(); in = new BufferedReader(new FileReader(ins)); }
		void setFileOut(String outs) throws IOException { out.flush(); out.close(); out = new PrintWriter(new FileWriter(outs)); }
		void setFileIO(String ins, String outs) throws IOException { setFileIn(ins); setFileOut(outs); }

		private static int pos, readLen;
		private static final char[] buffer = new char[1024 * 8];
		private static char[] str = new char[500*8*2];
		private static boolean[] isDigit = new boolean[256];
		private static boolean[] isSpace = new boolean[256];
		private static boolean[] isLineSep = new boolean[256];

		static { for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; }
		public int read() throws IOException { if(pos >= readLen) { pos = 0; readLen = in.read(buffer); if(readLen <= 0) { throw new EndOfFileRuntimeException(); } } return buffer[pos++]; }
		public int nextInt() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }
		public long nextLong() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }
		public char nextChar() throws IOException { while(true) { final int c = read(); if(!isSpace[c]) { return (char)c; } } }
		int reads(int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } if(str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		int reads(char[] cs, int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } cs[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		public char[] nextLine() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isLineSep); try { if(str[len-1] == '\r') { len--; read(); } } catch(EndOfFileRuntimeException e) { ; } return Arrays.copyOf(str, len); }
		public String nextString() throws IOException { return new String(next()); }
		public char[] next() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); return Arrays.copyOf(str, len); }
//		public int next(char[] cs) throws IOException { int len = 0; cs[len++] = nextChar(); len = reads(cs, len, isSpace); return len; }
		public double nextDouble() throws IOException { return Double.parseDouble(nextString()); }
		public long[] nextLongArray(final int n) throws IOException { final long[] res = new long[n]; for(int i = 0; i < n; i++) { res[i] = nextLong(); } return res; }
		public int[] nextIntArray(final int n) throws IOException { final int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = nextInt(); } return res; }
		public int[][] nextIntArray2D(final int n, final int k) throws IOException { final int[][] res = new int[n][]; for(int i = 0; i < n; i++) { res[i] = nextIntArray(k); } return res; }
		public int[][] nextIntArray2DWithIndex(final int n, final int k) throws IOException { final int[][] res = new int[n][k+1]; for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { res[i][j] = nextInt(); } res[i][k] = i; } return res; }
		public double[] nextDoubleArray(final int n) throws IOException { final double[] res = new double[n]; for(int i = 0; i < n; i++) { res[i] = nextDouble(); } return res; }
	}
}