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

#include <bits/stdc++.h>

using namespace std;

const double r = 1.0;
const double PI = acos(-1.0);
const double sq2 = sqrt(2.0);
const double dx[] = {0.0, 1.0, 0.0, 1.0};
const double dy[] = {0.0, 0.0, 1.0, 1.0};

struct Point {
  double x, y;
  Point() {}
  Point(double x, double y) : x(x), y(y) {}
};
struct Rect {
  double x, y, len; // ??????(x, y)??§?????????len?????£?????¢
  int ub;
  Rect() { ub = 0; }
  Rect(double x, double y, double len, int ub) : x(x), y(y), len(len), ub(ub) {}
};
bool operator<(const Rect &a, const Rect &b) { return a.ub < b.ub; }
inline double Distance2(const Point &a, const Point &b) {
  return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

int n;
vector<Point> p;

Rect MakeRect(double x, double y, double l, int &ans) {
  int num = 0, u_bound = 0;
  const double len = l * l * 0.5 + sq2 * l + 1.0;
  Point c(x + l * 0.5, y + l * 0.5);

  for (int i = 0; i < n; ++i) {
    double d = Distance2(c, p[i]);

    if (d <= 1.0)
      ++num;
    if (d <= len)
      ++u_bound;
  }

  ans = max(ans, num);

  return Rect(x, y, l, u_bound);
}

int Solve() {
  int ans = 1;
  priority_queue<Rect> que;

  que.push(MakeRect(0.0, 0.0, 10.0, ans));

  while (!que.empty()) {
    Rect now = que.top();
    que.pop();

    if (ans < now.ub) {
      double half = now.len * 0.5;

      for (int d = 0; d < 4; ++d) {
        Rect r =
            MakeRect(now.x + half * dx[d], now.y + half * dy[d], half, ans);
        if (ans < r.ub)
          que.push(r);
      }
    }
  }

  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> n, n) {
    p.resize(n);
    for (int i = 0; i < n; ++i)
      cin >> p[i].x >> p[i].y;

    cout << Solve() << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const double r = 1.0;
const double PI = acos(-1.0);

struct Point {
  double x, y;
  Point() {}
  Point(double x, double y) : x(x), y(y) {}
};
inline double Distance2(const Point &a, const Point &b) {
  return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  std::random_device rnd;
  std::mt19937 mt(rnd());
  std::uniform_real_distribution<> rand100(0.0, 10.0);

  while (cin >> n, n) {
    // Input
    vector<Point> p(n);
    for (int i = 0; i < n; ++i)
      cin >> p[i].x >> p[i].y;

    // Solve
    int ans = 1;
    auto start = std::chrono::high_resolution_clock::now();
    auto end = std::chrono::high_resolution_clock::now();
    auto take_time =
        std::chrono::duration_cast<std::chrono::milliseconds>(end - start);

    while (true) {
      end = std::chrono::high_resolution_clock::now();
      take_time =
          std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
      if (300 < take_time.count())
        break;

      for (int i = 0; i < 10; ++i) {
        Point c(rand100(mt), rand100(mt));
        int num = 0;
        for (int j = 0; j < n; ++j) {
          double d = Distance2(p[j], c);
          if (d <= 1.0)
            ++num;
        }
        ans = max(ans, num);
      }
    }

    // Output
    cout << ans << endl;
  }

  return 0;
}