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: Time Limit Exceeded)

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

namespace ConsoleApplication1
{
    class Program
    {
        static bool sorted;

        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]);
            }

            sorted = false;

            Array.Sort(P, (a, b) => (int)(a.x - b.x));
            Console.WriteLine("{0:f10}", ClosestPair(ref P, 0, n));
        }

        static double ClosestPair(ref Point[] P, int begin, int end)
        {
            int n = P.Length;

            if (end - begin <= 1) return double.MaxValue;

            int middle = begin + (end - begin) / 2;

            double x = P[middle].x;

            double leftD = ClosestPair(ref P, begin, middle);
            double rightD = ClosestPair(ref P, middle, end);
            double d = Math.Min(leftD, rightD);

            if (!sorted)
            {
                P = P.OrderBy(p => p.y).ToArray();
                sorted = true;
            }

            List<Point> temp = new List<Point>();

            for (int i = 0; i < n; i++)
            {
                if (Math.Abs(P[i].x - x) >= d) continue;

                for (int j = temp.Count - 1; j >= 0; j--)
                {
                    double dx = P[i].x - temp[j].x;
                    double dy = P[i].y - temp[j].y;

                    if (dy >= d) break;

                    d = Math.Min(d, Math.Sqrt(dx * dx + dy * dy));
                }
                temp.Add(P[i]);
            }
            return d;
        }
    }

    class Point
    {
        internal double x, y;

        public Point() { }

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