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

#include <bits/stdc++.h>
using namespace std;

using F = double;

const F EPS = 1e-6;

int N;
vector<F> x, r;

bool ok(auto y) {
  F left = -1e6;
  F right = 1e6;
  for (auto i = 0; i < N; ++i) {
    if (r[i] < y)
      return false;
    F d = sqrt(r[i] * r[i] - y * y);
    left = max(left, x[i] - d);
    right = min(right, x[i] + d);
  }
  return left <= right;
}

int main() {
  cin >> N;
  x.resize(N);
  r.resize(N);
  for (auto i = 0; i < N; ++i)
    cin >> x[i] >> r[i];

  F low = 0, high = 1e6;
  while (EPS < high - low) {
    auto middle = (low + high) / 2;
    if (ok(middle))
      low = middle;
    else
      high = middle;
  }
  cout << setprecision(8) << fixed << low << endl;
}

Error: TLE

Fixed submission:

////////////////////
// ???????????????
#include <algorithm>
#include <cassert>
#include <cmath>
////////////////////
// ??¨????????¢???
using D = double;
////////////////////
// ?????°
const D EPS = 1e-9; // |EPS|???????????°???0??¨????????±???
enum POSITION {
  ONLINE_FRONT = -2,
  CLOCKWISE,
  ON_SEGMENT,
  COUNTER_CLOCKWISE,
  ONLINE_BACK
}; // 3??????????????¢???
////////////////////
// ?§???????
struct Point {
  D x, y;
  Point(const D d = 0.0) : x(d), y(d) {}
  Point(const D x, const D y) : x(x), y(y) {}
};
struct Segment {
  Point initial, terminal;
  Segment(const Point &initial, const Point &terminal)
      : initial(initial), terminal(terminal) {}
  Segment(const D x1, const D y1, const D x2, const D y2)
      : initial(x1, y1), terminal(x2, y2) {}
};
struct Line {
  Point initial, terminal;
  Line(const Point &initial, const Point &terminal)
      : initial(initial), terminal(terminal) {}
  Line(const D x1, const D y1, const D x2, const D y2)
      : initial(x1, y1), terminal(x2, y2) {}
  Line(const Segment &s) : initial(s.initial), terminal(s.terminal) {}
};
////////////////////
// ????£?
bool relative_equal(const D d1, const D d2) {
  return d1 == 0 ? std::abs(d2) < EPS : std::abs((d1 - d2) / d1) < EPS;
}
// ??????????????????????????????
bool operator==(const Point &lhs, const Point &rhs) {
  return relative_equal(lhs.x, rhs.x) && relative_equal(lhs.y, rhs.y);
}
bool operator<(const Point &lhs, const Point &rhs) {
  return relative_equal(lhs.x, rhs.x) ? lhs.y < rhs.y : lhs.x < rhs.x;
}
bool operator>(const Point &lhs, const Point &rhs) {
  return relative_equal(lhs.x, rhs.x) ? lhs.y > rhs.y : lhs.x > rhs.x;
}
bool operator!=(const Point &lhs, const Point &rhs) { return !(lhs == rhs); }
bool operator<=(const Point &lhs, const Point &rhs) { return !(lhs > rhs); }
bool operator>=(const Point &lhs, const Point &rhs) { return !(lhs < rhs); }
const Point operator+(const Point &lhs, const Point &rhs) {
  return Point(lhs.x + rhs.x, lhs.y + rhs.y);
}
const Point operator-(const Point &lhs, const Point &rhs) {
  return Point(lhs.x - rhs.x, lhs.y - rhs.y);
}
const Point operator*(const Point &lhs, const Point &rhs) {
  return Point(lhs.x * rhs.x, lhs.y * rhs.y);
}
const Point operator/(const Point &lhs, const Point &rhs) {
  return Point(lhs.x / rhs.x, lhs.y / rhs.y);
}
const Point operator-(const Point &p) { return Point(-p.x, -p.y); }
// ???
D abs(const Point &p) { return std::hypot(p.x, p.y); }
D norm(const Point &p) { return p.x * p.x + p.y * p.y; }
D dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; }
D cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; }
// ??´???
bool parallel(const Line &l1, const Line &l2) {
  return relative_equal(
      0.0, cross(l2.terminal - l2.initial, l1.terminal - l1.initial));
}
bool orthogonal(const Line &l1, const Line &l2) {
  return relative_equal(
      0.0, dot(l2.terminal - l2.initial, l1.terminal - l1.initial));
}
// 3??????????????¢???
POSITION ccw(const Point &a, const Point &b, const Point &p) {
  Point v1 = b - a;
  Point v2 = p - a;
  if (cross(v1, v2) >= EPS)
    return COUNTER_CLOCKWISE;
  if (cross(v1, v2) <= -EPS)
    return CLOCKWISE;
  if (dot(v1, v2) <= -EPS)
    return ONLINE_BACK;
  if (norm(v1) < norm(v2))
    return ONLINE_FRONT; // ????????????: norm(v1)<norm(v2)+EPS?????¨WrongAnswer
  return ON_SEGMENT;
}
// ????????????
bool intersect(const Point &p1, const Point &p2) { return p1 == p2; }
bool intersect(const Point &p, const Segment &s) {
  return abs(p - s.initial) + abs(p - s.terminal) -
             abs(s.initial - s.terminal) <=
         EPS;
}
bool intersect(const Segment &s, const Point &p) { return intersect(p, s); }
bool intersect(const Point &p, const Line &l) {
  return !parallel(Line(p, l.initial), Line(p, l.terminal));
}
bool intersect(const Line &l, const Point &p) { return intersect(p, l); }
bool intersect(const Segment &s1, const Segment &s2) {
  return ccw(s1.initial, s1.terminal, s2.initial) *
                 ccw(s1.initial, s1.terminal, s2.terminal) <=
             0 &&
         ccw(s2.initial, s2.terminal, s1.initial) *
                 ccw(s2.initial, s2.terminal, s1.terminal) <=
             0;
}
bool intersect(const Segment &s, const Line &l) {
  auto v1 = l.terminal - l.initial;
  auto v2 = s.initial - l.initial;
  auto v3 = s.terminal - l.initial;
  return cross(v1, v2) * cross(v1, v3) <= EPS;
}
bool intersect(const Line &l, const Segment &s) { return intersect(s, l); }
bool intersect(const Line &l1, const Line &l2) {
  return !parallel(l1, l2) || intersect(l1.initial, l2);
}
// ???????§????
Point rotate(const Point &p, const D a) {
  return Point(p.x * std::cos(a) - p.y * std::sin(a),
               p.x * std::sin(a) + p.y * std::cos(a));
}
Point projection(const Point &p, const Line &l) {
  auto v = l.initial - l.terminal;
  return l.initial + dot(p - l.initial, v) / norm(v) * v;
}
Point projection(const Line &l, const Point &p) { return projection(p, l); }
Point reflection(const Point &p, const Line &l) {
  return p + 2 * (projection(p, l) - p);
}
Point reflection(const Line &l, const Point &p) { return reflection(p, l); }
// ?????¢
D distance(const Point &p1, const Point &p2) {
  auto p = p1 - p2;
  return hypot(p.x, p.y);
}
D distance(const Point &p, const Segment &s) {
  auto r = projection(s, p);
  return intersect(r, s)
             ? distance(p, r)
             : std::min(distance(p, s.initial), distance(p, s.terminal));
}
D distance(const Segment &s, const Point &p) { return distance(p, s); }
D distance(const Point &p, const Line &l) {
  return distance(p, projection(p, l));
}
D distance(const Line &l, const Point &p) { return distance(p, l); }
D distance(const Segment &s1, const Segment &s2) {
  return intersect(s1, s2)
             ? 0.0
             : std::min({distance(s1, s2.initial), distance(s1, s2.terminal),
                         distance(s2, s1.initial), distance(s2, s1.terminal)});
}
D distance(const Segment &s, const Line &l) {
  return intersect(s, l)
             ? 0.0
             : std::min(distance(s.initial, l), distance(s.terminal, l));
}
D distance(const Line &l, const Segment &s) { return distance(s, l); }
D distance(const Line &l1, const Line &l2) {
  return intersect(l1, l2) ? 0.0 : distance(l1.initial, l2);
}
// ??????
const Point crosspoint(const Line &l1, const Line &l2) {
  assert(intersect(l1, l2));
  if (parallel(l1, l2))
    return l1.initial; // ???????????¨???l1????§?????????????
  D A = cross(l1.terminal - l1.initial, l2.terminal - l2.initial);
  D B = cross(l1.terminal - l1.initial, l1.terminal - l2.initial);
  return l2.initial + B / A * (l2.terminal - l2.initial);
}
const Point crosspoint(const Segment &s, const Line &l) {
  assert(intersect(s, l));
  if (parallel(s, l))
    return s.initial;
  return crosspoint(Line(s), l);
}
const Point crosspoint(const Line &l, const Segment &s) {
  return crosspoint(s, l);
}
const Point crosspoint(const Segment &s1, const Segment &s2) {
  assert(intersect(s1, s2));
  if (parallel(s1, s2)) { // ???????????¨?????????????????????????????????
    if (intersect(s1.initial, s2))
      return s1.initial;
    if (intersect(s1.terminal, s2))
      return s1.terminal;
    if (intersect(s2.initial, s1))
      return s2.initial;
    if (intersect(s2.terminal, s1))
      return s2.terminal;
  }
  return crosspoint(Line(s1), Line(s2));
}
// ???????????°??¨
#include <iostream>
std::ostream &operator<<(std::ostream &os, const Point &p) {
  os << "point: ";
  os << "(" << p.x << "," << p.y << ")";
  return os;
}
std::ostream &operator<<(std::ostream &os, const Segment &s) {
  os << "segment: ";
  os << s.initial << " - " << s.terminal;
  return os;
}
std::ostream &operator<<(std::ostream &os, const Line &l) {
  os << "line: ";
  os << l.initial << " - " << l.terminal;
  return os;
}
////////////////////////////////////////////////////////////////////////////////
// ???
#include <vector>
const D PI = std::acos(-1);
struct Circle {
  Point center;
  D radius;
  Circle(const Point &center = 0, const D radius = 1)
      : center(center), radius(radius) {}
  Circle(const D x, const D y, const D radius) : center(x, y), radius(radius) {}
};
// ????????¢???
bool contain(const Circle &c, const Point &p) {
  D d = distance(c.center, p);
  if (relative_equal(c.radius, d))
    return true;
  return d < c.radius;
}
bool contain(const Point &p, const Circle &c) { return contain(c, p); }
// ??±?????\\???
int tangent(const Circle &c1, const Circle &c2) {
  D d = distance(c1.center, c2.center);
  D r = c1.radius + c2.radius;
  if (relative_equal(d, r))
    return 3; // ?????\\
  if(r < d) return 4;                   // ?????¢
  r = std::abs(c1.radius - c2.radius);
  if (relative_equal(d, r))
    return 1; // ?????\\
  if(r < d) return 2;                   // ??????
  return 0;   // ??????
}
// ??????
std::vector<Point> crosspoint(const Circle &c, const Line &l) {
  std::vector<Point> cp;
  Point p = projection(c.center, l);
  if (!contain(c, p))
    return cp;
  Point v = p - c.center;
  if (relative_equal(abs(v), 0)) {
    Point p = l.initial - c.center;
    if (p == 0)
      p = l.terminal - c.center;
    p = (c.radius * p / abs(p));
    cp.push_back(c.center + p);
    cp.push_back(c.center - p);
    return cp;
  }
  D angle = acos(abs(v) / c.radius);
  for (D a : {angle, -angle})
    cp.push_back(c.center + c.radius * (rotate(v, a) / abs(v)));
  return cp;
}
std::vector<Point> crosspoint(const Line &l, const Circle &c) {
  return crosspoint(c, l);
}
std::vector<Point> crosspoint(const Circle &c1, const Circle &c2) {
  assert(tangent(c1, c2) != 4);
  std::vector<Point> cp;
  D angle = atan2(c2.center.y - c1.center.y, c2.center.x - c1.center.x);
  D d = distance(c1.center, c2.center);
  D add = acos((d * d + c1.radius * c1.radius - c2.radius * c2.radius) /
               (2.0 * d * c1.radius));
  for (D a : {angle + add, angle - add})
    cp.push_back(c1.center + c1.radius * Point(std::cos(a), std::sin(a)));
  return cp;
}

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;

  vector<Circle> C(N);
  for (auto &c : C)
    cin >> c.center.x >> c.radius;

  D ans = 1e9;
  for (auto c : C)
    ans = min(ans, c.radius);

  struct Query {
    int id;
    bool out;
    D x;
  };
  vector<Query> query;
  for (auto i = 0; i < C.size(); ++i) {
    query.push_back({i, true, C[i].center.x + C[i].radius});
    query.push_back({i, false, C[i].center.x - C[i].radius});
  }
  sort(begin(query), end(query), [](auto l, auto r) {
    if (l.x != r.x)
      return l.x < r.x;
    if (l.out != r.out)
      return l.out < r.out;
    return l.id < r.id;
  });

  set<int> S;
  for (auto q : query) {
    if (q.out) {
      S.erase(q.id);
      for (auto i : S) {
        auto t = tangent(C[q.id], C[i]);
        if (t == 2 || t == 3)
          ans = min(ans, distance(crosspoint(C[q.id], C[i]).front(),
                                  Line(0, 0, 1, 0)));
      }
    } else {
      S.emplace(q.id);
    }
  }

  cout << setprecision(6) << fixed << ans << endl;
}