Problem:
Closest Pair

For given n points in metric space, find the distance of the closest points.

Input

n
x0 y0
x1 y1
:
xn-1 yn-1

The first integer n is the number of points.

In the following n lines, the coordinate of the i-th point is given by two real numbers xi and yi. Each value is a real number with at most 6 digits after the decimal point.

Output

Print the distance in a line. The output values should be in a decimal fraction with an error less than 0.000001.

Constraints

2 ≤ n ≤ 100,000

-100 ≤ x, y ≤ 100

Sample Input 1

2
0.0 0.0
1.0 0.0

Sample Output 1

1.000000

Sample Input 2

3
0.0 0.0
2.0 0.0
1.0 1.0

Sample Output 2

1.41421356237

Solution in C# (judge verdict: Accepted)

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
//using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        int N = Reader.Int();
        var P = new Point[N];
        for (int i = 0; i < N; i++)
            P[i] = new Point(Reader.Double(), Reader.Double());
        Console.WriteLine(ClosestPair(P).ToString("f9"));
        Console.ReadLine();
    }

    public double ClosestPair(Point[] P)
    {
        Array.Sort(P);
        return ClosestPairRec(P, 0, P.Length);
    }
    private double ClosestPairRec(Point[] P, int L, int R)
    {
        if (R - L <= 1) return double.MaxValue;
        int mid = L + (R - L) / 2;
        double midX = P[mid].X;
        double minDist = ClosestPairRec(P, L, mid);
        minDist = Math.Min(minDist, ClosestPairRec(P, mid, R));
        InplaceMerge(P, L, mid, R);

        var B = new Point[R - L];
        var Bnum = 0;
        for (int ai = L; ai < R; ai++)
        {
            if (Math.Abs(P[ai].X - midX) >= minDist) continue;
            for (int bi = Bnum - 1; bi >= 0; bi--)
            {
                double dy = P[ai].Y - B[bi].Y;
                if (dy >= minDist) break;
                double dx = P[ai].X - B[bi].X;
                minDist = Math.Min(minDist, Math.Sqrt(dx * dx + dy * dy));
            }
            B[Bnum++] = P[ai];
        }

        return minDist;
    }
    private void InplaceMerge(Point[] P, int L, int mid, int R)
    {
        Point[] res = new Point[R - L];
        int ai = L, bi = mid;
        for (int ni = 0; ni < res.Length; ni++)
            if (ai < mid && (bi == R || P[ai].Y < P[bi].Y)) res[ni] = P[ai++];
            else res[ni] = P[bi++];
        Array.Copy(res, 0, P, L, res.Length);
    }
    public struct Point : IEquatable<Point>, IComparable<Point>
    {
        public static readonly double Eps = 1e-10;
        public double X, Y;

        public Point(double x, double y) { X = x; Y = y; ; }

        public static Point operator +(Point a, Point b)
        {
            return new Point(Add(a.X, b.X), Add(a.Y, b.Y));
        }
        public static Point operator -(Point a, Point b)
        {
            return new Point(Add(a.X, -b.X), Add(a.Y, -b.Y));
        }
        public static Point operator *(double d, Point p)
        {
            return new Point(p.X * d, p.Y * d);
        }
        public static Point operator *(Point p, double d) { return d * p; }
        public double Norm { get { return Math.Sqrt(Add(X * X, Y * Y)); } }
        public double Distance(Point b) { return (b - this).Norm; }
        public double Dot(Point b) { return Add(X * b.X, Y * b.Y); }
        public double Det(Point b) { return Add(X * b.Y, -Y * b.X); }
        public bool Equals(Point b) { return X == b.X && Y == b.Y; }
        public override bool Equals(object obj) { return Equals((Point)obj); }
        public static bool operator ==(Point a, Point b) { return a.Equals(b); }
        public static bool operator !=(Point a, Point b) { return !a.Equals(b); }
        public int CompareTo(Point b)
        {
            if (Add(X, -b.X) == 0) return Math.Sign(Y - b.Y);
            return Math.Sign(X - b.X);
        }
        public override int GetHashCode() { return (int)(X * 100000 + Y); }
        public override string ToString() { return X + " " + Y; }

        private static double Add(double a, double b)
        {
            if (Math.Abs(a + b) < Eps * (Math.Abs(a) + Math.Abs(b))) return 0;
            return a + b;
        }
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    private static TextReader reader = Console.In;
    private static readonly char[] separator = { ' ' };
    private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    private static string[] A = new string[0];
    private static int i;
    private static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); }
    public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
    public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); }
    public static string Line() { return reader.ReadLine().Trim(); }
    private static string[] Split(string s) { return s.Split(separator, op); }
    private static string Next() { CheckNext(); return A[i++]; }
    private static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}