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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

const double EPS = 1e-8;

int dcmp(double d) {
  if (fabs(d) < EPS) {
    return 0;
  } else {
    return d < 0 ? -1 : 1;
  }
}

struct Vector {
  double x, y;
  Vector(double x = 0, double y = 0) : x(x), y(y) {}

  bool operator<(const Vector &other) const {
    return dcmp(x - other.x) < 0 ||
           (dcmp(x - other.x) == 0 && dcmp(y - other.y) < 0);
  }
};

typedef Vector Point;

double dist(const Point &a, const Point &b) {
  return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

bool cmpY(const Point &a, const Point &b) {
  return dcmp(a.y - b.y) < 0 || (dcmp(a.y - b.y) == 0 && dcmp(a.x - b.x) < 0);
}

double combine(vector<Point> &p, int l, int m, int r, double leftSmall,
               double rightSmall) {
  double d = min(leftSmall, rightSmall);
  double x = (p.at(m).x + p.at(m + 1).x) / 2;

  vector<Point> strip;

  for (int i = m; i >= l && dcmp(x - p.at(i).x - d) <= 0; i--) {
    strip.push_back(p.at(i));
  }
  for (int j = m + 1; j <= r && dcmp(p.at(j).x - x - d) <= 0; j++) {
    strip.push_back(p.at(j));
  }

  sort(strip.begin(), strip.end(), cmpY);

  int size = strip.size();

  for (int i = 0; i < size; i++) {
    for (int j = i + 1; j < size && dcmp(strip[j].y - strip[i].y - d) < 0;
         j++) {
      if (dcmp(dist(strip[i], strip[j]) - d) < 0) {
        d = dist(strip[i], strip[j]);
      }
    }
  }
  return d;
}

// naive n^2 method
double combine1(vector<Point> &p, int l, int m, int r, double leftSmall,
                double rightSmall) {
  double d = min(leftSmall, rightSmall);
  double x = (p.at(m).x + p.at(m + 1).x) / 2;

  double best = DBL_MAX;

  for (int i = m; i >= l && dcmp(x - p.at(i).x - d) <= 0; i--) {
    for (int j = m + 1; j <= r && dcmp(p.at(j).x - x - d) <= 0; j++) {
      best = min(best, dist(p.at(i), p.at(j)));
    }
  }
  return min(best, d);
}

double solve(vector<Point> &p, int l, int r) {
  if (l >= r) {
    return DBL_MAX;
  }
  int m = (l + r) / 2;

  double leftSmall = solve(p, l, m);
  double rightSmall = solve(p, m + 1, r);

  return combine(p, l, m, r, leftSmall, rightSmall);
}

int main() {
  int n;
  cin >> n;
  vector<Point> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i].x >> p[i].y;
  }

  sort(p.begin(), p.end());

  double ans = solve(p, 0, n - 1);

  cout << fixed << setprecision(8) << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

const double EPS = 1e-8;

int dcmp(double d) {
  if (fabs(d) < EPS) {
    return 0;
  } else {
    return d < 0 ? -1 : 1;
  }
}

struct Vector {
  double x, y;
  Vector(double x = 0, double y = 0) : x(x), y(y) {}

  bool operator<(const Vector &other) const {
    return dcmp(x - other.x) < 0 ||
           (dcmp(x - other.x) == 0 && dcmp(y - other.y) < 0);
  }
};

typedef Vector Point;

double dist(const Point &a, const Point &b) {
  return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

double combine(vector<Point> &p, int l, int m, int r, double leftSmall,
               double rightSmall) {
  double d = min(leftSmall, rightSmall);
  double x = (p.at(m).x + p.at(m + 1).x) / 2;

  double best = DBL_MAX;

  for (int i = m; i >= l && dcmp(x - p.at(i).x - d) <= 0; i--) {
    for (int j = m + 1; j <= r && dcmp(p.at(i).x - x - d) <= 0; j++) {
      best = min(best, dist(p.at(i), p.at(j)));
    }
  }
  return min(best, d);
}

double solve(vector<Point> &p, int l, int r) {
  if (l >= r) {
    return DBL_MAX;
  }
  int m = (l + r) / 2;

  double leftSmall = solve(p, l, m);
  double rightSmall = solve(p, m + 1, r);

  return combine(p, l, m, r, leftSmall, rightSmall);
}

int main() {
  int n;
  cin >> n;
  vector<Point> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i].x >> p[i].y;
  }

  sort(p.begin(), p.end());

  double ans = solve(p, 0, n - 1);

  cout << fixed << setprecision(8) << ans << endl;

  return 0;
}