Problem p02303 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

const double EPS = 1e-8, PI = acos(-1);

inline bool eq(double a, double b) { return fabs(b - a) < EPS; }

using Point = complex<double>;
istream &operator>>(istream &is, Point &p) {
  double a, b;
  is >> a >> b;
  p = Point(a, b);
  return is;
}
ostream &operator<<(ostream &os, Point &p) {
  os << fixed << setprecision(10) << p.real() << " " << p.imag();
}

namespace std {
bool operator<(const Point &a, const Point &b) {
  return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
}
} // namespace std

Point rotate(double theta, const Point &p) {
  return Point(cos(theta) * p.real() - sin(theta) * p.imag(),
               sin(theta) * p.real() + cos(theta) * p.imag());
}

double cross(const Point &a, const Point &b) {
  return a.real() * b.imag() - a.imag() * b.real();
}

double dot(const Point &a, const Point &b) {
  return a.real() * b.real() + a.imag() * b.imag();
}

double RadianToDegree(double r) { return (r * 180.0 / acos(-1)); }

double DegreeToRadian(double d) { return (d * acos(-1) / 180.0); }

double GetAngle(const Point &a, const Point &b, const Point &c) {
  const Point v(b - a), w(c - b);
  double alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real());
  if (alpha > beta)
    swap(alpha, beta);
  double theta = (beta - alpha);
  return min(theta, 2 * acos(-1) - theta);
}

struct Line {
  Point a, b;

  Line(){};
  Line(Point a, Point b) : a(a), b(b){};

  // Ax + By = C
  Line(double A, double B, double C) {
    if (eq(A, 0)) {
      a = Point(0, C / B);
      b = Point(1, C / B);
    } else if (eq(B, 0)) {
      a = Point(C / A, 0);
      b = Point(C / A, 1);
    } else {
      a = Point(0, C / B);
      b = Point(C / A, 0);
    }
  }

  friend ostream &operator<<(ostream &os, Line &p) {
    return os << p.a << " to " << p.b;
  }

  friend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; }
};

struct Segment {
  Point a, b;

  Segment(){};
  Segment(Point a, Point b) : a(a), b(b){};

  friend ostream &operator<<(ostream &os, Segment &p) {
    return os << p.a << " to " << p.b;
  }

  friend istream &operator>>(istream &is, Segment &a) {
    return is >> a.a >> a.b;
  }
};

struct Circle {
  Point p;
  double r;

  Circle(){};

  Circle(Point p, double r) : p(p), r(r){};
};

using Polygon = vector<Point>;
using Points = vector<Point>;
using Segments = vector<Segment>;
using Lines = vector<Line>;
using Circles = vector<Circle>;
using PointPoint = pair<Point, Point>;

int ccw(const Point &a, Point b, Point c) {
  b = b - a, c = c - a;
  if (cross(b, c) > EPS)
    return +1; // "COUNTER_CLOCKWISE"
  if (cross(b, c) < -EPS)
    return -1; // "CLOCKWISE"
  if (dot(b, c) < 0)
    return +2; // "ONLINE_BACK"
  if (norm(b) < norm(c))
    return -2; // "ONLINE_FRONT"
  return 0;    // "ON_SEGMENT"
}

bool Parallel(const Line &a, const Line &b) {
  return abs(cross(a.b - a.a, b.b - b.a)) < EPS;
}

bool Orthogonal(const Line &a, const Line &b) {
  return abs(dot(a.a - a.b, b.a - b.b)) < EPS;
}

Point Projection(const Line &l, const Point &p) {
  double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
  return l.a + (l.a - l.b) * t;
}
Point Projection(const Segment &l, const Point &p) {
  double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
  return l.a + (l.a - l.b) * t;
}

Point Reflection(const Line &l, const Point &p) {
  return p + (Projection(l, p) - p) * 2.0;
}

double Distance(const Point &a, const Point &b) { return abs(a - b); }

double Distance(const Line &l, const Point &p) {
  return abs(p - Projection(l, p));
}

bool Intersect(const Line &l, const Line &m);
double Distance(const Line &l, const Line &m) {
  return Intersect(l, m) ? 0 : Distance(l, m.a);
}

bool Intersect(const Segment &s, const Point &p);
double Distance(const Segment &s, const Point &p) {
  Point r = Projection(s, p);
  if (Intersect(s, r))
    return abs(r - p);
  return min(abs(s.a - p), abs(s.b - p));
}

bool Intersect(const Segment &s, const Segment &t);
double Distance(const Segment &a, const Segment &b) {
  if (Intersect(a, b))
    return 0;
  return min(min(Distance(a, b.a), Distance(a, b.b)),
             min(Distance(b, a.a), Distance(b, a.b)));
}

bool Intersect(const Line &l, const Segment &s);
double Distance(const Line &l, const Segment &s) {
  if (Intersect(l, s))
    return 0;
  return min(Distance(l, s.a), Distance(l, s.b));
}

bool Intersect(const Line &l, const Point &p) {
  return abs(ccw(l.a, l.b, p)) != 1;
}

bool Intersect(const Line &l, const Line &m) {
  return abs(cross(l.b - l.a, m.b - m.a)) > EPS ||
         abs(cross(l.b - l.a, m.b - l.a)) < EPS;
}

bool Intersect(const Segment &s, const Point &p) {
  return ccw(s.a, s.b, p) == 0;
}

bool Intersect(const Line &l, const Segment &s) {
  return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;
}

bool Intersect(const Segment &s, const Segment &t) {
  return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
         ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}

double Distance(const Line &l, const Point &p);
bool Intersect(const Circle &c, const Line &l) {
  return Distance(l, c.p) <= c.r + EPS;
}

bool Intersect(const Circle &c, const Point &p) {
  return abs(abs(p - c.p) - c.r) < EPS;
}

int Intersect(const Circle &c, const Segment &l) {
  if (norm(Projection(l, c.p) - c.p) - c.r * c.r > EPS)
    return 0;
  const double d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);
  if (d1 < c.r + EPS && d2 < c.r + EPS)
    return 0;
  if (d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS)
    return 1;
  const Point h = Projection(l, c.p);
  if (dot(l.a - h, l.b - h) < 0)
    return 2;
  return 0;
}

Point Crosspoint(const Line &l, const Line &m) {
  double A = cross(l.b - l.a, m.b - m.a);
  double B = cross(l.b - l.a, l.b - m.a);
  if (abs(A) < EPS && abs(B) < EPS)
    return m.a;
  return m.a + (m.b - m.a) * B / A;
}

Point Crosspoint(const Segment &l, const Segment &m) {
  double A = cross(l.b - l.a, m.b - m.a);
  double B = cross(l.b - l.a, l.b - m.a);
  if (abs(A) < EPS && abs(B) < EPS)
    return m.a;
  return m.a + (m.b - m.a) * B / A;
}

PointPoint Crosspoint(const Circle &c, const Line l) {
  Point hp = Projection(l, c.p), h = hp - c.p;
  const double d2 = norm(h);
  Point v = (l.b - l.a) * sqrt(c.r * c.r - d2) / abs(l.b - l.a);
  return PointPoint(hp - v, hp + v);
}

PointPoint Crosspoint(const Circle &c, const Segment &l) {
  Line aa = Line(l.a, l.b);
  if (Intersect(c, l) == 2)
    return Crosspoint(c, aa);
  PointPoint ret = Crosspoint(c, aa);
  if (dot(l.a - ret.first, l.b - ret.first) < 0)
    ret.second = ret.first;
  else
    ret.first = ret.second;
  return ret;
}

PointPoint Crosspoint(const Circle &c1, const Circle &c2) {
  double d = abs(c1.p - c2.p);
  double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));
  double t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());
  Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);
  Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);
  return (PointPoint(p1, p2));
}

bool IsConvex(const Polygon &p) {
  int n = p.size();
  for (int i = 0; i < n; i++) {
    if (ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1)
      return false;
  }
  return true;
}

Polygon Convex_Hull(Polygon &p) {
  int n = p.size(), k = 0;
  if (n >= 3) {
    sort(p.begin(), p.end());
    vector<Point> ch(2 * n);
    for (int i = 0; i < n; ch[k++] = p[i++]) {
      while (k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)
        --k;
    }
    for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {
      while (k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)
        --k;
    }
    ch.resize(k - 1);
    return ch;
  } else {
    return p;
  }
}

enum { OUT, ON, IN };
int Contains(const Polygon &Q, const Point &p) {
  bool in = false;
  for (int i = 0; i < Q.size(); i++) {
    Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;
    if (a.imag() > b.imag())
      swap(a, b);
    if (a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0)
      in = !in;
    if (cross(a, b) == 0 && dot(a, b) <= 0)
      return ON;
  }
  return in ? IN : OUT;
}

bool MergeIfAble(Segment &s1, Segment s2) {
  if (abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS)
    return false;
  if (ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1)
    return false;
  if (ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2)
    return false;
  s1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));
  return true;
}

void MergeSegments(vector<Segment> &segs) {
  for (int i = 0; i < segs.size(); i++) {
    if (segs[i].b < segs[i].a)
      swap(segs[i].a, segs[i].b);
  }
  for (int i = 0; i < segs.size(); i++) {
    for (int j = i + 1; j < segs.size(); j++) {
      if (MergeIfAble(segs[i], segs[j])) {
        segs[j--] = segs.back(), segs.pop_back();
      }
    }
  }
}

vector<vector<int>> SegmentArrangement(vector<Segment> &segs,
                                       vector<Point> &ps) {
  vector<vector<int>> g;
  const int N = (int)segs.size();
  for (int i = 0; i < N; i++) {
    ps.emplace_back(segs[i].a);
    ps.emplace_back(segs[i].b);
    for (int j = i + 1; j < N; j++) {
      const Point p1 = segs[i].b - segs[i].a;
      const Point p2 = segs[j].b - segs[j].a;
      if (cross(p1, p2) == 0)
        continue;
      if (Intersect(segs[i], segs[j])) {
        ps.emplace_back(Crosspoint(segs[i], segs[j]));
      }
    }
  }
  sort(begin(ps), end(ps));
  ps.erase(unique(begin(ps), end(ps)), end(ps));

  const int M = (int)ps.size();
  g.resize(M);
  for (int i = 0; i < N; i++) {
    vector<int> vec;
    for (int j = 0; j < M; j++) {
      if (Intersect(segs[i], ps[j])) {
        vec.emplace_back(j);
      }
    }
    for (int j = 1; j < vec.size(); j++) {
      g[vec[j - 1]].push_back(vec[j]);
      g[vec[j]].push_back(vec[j - 1]);
    }
  }
  return (g);
}

Polygon ConvexCut(Polygon &U, Line l) {
  Polygon ret;
  for (int i = 0; i < U.size(); i++) {
    Point now = U[i], nxt = U[(i + 1) % U.size()];
    if (ccw(l.a, l.b, now) != -1)
      ret.push_back(now);
    if (ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) {
      ret.push_back(Crosspoint(Line(now, nxt), l));
    }
  }
  return (ret);
}

double Area2(const Polygon &p) {
  double A = 0;
  for (int i = 0; i < p.size(); ++i) {
    A += cross(p[i], p[(i + 1) % p.size()]);
  }
  return A;
}

double Convex_Diameter(Polygon &p) {
  int n = p.size();
  int is = 0, js = 0;
  for (int i = 1; i < n; i++) {
    if (p[i].imag() > p[is].imag())
      is = i;
    if (p[i].imag() < p[js].imag())
      js = i;
  }
  double maxdis = norm(p[is] - p[js]);

  int maxi, maxj, i, j;
  i = maxi = is;
  j = maxj = js;
  do {
    if (cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) >= 0) {
      j = (j + 1) % n;
    } else {
      i = (i + 1) % n;
    }
    if (norm(p[i] - p[j]) > maxdis) {
      maxdis = norm(p[i] - p[j]);
      maxi = i;
      maxj = j;
    }
  } while (i != is || j != js);
  return sqrt(maxdis);
}

PointPoint Closer(PointPoint a, PointPoint b) {
  return Distance(a.first, a.second) < Distance(b.first, b.second) ? a : b;
}
PointPoint ClosestPairUtil(Points &ps, int l, int r) {
  // cerr << "begin [" << l << ", " << r << ")" << endl;
  if (r - l <= 3) {
    // cerr << "[" << l << ", " << r << ")" << endl;
    PointPoint res(ps[l], ps[r - 1]);
    // cerr << "res : " << res.first << ", " << res.second << endl;
    for (int i = l; i < r; ++i) {
      for (int j = i + 1; j < r; ++j) {
        // cerr << i << ", " << j << endl;
        res = Closer(res, PointPoint(ps[i], ps[j]));
        // cerr << "res : " << res.first << " " << res.second << endl;
      }
    }
    // cerr << "[" << l << ", " << r << ") = " << Distance(res.first,
    // res.second) << endl;
    return res;
  }
  int mid = (l + r) / 2;
  Point midPoint = ps[mid];
  PointPoint res =
      Closer(ClosestPairUtil(ps, l, mid), ClosestPairUtil(ps, mid, r));
  double d = Distance(res.first, res.second);

  Points strip;
  for (int i = l; i < r; ++i) {
    // if (l == 43 && r == 50 && abs(ps[i].real() - midPoint.real()) < d) {
    //   cerr << i << " ";
    // }
    if (abs(ps[i].real() - midPoint.real()) < d)
      strip.push_back(ps[i]);
  }
  // if (l == 43 && r == 50) cerr << endl;

  for (int i = 0; i < strip.size(); ++i) {
    for (int j = i + 1; j < strip.size(); ++j) {
      if (Distance(strip[i], strip[j]) < d) {
        d = Distance(strip[i], strip[j]);
        res = PointPoint(strip[i], strip[j]);
      }
    }
  }

  // cerr << "[" << l << ", " << r << ") = " << Distance(res.first, res.second)
  // << endl;
  return res;
}

PointPoint ClosestPair(Points ps) {
  if (ps.size() <= 1)
    exit(-1);
  for (int i = 0; i < ps.size(); ++i) {
    ps[i] = rotate(0.1, ps[i]);
  }
  sort(ps.begin(), ps.end());

  if (ps.size() == 2)
    return PointPoint(ps[0], ps[1]);
  return ClosestPairUtil(ps, 0, ps.size());
}

// int main() {}

// Projection
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/1/CGL_1_A
// int main() {
//   Line l;
//   cin >> l;
//
//   int q;
//   cin >> q;
//   while (q--) {
//     Point p;
//     cin >> p;
//     Point ans = Projection(l, p);
//     cout << ans << endl;
//   }
// }

// Reflection
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/1/CGL_1_B
// int main() {
//   Line l;
//   cin >> l;
//
//   int q;
//   cin >> q;
//   while (q--) {
//     Point p;
//     cin >> p;
//     Point ans = Reflection(l, p);
//     cout << ans << endl;
//   }
// }

// ccw
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/1/CGL_1_C
// int main() {
//   const vector<string> ans{"ONLINE_FRONT", "CLOCKWISE", "ON_SEGMENT",
//   "COUNTER_CLOCKWISE", "ONLINE_BACK"}; Point p0, p1; cin >> p0 >> p1;
//
//   int q;
//   cin >> q;
//   while (q--) {
//     Point p2;
//     cin >> p2;
//     cout << ans[ccw(p0, p1, p2) + 2] << endl;
//   }
// }

// Parallel and Orthogonal
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/2/CGL_2_A
// int main() {
//   int n;
//   cin >> n;
//   while (n--) {
//     Line a, b;
//     cin >> a >> b;
//     if (Parallel(a, b))
//       puts("2");
//     else if (Orthogonal(a, b))
//       puts("1");
//     else
//       puts("0");
//   }
// }

// Intersect(S, S)
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/2/CGL_2_B
// int main() {
//   int q;
//   cin >> q;
//   while (q--) {
//     Segment a, b;
//     cin >> a >> b;
//     cout << Intersect(a, b) << endl;
//   }
// }

// Crosspoint(S, S)
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/2/CGL_2_C
// int main() {
//   int q;
//   cin >> q;
//   while (q--) {
//     Segment a, b;
//     cin >> a >> b;
//     Point ans = Crosspoint(a, b);
//     cout << ans << endl;
//   }
// }

// Distance(S, S);
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/2/CGL_2_D
// int main() {
//   int q;
//   cin >> q;
//   while (q--) {
//     Segment a, b;
//     cin >> a >> b;
//     cout << fixed << setprecision(10) << Distance(a, b) << endl;
//   }
// }

// Area2
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/3/CGL_3_A
// int main() {
//   int n;
//   cin >> n;
//   Polygon p(n);
//   for (auto &i : p) cin >> i;
//
//   cout << fixed << setprecision(1) << Area2(p) / 2.0 << endl;
// }

// IsConvex
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/3/CGL_3_B
// int main() {
//   int n;
//   cin >> n;
//   Polygon p(n);
//   for (auto &i : p) cin >> i;
//
//   cout << IsConvex(p) << endl;
// }

// Contains
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/3/CGL_3_C
// int main() {
//   int n;
//   cin >> n;
//   Polygon p(n);
//   for (auto &i : p) cin >> i;
//
//   int q;
//   cin >> q;
//   while (q--) {
//     Point a;
//     cin >> a;
//
//     cout << Contains(p, a) << endl;
//   }
// }

// Convex_Hull
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/4/CGL_4_A
// int main() {
//   int n;
//   cin >> n;
//   Polygon p;
//   for (int i = 0; i < n; ++i) {
//     int a, b;
//     cin >> a >> b;
//     p.push_back(Point(a, b));
//   }
//
//   p = Convex_Hull(p);
//   cout << p.size() << endl;
//
//   // 最悪
//   Point minp = p[0];
//   int idx = 0;
//   for (int i = 0; i < p.size(); ++i) {
//     if (p[i].imag() < minp.imag() ? true : p[i].imag() == minp.imag() &&
//     p[i].real() < minp.real()) {
//       minp = p[i];
//       idx = i;
//     }
//   }
//
//   for (int i = 0; i < p.size(); ++i) {
//     Point latte = p[(i + idx) % p.size()];
//     cout << latte.real() << " " << latte.imag() << endl;
//   }
// }

// Convex_Diameter
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/4/CGL_4_B
// int main() {
//   int n;
//   cin >> n;
//
//   Polygon p(n);
//   for (auto &i : p) cin >> i;
//
//   cout << fixed << setprecision(10) << Convex_Diameter(p) << endl;
// }

// ConvexCut
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/4/CGL_4_C
// int main() {
//   int n;
//   cin >> n;
//
//   Polygon p(n);
//   for (auto &i : p) cin >> i;
//
//   int q;
//   cin >> q;
//   while (q--) {
//     Line l;
//     cin >> l;
//
//     cout << fixed << setprecision(10) << Area2(ConvexCut(p, l)) / 2 << endl;
//   }
// }

// ClosestPair
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/5/CGL_5_A
int main() {
  int n;
  cin >> n;

  Points p(n);
  for (auto &i : p)
    cin >> i;
  sort(p.begin(), p.end());
  PointPoint pp = ClosestPair(p);
  cout << fixed << setprecision(10) << Distance(pp.first, pp.second) << endl;
  // cerr << pp.first << " " << pp.second << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

const double EPS = 1e-8, PI = acos(-1);

inline bool eq(double a, double b) { return fabs(b - a) < EPS; }

using Point = complex<double>;
istream &operator>>(istream &is, Point &p) {
  double a, b;
  is >> a >> b;
  p = Point(a, b);
  return is;
}
ostream &operator<<(ostream &os, Point &p) {
  os << fixed << setprecision(10) << p.real() << " " << p.imag();
}

namespace std {
bool operator<(const Point &a, const Point &b) {
  return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
}
} // namespace std

double cross(const Point &a, const Point &b) {
  return a.real() * b.imag() - a.imag() * b.real();
}

double dot(const Point &a, const Point &b) {
  return a.real() * b.real() + a.imag() * b.imag();
}

double RadianToDegree(double r) { return (r * 180.0 / acos(-1)); }

double DegreeToRadian(double d) { return (d * acos(-1) / 180.0); }

double GetAngle(const Point &a, const Point &b, const Point &c) {
  const Point v(b - a), w(c - b);
  double alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real());
  if (alpha > beta)
    swap(alpha, beta);
  double theta = (beta - alpha);
  return min(theta, 2 * acos(-1) - theta);
}

struct Line {
  Point a, b;

  Line(){};
  Line(Point a, Point b) : a(a), b(b){};

  // Ax + By = C
  Line(double A, double B, double C) {
    if (eq(A, 0)) {
      a = Point(0, C / B);
      b = Point(1, C / B);
    } else if (eq(B, 0)) {
      a = Point(C / A, 0);
      b = Point(C / A, 1);
    } else {
      a = Point(0, C / B);
      b = Point(C / A, 0);
    }
  }

  friend ostream &operator<<(ostream &os, Line &p) {
    return os << p.a << " to " << p.b;
  }

  friend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; }
};

struct Segment {
  Point a, b;

  Segment(){};
  Segment(Point a, Point b) : a(a), b(b){};

  friend ostream &operator<<(ostream &os, Segment &p) {
    return os << p.a << " to " << p.b;
  }

  friend istream &operator>>(istream &is, Segment &a) {
    return is >> a.a >> a.b;
  }
};

struct Circle {
  Point p;
  double r;

  Circle(){};

  Circle(Point p, double r) : p(p), r(r){};
};

using Polygon = vector<Point>;
using Points = vector<Point>;
using Segments = vector<Segment>;
using Lines = vector<Line>;
using Circles = vector<Circle>;
using PointPoint = pair<Point, Point>;

int ccw(const Point &a, Point b, Point c) {
  b = b - a, c = c - a;
  if (cross(b, c) > EPS)
    return +1; // "COUNTER_CLOCKWISE"
  if (cross(b, c) < -EPS)
    return -1; // "CLOCKWISE"
  if (dot(b, c) < 0)
    return +2; // "ONLINE_BACK"
  if (norm(b) < norm(c))
    return -2; // "ONLINE_FRONT"
  return 0;    // "ON_SEGMENT"
}

bool Parallel(const Line &a, const Line &b) {
  return abs(cross(a.b - a.a, b.b - b.a)) < EPS;
}

bool Orthogonal(const Line &a, const Line &b) {
  return abs(dot(a.a - a.b, b.a - b.b)) < EPS;
}

Point Projection(const Line &l, const Point &p) {
  double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
  return l.a + (l.a - l.b) * t;
}
Point Projection(const Segment &l, const Point &p) {
  double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
  return l.a + (l.a - l.b) * t;
}

Point Reflection(const Line &l, const Point &p) {
  return p + (Projection(l, p) - p) * 2.0;
}

double Distance(const Point &a, const Point &b) { return abs(a - b); }

double Distance(const Line &l, const Point &p) {
  return abs(p - Projection(l, p));
}

bool Intersect(const Line &l, const Line &m);
double Distance(const Line &l, const Line &m) {
  return Intersect(l, m) ? 0 : Distance(l, m.a);
}

bool Intersect(const Segment &s, const Point &p);
double Distance(const Segment &s, const Point &p) {
  Point r = Projection(s, p);
  if (Intersect(s, r))
    return abs(r - p);
  return min(abs(s.a - p), abs(s.b - p));
}

bool Intersect(const Segment &s, const Segment &t);
double Distance(const Segment &a, const Segment &b) {
  if (Intersect(a, b))
    return 0;
  return min(min(Distance(a, b.a), Distance(a, b.b)),
             min(Distance(b, a.a), Distance(b, a.b)));
}

bool Intersect(const Line &l, const Segment &s);
double Distance(const Line &l, const Segment &s) {
  if (Intersect(l, s))
    return 0;
  return min(Distance(l, s.a), Distance(l, s.b));
}

bool Intersect(const Line &l, const Point &p) {
  return abs(ccw(l.a, l.b, p)) != 1;
}

bool Intersect(const Line &l, const Line &m) {
  return abs(cross(l.b - l.a, m.b - m.a)) > EPS ||
         abs(cross(l.b - l.a, m.b - l.a)) < EPS;
}

bool Intersect(const Segment &s, const Point &p) {
  return ccw(s.a, s.b, p) == 0;
}

bool Intersect(const Line &l, const Segment &s) {
  return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;
}

bool Intersect(const Segment &s, const Segment &t) {
  return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
         ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}

double Distance(const Line &l, const Point &p);
bool Intersect(const Circle &c, const Line &l) {
  return Distance(l, c.p) <= c.r + EPS;
}

bool Intersect(const Circle &c, const Point &p) {
  return abs(abs(p - c.p) - c.r) < EPS;
}

int Intersect(const Circle &c, const Segment &l) {
  if (norm(Projection(l, c.p) - c.p) - c.r * c.r > EPS)
    return 0;
  const double d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);
  if (d1 < c.r + EPS && d2 < c.r + EPS)
    return 0;
  if (d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS)
    return 1;
  const Point h = Projection(l, c.p);
  if (dot(l.a - h, l.b - h) < 0)
    return 2;
  return 0;
}

Point Crosspoint(const Line &l, const Line &m) {
  double A = cross(l.b - l.a, m.b - m.a);
  double B = cross(l.b - l.a, l.b - m.a);
  if (abs(A) < EPS && abs(B) < EPS)
    return m.a;
  return m.a + (m.b - m.a) * B / A;
}

Point Crosspoint(const Segment &l, const Segment &m) {
  double A = cross(l.b - l.a, m.b - m.a);
  double B = cross(l.b - l.a, l.b - m.a);
  if (abs(A) < EPS && abs(B) < EPS)
    return m.a;
  return m.a + (m.b - m.a) * B / A;
}

PointPoint Crosspoint(const Circle &c, const Line l) {
  Point hp = Projection(l, c.p), h = hp - c.p;
  const double d2 = norm(h);
  Point v = (l.b - l.a) * sqrt(c.r * c.r - d2) / abs(l.b - l.a);
  return PointPoint(hp - v, hp + v);
}

PointPoint Crosspoint(const Circle &c, const Segment &l) {
  Line aa = Line(l.a, l.b);
  if (Intersect(c, l) == 2)
    return Crosspoint(c, aa);
  PointPoint ret = Crosspoint(c, aa);
  if (dot(l.a - ret.first, l.b - ret.first) < 0)
    ret.second = ret.first;
  else
    ret.first = ret.second;
  return ret;
}

PointPoint Crosspoint(const Circle &c1, const Circle &c2) {
  double d = abs(c1.p - c2.p);
  double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));
  double t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());
  Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);
  Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);
  return (PointPoint(p1, p2));
}

bool IsConvex(const Polygon &p) {
  int n = p.size();
  for (int i = 0; i < n; i++) {
    if (ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1)
      return false;
  }
  return true;
}

Polygon Convex_Hull(Polygon &p) {
  int n = p.size(), k = 0;
  if (n >= 3) {
    sort(p.begin(), p.end());
    vector<Point> ch(2 * n);
    for (int i = 0; i < n; ch[k++] = p[i++]) {
      while (k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)
        --k;
    }
    for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) {
      while (k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0)
        --k;
    }
    ch.resize(k - 1);
    return ch;
  } else {
    return p;
  }
}

enum { OUT, ON, IN };
int Contains(const Polygon &Q, const Point &p) {
  bool in = false;
  for (int i = 0; i < Q.size(); i++) {
    Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p;
    if (a.imag() > b.imag())
      swap(a, b);
    if (a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0)
      in = !in;
    if (cross(a, b) == 0 && dot(a, b) <= 0)
      return ON;
  }
  return in ? IN : OUT;
}

bool MergeIfAble(Segment &s1, Segment s2) {
  if (abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS)
    return false;
  if (ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1)
    return false;
  if (ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2)
    return false;
  s1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b));
  return true;
}

void MergeSegments(vector<Segment> &segs) {
  for (int i = 0; i < segs.size(); i++) {
    if (segs[i].b < segs[i].a)
      swap(segs[i].a, segs[i].b);
  }
  for (int i = 0; i < segs.size(); i++) {
    for (int j = i + 1; j < segs.size(); j++) {
      if (MergeIfAble(segs[i], segs[j])) {
        segs[j--] = segs.back(), segs.pop_back();
      }
    }
  }
}

vector<vector<int>> SegmentArrangement(vector<Segment> &segs,
                                       vector<Point> &ps) {
  vector<vector<int>> g;
  const int N = (int)segs.size();
  for (int i = 0; i < N; i++) {
    ps.emplace_back(segs[i].a);
    ps.emplace_back(segs[i].b);
    for (int j = i + 1; j < N; j++) {
      const Point p1 = segs[i].b - segs[i].a;
      const Point p2 = segs[j].b - segs[j].a;
      if (cross(p1, p2) == 0)
        continue;
      if (Intersect(segs[i], segs[j])) {
        ps.emplace_back(Crosspoint(segs[i], segs[j]));
      }
    }
  }
  sort(begin(ps), end(ps));
  ps.erase(unique(begin(ps), end(ps)), end(ps));

  const int M = (int)ps.size();
  g.resize(M);
  for (int i = 0; i < N; i++) {
    vector<int> vec;
    for (int j = 0; j < M; j++) {
      if (Intersect(segs[i], ps[j])) {
        vec.emplace_back(j);
      }
    }
    for (int j = 1; j < vec.size(); j++) {
      g[vec[j - 1]].push_back(vec[j]);
      g[vec[j]].push_back(vec[j - 1]);
    }
  }
  return (g);
}

Polygon ConvexCut(Polygon &U, Line l) {
  Polygon ret;
  for (int i = 0; i < U.size(); i++) {
    Point now = U[i], nxt = U[(i + 1) % U.size()];
    if (ccw(l.a, l.b, now) != -1)
      ret.push_back(now);
    if (ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) {
      ret.push_back(Crosspoint(Line(now, nxt), l));
    }
  }
  return (ret);
}

double Area2(const Polygon &p) {
  double A = 0;
  for (int i = 0; i < p.size(); ++i) {
    A += cross(p[i], p[(i + 1) % p.size()]);
  }
  return A;
}

double Convex_Diameter(Polygon &p) {
  int n = p.size();
  int is = 0, js = 0;
  for (int i = 1; i < n; i++) {
    if (p[i].imag() > p[is].imag())
      is = i;
    if (p[i].imag() < p[js].imag())
      js = i;
  }
  double maxdis = norm(p[is] - p[js]);

  int maxi, maxj, i, j;
  i = maxi = is;
  j = maxj = js;
  do {
    if (cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) >= 0) {
      j = (j + 1) % n;
    } else {
      i = (i + 1) % n;
    }
    if (norm(p[i] - p[j]) > maxdis) {
      maxdis = norm(p[i] - p[j]);
      maxi = i;
      maxj = j;
    }
  } while (i != is || j != js);
  return sqrt(maxdis);
}

PointPoint Closer(PointPoint a, PointPoint b) {
  return Distance(a.first, a.second) < Distance(b.first, b.second) ? a : b;
}
PointPoint ClosestPairUtil(Points &ps, int l, int r) {
  // cerr << "begin [" << l << ", " << r << ")" << endl;
  if (r - l <= 3) {
    // cerr << "[" << l << ", " << r << ")" << endl;
    PointPoint res(ps[l], ps[r - 1]);
    // cerr << "res : " << res.first << ", " << res.second << endl;
    for (int i = l; i < r; ++i) {
      for (int j = i + 1; j < r; ++j) {
        // cerr << i << ", " << j << endl;
        res = Closer(res, PointPoint(ps[i], ps[j]));
        // cerr << "res : " << res.first << " " << res.second << endl;
      }
    }
    // cerr << "[" << l << ", " << r << ") = " << Distance(res.first,
    // res.second) << endl;
    return res;
  }
  int mid = (l + r) / 2;
  Point midPoint = ps[mid];
  PointPoint res =
      Closer(ClosestPairUtil(ps, l, mid), ClosestPairUtil(ps, mid, r));
  double d = Distance(res.first, res.second);

  Points strip;
  for (int i = l; i < r; ++i) {
    // if (l == 43 && r == 50 && abs(ps[i].real() - midPoint.real()) < d) {
    //   cerr << i << " ";
    // }
    if (abs(ps[i].real() - midPoint.real()) < d)
      strip.push_back(ps[i]);
  }
  // if (l == 43 && r == 50) cerr << endl;

  for (int i = 0; i < strip.size(); ++i) {
    for (int j = i + 1; j < strip.size(); ++j) {
      if (Distance(strip[i], strip[j]) < d) {
        d = Distance(strip[i], strip[j]);
        res = PointPoint(strip[i], strip[j]);
      }
    }
  }

  // cerr << "[" << l << ", " << r << ") = " << Distance(res.first, res.second)
  // << endl;
  return res;
}

PointPoint ClosestPair(Points ps) {
  if (ps.size() <= 1)
    exit(-1);
  sort(ps.begin(), ps.end());

  if (ps.size() == 2)
    return PointPoint(ps[0], ps[1]);
  return ClosestPairUtil(ps, 0, ps.size());
}

// int main() {}

// Projection
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/1/CGL_1_A
// int main() {
//   Line l;
//   cin >> l;
//
//   int q;
//   cin >> q;
//   while (q--) {
//     Point p;
//     cin >> p;
//     Point ans = Projection(l, p);
//     cout << ans << endl;
//   }
// }

// Reflection
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/1/CGL_1_B
// int main() {
//   Line l;
//   cin >> l;
//
//   int q;
//   cin >> q;
//   while (q--) {
//     Point p;
//     cin >> p;
//     Point ans = Reflection(l, p);
//     cout << ans << endl;
//   }
// }

// ccw
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/1/CGL_1_C
// int main() {
//   const vector<string> ans{"ONLINE_FRONT", "CLOCKWISE", "ON_SEGMENT",
//   "COUNTER_CLOCKWISE", "ONLINE_BACK"}; Point p0, p1; cin >> p0 >> p1;
//
//   int q;
//   cin >> q;
//   while (q--) {
//     Point p2;
//     cin >> p2;
//     cout << ans[ccw(p0, p1, p2) + 2] << endl;
//   }
// }

// Parallel and Orthogonal
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/2/CGL_2_A
// int main() {
//   int n;
//   cin >> n;
//   while (n--) {
//     Line a, b;
//     cin >> a >> b;
//     if (Parallel(a, b))
//       puts("2");
//     else if (Orthogonal(a, b))
//       puts("1");
//     else
//       puts("0");
//   }
// }

// Intersect(S, S)
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/2/CGL_2_B
// int main() {
//   int q;
//   cin >> q;
//   while (q--) {
//     Segment a, b;
//     cin >> a >> b;
//     cout << Intersect(a, b) << endl;
//   }
// }

// Crosspoint(S, S)
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/2/CGL_2_C
// int main() {
//   int q;
//   cin >> q;
//   while (q--) {
//     Segment a, b;
//     cin >> a >> b;
//     Point ans = Crosspoint(a, b);
//     cout << ans << endl;
//   }
// }

// Distance(S, S);
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/2/CGL_2_D
// int main() {
//   int q;
//   cin >> q;
//   while (q--) {
//     Segment a, b;
//     cin >> a >> b;
//     cout << fixed << setprecision(10) << Distance(a, b) << endl;
//   }
// }

// Area2
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/3/CGL_3_A
// int main() {
//   int n;
//   cin >> n;
//   Polygon p(n);
//   for (auto &i : p) cin >> i;
//
//   cout << fixed << setprecision(1) << Area2(p) / 2.0 << endl;
// }

// IsConvex
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/3/CGL_3_B
// int main() {
//   int n;
//   cin >> n;
//   Polygon p(n);
//   for (auto &i : p) cin >> i;
//
//   cout << IsConvex(p) << endl;
// }

// Contains
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/3/CGL_3_C
// int main() {
//   int n;
//   cin >> n;
//   Polygon p(n);
//   for (auto &i : p) cin >> i;
//
//   int q;
//   cin >> q;
//   while (q--) {
//     Point a;
//     cin >> a;
//
//     cout << Contains(p, a) << endl;
//   }
// }

// Convex_Hull
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/4/CGL_4_A
// int main() {
//   int n;
//   cin >> n;
//   Polygon p;
//   for (int i = 0; i < n; ++i) {
//     int a, b;
//     cin >> a >> b;
//     p.push_back(Point(a, b));
//   }
//
//   p = Convex_Hull(p);
//   cout << p.size() << endl;
//
//   // 最悪
//   Point minp = p[0];
//   int idx = 0;
//   for (int i = 0; i < p.size(); ++i) {
//     if (p[i].imag() < minp.imag() ? true : p[i].imag() == minp.imag() &&
//     p[i].real() < minp.real()) {
//       minp = p[i];
//       idx = i;
//     }
//   }
//
//   for (int i = 0; i < p.size(); ++i) {
//     Point latte = p[(i + idx) % p.size()];
//     cout << latte.real() << " " << latte.imag() << endl;
//   }
// }

// Convex_Diameter
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/4/CGL_4_B
// int main() {
//   int n;
//   cin >> n;
//
//   Polygon p(n);
//   for (auto &i : p) cin >> i;
//
//   cout << fixed << setprecision(10) << Convex_Diameter(p) << endl;
// }

// ConvexCut
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/4/CGL_4_C
// int main() {
//   int n;
//   cin >> n;
//
//   Polygon p(n);
//   for (auto &i : p) cin >> i;
//
//   int q;
//   cin >> q;
//   while (q--) {
//     Line l;
//     cin >> l;
//
//     cout << fixed << setprecision(10) << Area2(ConvexCut(p, l)) / 2 << endl;
//   }
// }

// ClosestPair
// verified https://onlinejudge.u-aizu.ac.jp/#/courses/library/4/CGL/5/CGL_5_A
int main() {
  int n;
  cin >> n;

  Points p(n);
  for (auto &i : p)
    cin >> i;
  sort(p.begin(), p.end());
  PointPoint pp = ClosestPair(p);
  cout << fixed << setprecision(10) << Distance(pp.first, pp.second) << endl;
  // cerr << pp.first << " " << pp.second << endl;
}