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

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

const double EPS = 1.0e-8;

int n;
double xs[311];
double ys[311];

void solve() {
  int maxval = 1;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      double dx = xs[i] - xs[j];
      double dy = ys[i] - ys[j];
      if (dx * dx + dy * dy >= 4.0 - EPS)
        continue;

      double mx = 0.5 * (xs[i] + xs[j]);
      double my = 0.5 * (ys[i] + ys[j]);
      double nx = -(ys[i] - ys[j]);
      double ny = xs[i] - xs[j];
      double nl = sqrt(nx * nx + ny * ny);
      double cl = sqrt(1.0 - 0.25 * nl * nl);
      nx = cl * nx / nl;
      ny = cl * ny / nl;

      int cnt;
      double cx, cy;

      cx = mx + nx;
      cy = my + ny;
      cnt = 0;
      for (int k = 0; k < n; k++) {
        dx = cx - xs[k];
        dy = cy - ys[k];
        if (dx * dx + dy * dy <= 1.0 + EPS)
          cnt++;
      }
      maxval = max(maxval, cnt);

      cx = mx - nx;
      cy = my - ny;
      cnt = 0;
      for (int k = 0; k < n; k++) {
        dx = cx - xs[k];
        dy = cy - ys[k];
        if (dx * dx + dy * dy <= 1.0 + EPS)
          cnt++;
      }
      maxval = max(maxval, cnt);
    }
  }
  printf("%d\\n", maxval);
}

int main(int argc, char **argv) {
  while (cin >> n) {
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> xs[i] >> ys[i];
    }
    solve();
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

struct Point {
  double x, y;
  Point(double x_ = 0.0, double y_ = 0.0) : x(x_), y(y_) {}
  Point(const Point &p) : x(p.x), y(p.y) {}

  Point &operator=(const Point &p) {
    x = p.x;
    y = p.y;
    return *this;
  }

  double det(const Point &p) { return x * p.y - y * p.x; }

  Point nrm() {
    Point ret;
    double n = length();
    ret.x = x / n;
    ret.y = y / n;
    return ret;
  }

  double length() { return sqrt(x * x + y * y); }
};

Point operator+(const Point &p1, const Point &p2) {
  return Point(p1.x + p2.x, p1.y + p2.y);
}

Point operator-(const Point &p1, const Point &p2) {
  return Point(p1.x - p2.x, p1.y - p2.y);
}

Point operator*(const Point &p1, double s) { return Point(p1.x * s, p1.y * s); }

Point operator/(const Point &p1, double s) { return Point(p1.x / s, p1.y / s); }

Point calcCenter(const Point &p1, const Point &p2, const Point &p3) {
  double a = (p1 - p2).length();
  double b = (p2 - p3).length();
  double c = (p3 - p1).length();
  double A = a * a;
  double B = b * b;
  double C = c * c;
  double S = 0.5 * std::abs((p2 - p1).det(p3 - p1));
  Point cent = (p1 * (A * (B + C - A)) + p2 * (B * (C + A - B)) +
                p3 * (C * (A + B - C))) /
               (16.0 * S * S);
  return p1 + (cent - p1).nrm();
}

void solve(const std::vector<Point> &pts) {
  int n = (int)pts.size();
  int maxval = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = j + 1; k < n; k++) {
        Point cnt = calcCenter(pts[i], pts[j], pts[k]);
        int s = 0;
        for (int l = 0; l < n; l++) {
          if ((cnt - pts[l]).length() <= 1.0) {
            s++;
          }
        }

        if (maxval < s) {
          maxval = s;
        }
      }
    }
  }
  printf("%d\\n", maxval);
}

int main(int argc, char **argv) {
  int n;
  double x, y;
  while (cin >> n) {
    if (n == 0)
      break;

    vector<Point> pts;
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      pts.push_back(Point(x, y));
    }
    solve(pts);
  }
}