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

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

struct Point {
  bool operator<(const struct Point &arg) const { return x < arg.x; };
  double x, y;
};

double a, b, c, d;

double calc(double x) {
  return ((c - a) * x) / (b - d) + (b + d) / 2 -
         (c - a) * (a + c) / (2.0 * (b - d));
}

double dist(double x, double y) {
  double ans1 = sqrt((x - a) * (x - a) + (y - b) * (y - b));
  double ans2 = sqrt((x - c) * (x - c) + (y - d) * (y - d));

  if (fabs(1.0 - ans1) < fabs(1.0 - ans2)) {
    return ans1;
  } else {
    return ans2;
  }
}

int main() {

  int N, maximum, count;
  Point points[300];
  double left, right, m, tmp_x, tmp_y, tmp_dist;

  bool FLG;

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    maximum = 1;

    for (int loop = 0; loop < N; loop++) {
      scanf("%lf %lf", &points[loop].x, &points[loop].y);
    }

    sort(points, points + N);

    for (int i = 0; i <= N - 2; i++) {
      for (int k = i + 1; k <= N - 1; k++) {

        a = points[i].x;
        b = points[i].y;
        c = points[k].x;
        d = points[k].y;

        left = 0.0;
        right = (a + c) / 2.0;
        m = (left + right) / 2.0;

        FLG = false;

        while (left <= right) {
          tmp_y = calc(m);
          tmp_dist = dist(m, tmp_y);

          if (fabs(tmp_dist - 1.0) < EPS) {
            FLG = true;
            tmp_x = m;
            break;
          } else if (tmp_dist > 1.0) {
            left = m + EPS;
          } else {
            right = m - EPS;
          }
          m = (left + right) / 2.0;
        }

        if (FLG) {
          count = 0;
          for (int p = 0; p < N; p++) {
            if (sqrt((tmp_x - points[p].x) * (tmp_x - points[p].x) +
                     (tmp_y - points[p].y) * (tmp_y - points[p].y)) < 1.0) {
              count++;
            } else if (fabs(
                           sqrt((tmp_x - points[p].x) * (tmp_x - points[p].x) +
                                (tmp_y - points[p].y) * (tmp_y - points[p].y)) -
                           1.0) <= EPS) {
              count++;
            }
          }
          maximum = max(maximum, count);
        }

        left = (a + c) / 2.0;
        right = 10.0;
        m = (left + right) / 2.0;
        FLG = false;

        while (left <= right) {
          tmp_y = calc(m);
          tmp_dist = dist(m, tmp_y);

          if (fabs(tmp_dist - 1.0) < EPS) {
            FLG = true;
            tmp_x = m;
            break;
          } else if (tmp_dist > 1.0) {
            right = m - EPS;
          } else {
            left = m + EPS;
          }
          m = (left + right) / 2.0;
        }

        if (FLG) {
          count = 0;
          for (int p = 0; p < N; p++) {
            if (sqrt((tmp_x - points[p].x) * (tmp_x - points[p].x) +
                     (tmp_y - points[p].y) * (tmp_y - points[p].y)) < 1.0) {
              count++;
            } else if (fabs(
                           sqrt((tmp_x - points[p].x) * (tmp_x - points[p].x) +
                                (tmp_y - points[p].y) * (tmp_y - points[p].y)) -
                           1.0) <= EPS) {
              count++;
            }
          }
          maximum = max(maximum, count);
        }
      }
    }

    printf("%d\\n", maximum);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

struct Point {
  double x, y;
};

void func(int N) {

  Point point[N];
  int x_table[1001], y_table[1001], x_start, x_end, y_start, y_end;

  for (int i = 0; i <= 1000; i++) {
    x_table[i] = y_table[i] = 0;
  }

  for (int i = 0; i < N; i++) {
    scanf("%lf %lf", &point[i].x, &point[i].y);
    x_start = point[i].x * 100 - 50;
    x_start = max(49, x_start);
    x_end = point[i].x * 100 + 50;
    x_end = min(x_end, 901);
    y_start = point[i].y * 100 - 50;
    y_start = max(49, y_start);
    y_end = point[i].y * 100 + 50;
    y_end = min(y_end, 901);

    for (int k = x_start; k <= x_end; k++) {
      x_table[k]++;
    }

    for (int k = y_start; k <= y_end; k++) {
      y_table[k]++;
    }
  }

  int count, max_count = 0;
  double calc_x, calc_y;

  for (int x = 49; x <= 951; x++) {
    for (int y = 49; y <= 951; y++) {
      if (x_table[x] > 0 && y_table[y] > 0) {
        calc_x = x, calc_y = y;
        calc_x /= 100, calc_y /= 100;
        count = 0;
        for (int i = 0; i < N; i++) {
          if (sqrt((calc_x - point[i].x) * (calc_x - point[i].x) +
                   (calc_y - point[i].y) * (calc_y - point[i].y)) <= 1) {
            count++;
          }
        }
        max_count = max(max_count, count);
      }
    }
  }
  printf("%d\\n", max_count);
}

int main() {

  int N;

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    func(N);
  }

  return 0;
}