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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const double EPS = 1e-8;

struct Rect {
  double x, y, len;
  int c;
  Rect(double x, double y, double len, int c) : x(x), y(y), len(len), c(c) {}
};
bool operator<(const Rect &a, const Rect &b) { return a.c < b.c; };

int n;
double px[333], py[333];

bool contain(double x, double y, double radius, double xa, double ya) {
  double dx = x - xa, dy = y - ya;
  return dx * dx + dy * dy < radius * radius + EPS;
}
int count_p(double cx, double cy, double radius) {
  int res = 0;
  for (int i = 0; i < n; ++i)
    if (contain(cx, cy, radius, px[i], py[i]))
      ++res;
  return res;
}

int main() {
  while (scanf("%d", &n), n) {
    rep(i, n) scanf("%lf%lf", px + i, py + i);

    int res = 0;
    priority_queue<Rect> q;
    q.push(Rect(0, 0, 10, n));
    while (!q.empty()) {
      Rect t = q.top();
      q.pop();
      if (t.c <= res)
        break;

      double len = t.len / 2;
      double rela_x[] = {0, len, 0, len};
      double rela_y[] = {0, 0, len, len};
      rep(i, 4) {
        double tx = t.x + rela_x[i], ty = t.y + rela_y[i];
        double cx = tx + len / 2, cy = ty + len / 2;

        max_swap(res, count_p(cx, cy, 1));

        int c = count_p(cx, cy, len * sqrt(2.0) + 1);
        if (c > res)
          q.push(Rect(tx, ty, len, c));
      }
    }
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

typedef pair<double, double> point;
const double EPS = 1e-6;
int main() {
  int n;
  while (scanf("%d", &n), n) {
    point p[333];
    rep(i, n) scanf("%lf%lf", &p[i].first, &p[i].second);
    sort(p, p + n);

    queue<point> q;
    rep(i, n) q.push(p[i]);

    list<point> r;
    int res = 0;
    for (int i = -1000; i <= 10000; ++i) {
      double cx = i * 1e-3;
      while (!r.empty() && r.front().first < cx - 1 + EPS)
        r.pop_front();
      while (!q.empty() && q.front().first < cx + 1 + EPS)
        r.push_back(q.front()), q.pop();

      vector<double> cy;
      foreach (it, r) {
        double x = it->first, y = it->second;
        double d = sqrt(1 - (x - cx) * (x - cx));
        cy.push_back(y - d);
        cy.push_back(y + d);
      }

      for (int i = 0; i < cy.size(); ++i) {
        int c = 0;
        foreach (it, r) {
          double dx = cx - it->first;
          double dy = cy[i] - it->second;
          if (dx * dx + dy * dy < 1 + EPS)
            ++c;
        }
        max_swap(res, c);
      }
    }
    printf("%d\\n", res);
  }
}