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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            Point[] P = new Point[n];

            for (int i = 0; i < n; i++)
            {
                double[] input = Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse);
                P[i] = new Point(input[0], input[1]);
            }
            Point[] res = ClosestPair(P);

            Console.WriteLine("{0:f10}", res[0].Distance(res[1]));
        }

        static Point[] ClosestPair(IEnumerable<Point> points)
        {
            var closestPair = new Point[2];

            var sortedX = new List<Point>();
            sortedX.AddRange(points);
            sortedX.Sort(new ComparerX());

            var sortedY = new List<Point>();
            sortedY.AddRange(points);
            sortedY.Sort(new ComparerX());

            double crtMinDist = double.MaxValue / 2;

            int xIndex = 0;
            int yIndex = 0;
            var candidates = new HashSet<Point>();

            foreach (var current in sortedX)
            {
                while (Math.Abs(current.x - sortedX[xIndex].x) > crtMinDist)
                {
                    candidates.Remove(sortedX[xIndex]);
                    xIndex++;
                }
                
                while (Math.Abs(current.y - sortedY[yIndex].y) > crtMinDist)
                {
                    candidates.Remove(sortedY[yIndex]);
                    yIndex++;
                }
                
                var subset = candidates.Where(p => p.y > checked(current.y - crtMinDist) && p.y < checked(current.y + crtMinDist)).ToArray();
                
                foreach (var point in subset)
                {
                    var distance = current.Distance(point);
                    
                    if (distance < crtMinDist)
                    {
                        crtMinDist = distance;
                        closestPair[0] = current;
                        closestPair[1] = point;
                    }
                }
                candidates.Add(current);
            }
            return closestPair;
        }
    }

    class Point : IComparable
    {
        static readonly double EPS = 1E-16;
        static int _counter;

        internal int Id;
        internal double x, y;

        public Point(double x, double y)
        {
            Id = ++_counter;
            this.x = x;
            this.y = y;
        }

        public double Abs { get { return Math.Sqrt(Norm); } }
        public double Norm { get { return x * x + y * y; } }

        public double Dot(Point p) { return x * p.x + y * p.y; }
        public double Cross(Point p) { return x * p.y - y * p.x; }
        public double Distance(Point p) { return (this - p).Abs; }

        public static Point operator +(Point a, Point b) { return new Point(a.x + b.x, a.y + b.y); }
        public static Point operator -(Point a, Point b) { return new Point(a.x - b.x, a.y - b.y); }
        public static Point operator *(Point p, double d) { return new Point(p.x * d, p.y * d); }
        public static Point operator /(Point p, double d) { return new Point(p.x / d, p.y / d); }
        public static bool operator <(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x < b.x : a.y < b.y; }
        public static bool operator >(Point a, Point b) { return Math.Abs(a.x - b.x) > EPS ? a.x > b.x : a.y > b.y; }
        public static bool operator ==(Point a, Point b) { return Math.Abs(a.x - b.x) < EPS && Math.Abs(a.y - b.y) < EPS; }
        public static bool operator !=(Point a, Point b) { return Math.Abs(a.x - b.x) >= EPS || Math.Abs(a.y - b.y) >= EPS; }


        public int CompareTo(object obj)
        {
            var other = obj as Point;
            if (other == null) return -1;

            ComparerX compX = new ComparerX();
            return compX.Compare(this, other);
        }

        public override bool Equals(object obj)
        {
            var other = obj as Point;

            return Id == other.Id;
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        public override string ToString()
        {
            return string.Format("{0} {1}", x, y);
        }
    }

    class ComparerX : IComparer<Point>
    {
        static readonly double EPS = 1E-10;

        public int Compare(Point a, Point b)
        {
            if (a.x - b.x < -EPS) return -1;
            if (a.x - b.x > EPS) return 1;
            if (a.y - b.y < -EPS) return -1;
            if (a.y - b.y > EPS) return 1;
            return 0;
        }
    }

    class ComparerY : IComparer<Point>
    {
        static readonly double EPS = 1E-10;

        public int Compare(Point a, Point b)
        {
            if (a.y - b.y < -EPS) return -1;
            if (a.y - b.y > EPS) return 1;
            if (a.x - b.x < -EPS) return -1;
            if (a.x - b.x > EPS) return 1;
            return 0;
        }
    }

    class Segment
    {
        internal Point p1, p2;

        public Segment(Point p1, Point p2)
        {
            this.p1 = p1;
            this.p2 = p2;
        }
    }

    class Circle
    {
        internal Point p;
        internal double r;

        public Circle(Point p, double r)
        {
            this.p = p;
            this.r = r;
        }
    }
}