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)

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#include<math.h>
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
struct Point {
  double x;
  double y;
};
struct Point *p;
void qsort1(int left, int right);
void msort(int left, int right);
double closest_pair(int left, int right);
int main(void)
{
  int i, n;
  scanf("%d", &n);
  p = (struct Point*)malloc(sizeof(struct Point) * n);
  for (i = 0; i < n; i++) scanf("%lf %lf", &p[i].x, &p[i].y);
  qsort1(0, n - 1);
  printf("%.6f\n", closest_pair(0, n - 1));
  free(p);
}
void qsort1(int left, int right)
{
  int i, j;
  struct Point pivot;
  if (left >= right) return;
  i = left;
  j = right;
  pivot = p[(i + j) / 2];
  do {
    while (p[i].x < pivot.x) i++;
    while (p[j].x > pivot.x) j--;
    if (i <= j) {
      struct Point temp = p[i];
      p[i] = p[j];
      p[j] = temp;
      i++;
      j--;
    }
  } while(i <= j);
  qsort1(left, j);
  qsort1(i, right);
}
void msort(int left, int right)
{
  int middle, num, base, i, j;
  struct Point *work;
  middle = (left + right) / 2;
  work = (struct Point*)malloc(sizeof(struct Point) * (middle - left + 1));
  for (num = 0, i = left; i <= middle; i++) work[num++] = p[i];
  j = 0;
  base = left;
  while (i <= right && j < num) {
    if (p[i].y < work[j].y) p[base++] = p[i++];
    else p[base++] = work[j++];
  }
  while (j < num) p[base++] = work[j++];
  free(work);
}
double closest_pair(int left , int right)
{
  int middle, num, i, j;
  double x, d, dtemp;
  struct Point *work;
  if (left >= right) return INT_MAX;
  middle = (left + right) / 2;
  x = p[middle].x;
  d = closest_pair(left, middle);
  dtemp = closest_pair(middle + 1, right);
  msort(left, right);
  if (d > dtemp) d = dtemp;
  work = (struct Point*)malloc(sizeof(struct Point) * (right - left + 1));
  num = 0;
  for (i = 0; i < (right - left + 1); i++) {
    if (abs(p[i].x - x) >= d) continue;
    for (j = 0; j < num; j++) {
      double dx = p[i].x - work[num - 1 - j].x, dy = p[i].y - work[num - 1 - j].y;
      if (dy >= d) break;
      d = MIN(d, sqrt(dx * dx + dy * dy));
    }
    work[num++] = p[i];
  }
  free(work);
  return d;
}