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

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
struct P {
  long double x, y;
};
#define rep(i, n) for (int i = 0; i < n; i++)

const long double EPS = 1e-15;

vector<P> cross(P a, P b) {
  vector<P> ret;

  long double x = a.x - b.x, y = a.y - b.y;
  long double L = x * x + y * y;
  if (L > 4.0)
    return ret;

  L = sqrt(L);
  long double C = atan2(b.y - a.y, b.x - a.x);
  long double alpha = acos(L / 2);

  P r1, r2;
  r1.x = a.x + cos(C + alpha), r1.y = a.y + sin(C + alpha);
  r2.x = a.x + cos(C - alpha), r2.y = a.y + sin(C - alpha);

  ret.push_back(r1), ret.push_back(r2);

  return ret;
}

bool check(P cir, P pt) {
  cir.x -= pt.x, cir.y -= pt.y;
  // if(pt.x>10.0 + EPS || pt.y>10.0+EPS || pt.x<0.0 || pt.y<0.0)return false;
  return (cir.x * cir.x + cir.y * cir.y) <= 1.0 + EPS;
}
int main() {
  int n;
  while (scanf("%d", &n) && n) {
    int ret = 1;
    vector<P> data(n);
    vector<P> search;
    rep(i, n) scanf("%Lf,%Lf\\n", &data[i].x, &data[i].y);

    rep(i, n) {
      for (int j = i + 1; j < n; j++) {
        vector<P> get = cross(data[i], data[j]);
        // cout << "(" << data[i].x << "," << data[i].y << ")" << " and " << "("
        // << data[j].x << "," << data[j].y << ") = ";
        rep(k, get.size()) {
          //	cout << " " << "(" << get[k].x << "," << get[k].y << ")" << " ";
          search.push_back(get[k]);
        }
        // cout << endl;
      }
    }
    rep(i, search.size()) {
      int count = 0;
      rep(j, n) { count += check(data[j], search[i]); }
      // cout << search[i].x << "," << search[i].y << " is " << count << endl;
      ret = max(ret, count);
    }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
struct P {
  long double x, y;
};
#define rep(i, n) for (int i = 0; i < n; i++)

const long double EPS = 1e-15;

vector<P> cross(P a, P b) {
  vector<P> ret;

  long double x = a.x - b.x, y = a.y - b.y;
  long double L = x * x + y * y;
  if (L > 4.0)
    return ret;

  L = sqrt(L);
  long double C = atan2(b.y - a.y, b.x - a.x);
  long double alpha = acos(L / 2);

  P r1, r2;
  r1.x = a.x + cos(C + alpha), r1.y = a.y + sin(C + alpha);
  r2.x = a.x + cos(C - alpha), r2.y = a.y + sin(C - alpha);

  ret.push_back(r1), ret.push_back(r2);

  return ret;
}

bool check(P cir, P pt) {
  cir.x -= pt.x, cir.y -= pt.y;
  if (pt.x > 10.0 + EPS || pt.y > 10.0 + EPS || pt.x < 0.0 || pt.y < 0.0)
    return false;
  return (cir.x * cir.x + cir.y * cir.y) <= 1.0; // + EPS;
}
int main() {
  int n;
  while (scanf("%d", &n) && n) {
    int ret = 1;
    vector<P> data(n);
    vector<P> search;
    rep(i, n) scanf("%lf,%lf\\n", &data[i].x, &data[i].y);

    rep(i, n) {
      for (int j = i + 1; j < n; j++) {
        vector<P> get = cross(data[i], data[j]);
        cout << "(" << data[i].x << "," << data[i].y << ")"
             << " and "
             << "(" << data[j].x << "," << data[j].y << ") = ";
        rep(k, get.size()) {
          cout << " "
               << "(" << get[k].x << "," << get[k].y << ")"
               << " ";
          search.push_back(get[k]);
        }
        cout << endl;
      }
    }
    rep(i, search.size()) {
      int count = 0;
      rep(j, n) { count += check(data[j], search[i]); }
      // cout << search[i].x << "," << search[i].y << " is " << count << endl;
      ret = max(ret, count);
    }
    cout << ret << endl;
  }
}