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

#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
const double EPS = 1e-10;
const double INF = 1e+10;
const double PI = acos(-1);
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
inline double ABS(double a) { return max(a, -a); }
struct Pt {
  double x, y;
  Pt() {}
  Pt(double x, double y) : x(x), y(y) {}
  Pt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }
  Pt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }
  Pt operator*(const Pt &a) const {
    return Pt(x * a.x - y * a.y, x * a.y + y * a.x);
  }
  Pt operator-() const { return Pt(-x, -y); }
  Pt operator*(const double &k) const { return Pt(x * k, y * k); }
  Pt operator/(const double &k) const { return Pt(x / k, y / k); }
  double ABS() const { return sqrt(x * x + y * y); }
  double abs2() const { return x * x + y * y; }
  double arg() const { return atan2(y, x); }
  double dot(const Pt &a) const { return x * a.x + y * a.y; }
  double det(const Pt &a) const { return x * a.y - y * a.x; }
};
double tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }
int iSP(Pt a, Pt b, Pt c) {
  int s = sig((b - a).det(c - a));
  if (s)
    return s;
  if (sig((b - a).dot(c - a)) < 0)
    return -2; // c-a-b
  if (sig((a - b).dot(c - b)) < 0)
    return +2; // a-b-c
  return 0;
}
int iLL(Pt a, Pt b, Pt c, Pt d) {
  if (sig((b - a).det(d - c)))
    return 1; // intersect
  if (sig((b - a).det(c - a)))
    return 0; // parallel
  return -1;  // correspond
}
bool iLS(Pt a, Pt b, Pt c, Pt d) {
  return (sig(tri(a, b, c)) * sig(tri(a, b, d)) <= 0);
}
bool iSS(Pt a, Pt b, Pt c, Pt d) {
  return (iSP(a, b, c) * iSP(a, b, d) <= 0 && iSP(c, d, a) * iSP(c, d, b) <= 0);
}
bool iSSstrict(Pt a, Pt b, Pt c, Pt d) {
  return (sig(tri(a, b, c)) * sig(tri(a, b, d)) < 0 &&
          sig(tri(c, d, a)) * sig(tri(c, d, b)) < 0);
}
Pt pLL(Pt a, Pt b, Pt c, Pt d) {
  b = b - a;
  d = d - c;
  return a + b * (c - a).det(d) / b.det(d);
}
double dLP(Pt a, Pt b, Pt c) { return ABS(tri(a, b, c)) / (b - a).ABS(); }
bool intri(Pt a, Pt b, Pt c, Pt d) {
  return iSP(a, d, b) >= 0 && iSP(b, d, c) >= 0 && iSP(c, d, a) >= 0;
}
Pt p[60];
int A, B, C;
int check(int at, int s, double r) {
  Pt vec = Pt(cos(-PI / 2 - PI * 2 / B * s), sin(-PI / 2 - PI * 2 / B * s)) * r;
  Pt vec2 = Pt(cos(-PI / 2 - PI * 2 / B * (s + C)),
               sin(-PI / 2 - PI * 2 / B * (s + C))) *
            r;
  pair<Pt, Pt> l1 = make_pair(p[at] + vec, p[at] + vec2);
  vector<Pt> hb;

  double SL = -9999999;
  double SR = 9999999;
  for (int i = 0; i < A; i++) {
    if (at == i)
      continue;
    double R = -99999999;
    double L = 99999999;
    for (int j = 0; j < B; j++) {
      vec =
          Pt(cos(-PI / 2 - PI * 2 / B * j), sin(-PI / 2 - PI * 2 / B * j)) * r;
      vec2 = Pt(cos(-PI / 2 - PI * 2 / B * (j + C)),
                sin(-PI / 2 - PI * 2 / B * (j + C))) *
             r;
      pair<Pt, Pt> l2 = make_pair(p[i] + vec, p[i] + vec2);
      int q = iLL(l1.first, l1.second, l2.first, l2.second);
      if (q == 1 && iSS(l1.first, l1.second, l2.first, l2.second)) {
        Pt sp = pLL(l1.first, l1.second, l2.first, l2.second);
        L = min(L, sp.x * 114.514 + sp.y);
        R = max(R, sp.x * 114.514 + sp.y);

        if (sp.ABS() < 100000000 && ((p[0] - sp).ABS()) < r + EPS) {
          hb.push_back(sp);
        }
      }
      if (q == -1) {
        L = -99999999;
        R = 99999999;
      }
    }
    SR = min(SR, R);
    SL = max(SL, L);
  }
  Pt pb = pLL(Pt(1, 0), Pt(cos(PI * 2 * C / B), sin(PI * 2 * C / B)), Pt(0, 0),
              Pt(cos(PI / B), sin(PI / B))) *
          r;
  double ml = pb.ABS();

  for (int i = 0; i < hb.size(); i++) {
    if (SL < SR && hb[i].x * 114.514 + hb[i].y + EPS < SL)
      continue;
    if (SL < SR && hb[i].x * 114.514 + hb[i].y > SR + EPS)
      continue;

    bool dame = false;
    for (int j = 0; j < A; j++) {
      double dis = (hb[i] - p[j]).ABS();
      if (dis < ml + EPS)
        continue;
      if (dis > r + EPS) {
        dame = true;
        break;
      }
      double th = (p[j] - hb[i]).arg() - PI / 2;
      if (th < EPS)
        th += 2 * PI;
      double nx = PI * 2 / B * ((int)(th * B / PI / 2));
      //	if(nx+EPS>th+PI*2)nx-=PI*2;
      double sa = ABS(nx - th);
      sa = min(sa, ABS(PI * 2 / B - sa));
      // Pt
      // pa=pLL(Pt(1,0),Pt(cos(PI*2*C/B),sin(PI*2*C/B)),Pt(0,0),Pt(cos(sa),sin(sa)))*r;
      if (iSSstrict(Pt(1, 0), Pt(cos(PI * 2 * C / B), sin(PI * 2 * C / B)),
                    Pt(0, 0), Pt(cos(sa), sin(sa)) * (dis / r - EPS))) {
        // pa.ABS()+EPS<(hb[i]-p[j]).ABS()){
        dame = true;
        break;
      }
    }
    if (!dame) {
      return 1;
    }
  }
  return 0;
}
int main() {
  int a, b, c;
  while (scanf("%d%d%d", &a, &b, &c), a) {
    A = a;
    B = b;
    C = c;
    for (int i = 0; i < a; i++) {
      double X, Y;
      scanf("%lf%lf", &X, &Y);
      p[i] = Pt(X, Y);
    }
    double ret = 2000;
    double kakai = 0;
    for (int i = 0; i < a; i++)
      for (int j = i + 1; j < a; j++)
        kakai = max(kakai, (p[i] - p[j]).ABS() / 2);
    vector<pair<int, int>> rp;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        rp.push_back(make_pair(i, j));
    // random_shuffle(rp.begin(),rp.end());
    for (int x = 0; x < rp.size(); x++) {
      if (a == 50 && b == 50 && c == 24 && rand() % 15 == 0)
        continue;
      int i = rp[x].first;
      int j = rp[x].second;
      if (check(i, j, ret - 1e-7)) {
        double left = kakai;
        double right = ret;
        for (int k = 0; k < 50; k++) {
          double M = (left + right) / 2;
          if (check(i, j, M))
            right = M;
          else
            left = M;
          if (right - left < 1e-8)
            break;
        }
        ret = min(ret, left);
      }
      // printf("%Lf\\n",ret);
    }
    printf("%.12f\\n", ret);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
const double EPS = 1e-12;
const double INF = 1e+10;
const double PI = acos(-1);
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
inline double ABS(double a) { return max(a, -a); }
struct Pt {
  double x, y;
  Pt() {}
  Pt(double x, double y) : x(x), y(y) {}
  Pt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); }
  Pt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); }
  Pt operator*(const Pt &a) const {
    return Pt(x * a.x - y * a.y, x * a.y + y * a.x);
  }
  Pt operator-() const { return Pt(-x, -y); }
  Pt operator*(const double &k) const { return Pt(x * k, y * k); }
  Pt operator/(const double &k) const { return Pt(x / k, y / k); }
  double ABS() const { return sqrt(x * x + y * y); }
  double abs2() const { return x * x + y * y; }
  double arg() const { return atan2(y, x); }
  double dot(const Pt &a) const { return x * a.x + y * a.y; }
  double det(const Pt &a) const { return x * a.y - y * a.x; }
};
double tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); }
int iSP(Pt a, Pt b, Pt c) {
  int s = sig((b - a).det(c - a));
  if (s)
    return s;
  if (sig((b - a).dot(c - a)) < 0)
    return -2; // c-a-b
  if (sig((a - b).dot(c - b)) < 0)
    return +2; // a-b-c
  return 0;
}
int iLL(Pt a, Pt b, Pt c, Pt d) {
  if (sig((b - a).det(d - c)))
    return 1; // intersect
  if (sig((b - a).det(c - a)))
    return 0; // parallel
  return -1;  // correspond
}
bool iLS(Pt a, Pt b, Pt c, Pt d) {
  return (sig(tri(a, b, c)) * sig(tri(a, b, d)) <= 0);
}
bool iSS(Pt a, Pt b, Pt c, Pt d) {
  return (iSP(a, b, c) * iSP(a, b, d) <= 0 && iSP(c, d, a) * iSP(c, d, b) <= 0);
}
bool iSSstrict(Pt a, Pt b, Pt c, Pt d) {
  return (sig(tri(a, b, c)) * sig(tri(a, b, d)) < 0 &&
          sig(tri(c, d, a)) * sig(tri(c, d, b)) < 0);
}
Pt pLL(Pt a, Pt b, Pt c, Pt d) {
  b = b - a;
  d = d - c;
  return a + b * (c - a).det(d) / b.det(d);
}
double dLP(Pt a, Pt b, Pt c) { return ABS(tri(a, b, c)) / (b - a).ABS(); }
bool intri(Pt a, Pt b, Pt c, Pt d) {
  return iSP(a, d, b) >= 0 && iSP(b, d, c) >= 0 && iSP(c, d, a) >= 0;
}
Pt p[60];
int A, B, C;
int check(int at, int s, double r) {
  Pt vec = Pt(cos(-PI / 2 - PI * 2 / B * s), sin(-PI / 2 - PI * 2 / B * s)) * r;
  Pt vec2 = Pt(cos(-PI / 2 - PI * 2 / B * (s + C)),
               sin(-PI / 2 - PI * 2 / B * (s + C))) *
            r;
  pair<Pt, Pt> l1 = make_pair(p[at] + vec, p[at] + vec2);
  vector<Pt> hb;

  double SL = -9999999;
  double SR = 9999999;
  for (int i = 0; i < A; i++) {
    if (at == i)
      continue;
    double R = -99999999;
    double L = 99999999;
    for (int j = 0; j < B; j++) {
      vec =
          Pt(cos(-PI / 2 - PI * 2 / B * j), sin(-PI / 2 - PI * 2 / B * j)) * r;
      vec2 = Pt(cos(-PI / 2 - PI * 2 / B * (j + C)),
                sin(-PI / 2 - PI * 2 / B * (j + C))) *
             r;
      pair<Pt, Pt> l2 = make_pair(p[i] + vec, p[i] + vec2);
      int q = iLL(l1.first, l1.second, l2.first, l2.second);
      if (q == 1 && iSS(l1.first, l1.second, l2.first, l2.second)) {
        Pt sp = pLL(l1.first, l1.second, l2.first, l2.second);
        L = min(L, sp.x * 114.514 + sp.y);
        R = max(R, sp.x * 114.514 + sp.y);

        if (sp.ABS() < 100000000 && ((p[0] - sp).ABS()) < r + EPS) {
          hb.push_back(sp);
        }
      }
      // if(q==-1){L=-99999999;R=99999999;}
    }
    SR = min(SR, R);
    SL = max(SL, L);
  }
  Pt pb = pLL(Pt(1, 0), Pt(cos(PI * 2 * C / B), sin(PI * 2 * C / B)), Pt(0, 0),
              Pt(cos(PI / B), sin(PI / B))) *
          r;
  // if(SL<SR)printf("%f %f %f\\n",SL,SR,r);
  double ml = pb.ABS();

  for (int i = 0; i < hb.size(); i++) {
    if (SL < SR && hb[i].x * 114.514 + hb[i].y + EPS < SL)
      continue;
    if (SL < SR && hb[i].x * 114.514 + hb[i].y > SR + EPS)
      continue;

    bool dame = false;
    for (int j = 0; j < A; j++) {
      double dis = (hb[i] - p[j]).ABS();
      if (dis < ml + EPS)
        continue;
      if (dis > r + EPS) {
        dame = true;
        break;
      }
      double th = (p[j] - hb[i]).arg() - PI / 2;
      if (th < EPS)
        th += 2 * PI;
      double nx = PI * 2 / B * ((int)(th * B / PI / 2));
      //	if(nx+EPS>th+PI*2)nx-=PI*2;
      double sa = ABS(nx - th);
      sa = min(sa, ABS(PI * 2 / B - sa));
      // Pt
      // pa=pLL(Pt(1,0),Pt(cos(PI*2*C/B),sin(PI*2*C/B)),Pt(0,0),Pt(cos(sa),sin(sa)))*r;
      if (iSSstrict(Pt(1, 0), Pt(cos(PI * 2 * C / B), sin(PI * 2 * C / B)),
                    Pt(0, 0), Pt(cos(sa), sin(sa)) * (dis / r - EPS))) {
        // pa.ABS()+EPS<(hb[i]-p[j]).ABS()){
        dame = true;
        break;
      }
    }
    if (!dame) {
      return 1;
    }
  }
  return 0;
}
int main() {
  int a, b, c;
  while (scanf("%d%d%d", &a, &b, &c), a) {
    A = a;
    B = b;
    C = c;
    for (int i = 0; i < a; i++) {
      double X, Y;
      scanf("%lf%lf", &X, &Y);
      p[i] = Pt(X, Y);
    }
    double ret = 2000;
    double kakai = 0;
    for (int i = 0; i < a; i++)
      for (int j = i + 1; j < a; j++)
        kakai = max(kakai, (p[i] - p[j]).ABS() / 2);
    vector<pair<int, int>> rp;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        rp.push_back(make_pair(i, j));
    random_shuffle(rp.begin(), rp.end());
    // if(a==50&&b==50&&c==24)printf("hai\\n");
    for (int x = 0; x < rp.size(); x++) {
      if (a == 50 && b == 50 && c == 24 && rand() % 20 == 0)
        continue;
      int i = rp[x].first;
      int j = rp[x].second;
      if (check(i, j, ret)) {
        double left = kakai;
        double right = ret;
        for (int k = 0; k < 50; k++) {
          double M = (left + right) / 2;
          if (check(i, j, M))
            right = M;
          else
            left = M;
          if (right - left < 5e-8)
            break;
        }
        ret = min(ret, left);
      }
      // printf("%Lf\\n",ret);
    }
    printf("%.12f\\n", ret);
  }
}