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

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

#define MAX_N 310

double x[MAX_N], y[MAX_N];
int n, maxn;

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
    }
    maxn = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int sum = 0;

        double X = (x[i] + x[j]) / 2.0;
        double Y = (y[i] + y[j]) / 2.0;

        for (int k = 0; k < n; k++) {
          if (((x[k] - X) * (x[k] - X) + (y[k] - Y) * (y[k] - Y)) <= 1) {
            sum++;
          }
        }
        maxn = max(maxn, sum);

        double X1, Y1, X2, Y2, X3, Y3, VX2, VY2, Dist, Dist2;

        Dist =
            sqrt((x[j] - x[i]) * (x[j] - x[i]) + (y[j] - y[i]) * (y[j] - y[i]));
        Dist2 = sqrt(4 - Dist * Dist);

        X2 = x[j] - x[i];
        Y2 = y[j] - y[i];
        VX2 = (X2 / Dist) * Dist2 / 2;
        VY2 = (Y2 / Dist) * Dist2 / 2;

        X1 = X + VY2;
        Y1 = Y - VX2;
        X3 = X - VY2;
        Y3 = Y + VX2;

        sum = 0;
        for (int k = 0; k < n; k++) {
          if (((x[k] - X1) * (x[k] - X1) + (y[k] - Y1) * (y[k] - Y1)) <=
              1.0001) {
            sum++;
          }
        }
        maxn = max(maxn, sum);
        sum = 0;
        for (int k = 0; k < n; k++) {
          if (((x[k] - X3) * (x[k] - X3) + (y[k] - Y3) * (y[k] - Y3)) <=
              1.0001) {
            sum++;
          }
        }
        maxn = max(maxn, sum);
      }
    }
    for (int i = 0; i < n; i++) {
      int sum = 0;
      long double X = x[i];
      long double Y = y[i];
      for (int k = 0; k < n; k++) {
        if (((x[k] - X) * (x[k] - X) + (y[k] - Y) * (y[k] - Y)) <= 1) {
          sum++;
        }
      }
      maxn = max(maxn, sum);
    }
    cout << maxn << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

#define MAX_N 310

long double x[MAX_N], y[MAX_N];
int n, maxn;

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
    }
    maxn = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        int sum = 0;

        long double X = (x[i] + x[j]) / 2.0;
        long double Y = (y[i] + y[j]) / 2.0;

        for (int k = 0; k < n; k++) {
          if (((x[k] - X) * (x[k] - X) + (y[k] - Y) * (y[k] - Y)) <= 1) {
            sum++;
          }
        }
        maxn = max(maxn, sum);

        long double X1, Y1, X2, Y2, X3, Y3, VX2, VY2, Dist, Dist2;

        Dist = sqrtl((x[j] - x[i]) * (x[j] - x[i]) +
                     (y[j] - y[i]) * (y[j] - y[i]));
        Dist2 = sqrtl(4 - Dist * Dist);

        X2 = x[j] - x[i];
        Y2 = y[j] - y[i];
        VX2 = (X2 / Dist) * Dist2 / 2;
        VY2 = (Y2 / Dist) * Dist2 / 2;

        X1 = X + VY2;
        Y1 = Y - VX2;
        X3 = X - VY2;
        Y3 = Y + VX2;

        sum = 0;
        for (int k = 0; k < n; k++) {
          if (((x[k] - X1) * (x[k] - X1) + (y[k] - Y1) * (y[k] - Y1)) <=
              1.0001) {
            sum++;
          }
        }
        maxn = max(maxn, sum);
        sum = 0;
        for (int k = 0; k < n; k++) {
          if (((x[k] - X3) * (x[k] - X3) + (y[k] - Y3) * (y[k] - Y3)) <= 1) {
            sum++;
          }
        }
        maxn = max(maxn, sum);
      }
    }
    for (int i = 0; i < n; i++) {
      int sum = 0;
      long double X = x[i];
      long double Y = y[i];
      for (int k = 0; k < n; k++) {
        if (((x[k] - X) * (x[k] - X) + (y[k] - Y) * (y[k] - Y)) <= 1) {
          sum++;
        }
      }
      maxn = max(maxn, sum);
    }
    cout << maxn << endl;
  }
  return 0;
}