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 Ruby (judge verdict: Time Limit Exceeded)

INF = 1.0 / 0.0

def dist(p1, p2)
  return Math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)
end

def bruteForce(px, n)
  min = INF
  n.times{|i|
    (i+1).upto(n-1){|j|
      d = dist(px[i], px[j])
      min = d if d < min
    }
  }
  return min
end

def strip_closest(strip, size, d)
  min = d

  size.times{|i|
    (i+1).upto(size-1){|j|
      break if strip[j][1] - strip[i][1] >= min
      dd = dist(strip[i], strip[j])
      min = dd if dd < min
    }
  }
  return min
end

def closest_util(px, py, n)
#  p [px, py, n]
  return bruteForce(px, n) if n <= 3

  mid = n/2 - 1
  midpoint = px[mid]

  pyl = []
  pyr = []
  n.times{|i|
    if py[i][0] <= midpoint[0]
      pyl << py[i]
    else
      pyr << py[i]
    end
  }

  d = INF
  if pyl.size == n
    d = bruteForce(px, n)
  else
    pxl = px[0,pyl.size].clone
    pxr = px[pyl.size, pyr.size].clone

    dl = closest_util(pxl, pyl, pyl.size)
    dr = closest_util(pxr, pyr, pyr.size)

    d = [dl, dr].min
  end

  strip = []
  n.times{|i|
    if (py[i][0] - midpoint[0]).abs < d
      strip << py[i]
    end
  }

  return [d, strip_closest(strip, strip.size, d)].min

end

n = gets.to_i
px = []
xra = [INF, -INF]
yra = [INF, -INF]
n.times{|i|
  px[i] = gets.split.map(&:to_f)
  xra[0] = px[i][0] if xra[0] > px[i][0]
  yra[0] = px[i][1] if yra[0] > px[i][1]
  xra[1] = px[i][0] if xra[1] < px[i][0]
  yra[1] = px[i][1] if yra[1] < px[i][1]
}

if xra[0] == xra[1] || yra[0] == yra[1]
  theta = Math.atan2(1, 1);
  ct = Math.cos(theta)
  st = Math.sin(theta)
  n.times{|i|
    x, y = px[i]
    px[i] = [x*ct-y*st, x*st+y*ct]
  }
end

px.sort!
py = px.map(&:clone)
py.sort! {|a, b| a[1] <=> b[1]}

puts "%.10f" % closest_util(px, py, px.size)