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

#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
ld eps = 1e-7;
/* ??????????????¬ */

#include <complex>

typedef complex<ld> Point;

const ld pi = acos(-1.0);
const ld dtop = pi / 180.;
const ld ptod = 1. / dtop;

namespace std {
bool operator<(const Point &lhs, const Point &rhs) {
  if (lhs.real() < rhs.real() - eps)
    return true;
  if (lhs.real() > rhs.real() + eps)
    return false;
  return lhs.imag() < rhs.imag();
}
} // namespace std

// ????????\\???
Point input_Point() {
  ld x, y;
  cin >> x >> y;
  return Point(x, y);
}

// ????????????????????????
bool eq(const ld a, const ld b) { return (abs(a - b) < eps); }

// ??????
ld dot(const Point &a, const Point &b) { return real(conj(a) * b); }

// ??????
ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); }

// ??´????????????
class Line {
public:
  Point a, b;
  Line() : a(Point(0, 0)), b(Point(0, 0)) {}
  Line(Point a, Point b) : a(a), b(b) {}
  Point operator[](const int _num) const {
    if (_num == 0)
      return a;
    else if (_num == 1)
      return b;
    else {
      assert(false);
      return Point();
    }
  }
};

// ????????????
class Circle {
public:
  Point p;
  ld r;
  Circle() : p(Point(0, 0)), r(0) {}
  Circle(Point p, ld r) : p(p), r(r) {}
};

// ccw
// 1: a,b,c??????????¨???¨?????????????????¶
//-1: a,b,c???????¨???¨?????????????????¶
// 2: c,a,b???????????´???????????¶
//-2: a,b,c???????????´???????????¶
// 0: a,c,b???????????´???????????¶
int ccw(const Point &a, const Point &b, const Point &c) {
  const Point nb(b - a);
  const Point nc(c - a);
  if (cross(nb, nc) > eps)
    return 1; // a,b,c??????????¨???¨?????????????????¶
  if (cross(nb, nc) < -eps)
    return -1; // a,b,c???????¨???¨?????????????????¶
  if (dot(nb, nc) < 0)
    return 2; // c,a,b???????????´???????????¶
  if (norm(nb) < norm(nc))
    return -2; // a,b,c???????????´???????????¶
  return 0;    // a,c,b???????????´???????????¶
}

/* ???????????? */

// ??´?????¨??´??????????????????
bool isis_ll(const Line &l, const Line &m) {
  return !eq(cross(l.b - l.a, m.b - m.a), 0);
}

// ??´?????¨?????????????????????
bool isis_ls(const Line &l, const Line &s) {
  return isis_ll(l, s) &&
         (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);
}

// ????????¨?????????????????????
bool isis_ss(const Line &s, const Line &t) {
  return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
         ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}

// ????????´????????????
bool isis_lp(const Line &l, const Point &p) {
  return (abs(cross(l.b - p, l.a - p)) < eps);
}

// ?????????????????????
bool isis_sp(const Line &s, const Point &p) {
  return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);
}

// ??????????¶?
Point proj(const Line &l, const Point &p) {
  ld t = dot(p - l.a, l.b - l.a) / norm(l.a - l.b);
  return l.a + t * (l.b - l.a);
}

//???????±??????????????????????
Point reflect(const Line &l, const Point &p) {
  Point pr = proj(l, p);
  return pr * 2.0 - p;
}

// ??´?????¨??´????????????
Point is_ll(const Line &s, const Line &t) {
  Point sv = s.b - s.a, tv = t.b - t.a;
  assert(cross(sv, tv) != 0);
  return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}
// ??´?????¨??´????????????
vector<Point> is_ll2(const Line &s, const Line &t) {
  Point sv = s.b - s.a, tv = t.b - t.a;
  if (cross(sv, tv) != 0)
    return vector<Point>(1, is_ll(s, t));
  else {
    vector<Point> ans;
    for (int k = 0; k < 2; ++k) {
      if (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())
        ans.push_back(t[k]);
      if (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())
        ans.push_back(s[k]);
    }
    return ans;
  }
}
// ????????¨???????????????
//???????????£????????¨???????????¨assert(false)
Point is_ss(const Line &s, const Line &t) {
  if (isis_ss(s, t)) {
    for (int k = 0; k < 2; ++k) {
      for (int l = 0; l < 2; ++l) {
        if (s[k] == t[l])
          return s[k];
      }
    }
    return is_ll(s, t);
  } else {
    //??????isis_ss?????????
    assert(false);
    return Point(0, 0);
  }
}
// ????????¨???????????????
vector<Point> is_ss2(const Line &s, const Line &t) {
  vector<Point> kouho(is_ll2(s, t));
  vector<Point> ans;
  for (auto p : kouho) {
    if (isis_sp(s, p) && isis_sp(t, p))
      ans.emplace_back(p);
  }
  return ans;
}
// ??´?????¨???????????¢
ld dist_lp(const Line &l, const Point &p) { return abs(p - proj(l, p)); }

//??´?????¨??´???????????¢
ld dist_ll(const Line &l, const Line &m) {
  return isis_ll(l, m) ? 0 : dist_lp(l, m.a);
}

// ??´?????¨??????????????¢
ld dist_ls(const Line &l, const Line &s) {
  return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));
}

// ????????¨???????????¢
ld dist_sp(const Line &s, const Point &p) {
  Point r = proj(s, p);
  return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
}

// ????????¨??????????????¢
ld dist_ss(const Line &s, const Line &t) {
  return min(min(dist_sp(s, t.a), dist_sp(s, t.b)),
             min(dist_sp(t, s.a), dist_sp(t, s.b)));
}

int main() {
  ld ans = 1e18;
  int N;
  cin >> N;
  vector<Line> ls;
  map<pair<int, int>, int> mp;
  for (int i = 0; i < N; ++i) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (c < a) {
      swap(a, c);
      swap(b, d);
    }
    ls.push_back(Line(Point(a, b), Point(c, d)));
    mp[make_pair(a, b)]++;
    mp[make_pair(c, d)]++;
  }
  sort(ls.begin(), ls.end(),
       [](const Line &l, const Line &r) { return l.a < r.a; });
  bool flag = false;
  for (auto m : mp) {
    if (m.second >= 2)
      flag = true;
  }

  if (flag) {
    ans = 0;
  } else {
    for (int i = 0; i < N; ++i) {
      for (int j = i + 1; j < N; ++j) {
        if (ls[i].b.real() + ans - eps < ls[j].a.real()) {
          break;
        }
        if (isis_ss(ls[i], ls[j]))
          ans = 0;
        else {
          ans = min(ans, dist_ss(ls[i], ls[j]));
        }
      }
      if (ans < eps)
        break;
    }
  }
  cout << setprecision(10) << fixed << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
ld eps = 1e-7;
/* ??????????????¬ */

#include <complex>

typedef complex<ld> Point;

const ld pi = acos(-1.0);
const ld dtop = pi / 180.;
const ld ptod = 1. / dtop;

namespace std {
bool operator<(const Point &lhs, const Point &rhs) {
  if (lhs.real() < rhs.real() - eps)
    return true;
  if (lhs.real() > rhs.real() + eps)
    return false;
  return lhs.imag() < rhs.imag();
}
} // namespace std

// ????????\\???
Point input_Point() {
  ld x, y;
  cin >> x >> y;
  return Point(x, y);
}

// ????????????????????????
bool eq(const ld a, const ld b) { return (abs(a - b) < eps); }

// ??????
ld dot(const Point &a, const Point &b) { return real(conj(a) * b); }

// ??????
ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); }

// ??´????????????
class Line {
public:
  Point a, b;
  Line() : a(Point(0, 0)), b(Point(0, 0)) {}
  Line(Point a, Point b) : a(a), b(b) {}
  Point operator[](const int _num) const {
    if (_num == 0)
      return a;
    else if (_num == 1)
      return b;
    else {
      assert(false);
      return Point();
    }
  }
};

// ????????????
class Circle {
public:
  Point p;
  ld r;
  Circle() : p(Point(0, 0)), r(0) {}
  Circle(Point p, ld r) : p(p), r(r) {}
};

// ccw
// 1: a,b,c??????????¨???¨?????????????????¶
//-1: a,b,c???????¨???¨?????????????????¶
// 2: c,a,b???????????´???????????¶
//-2: a,b,c???????????´???????????¶
// 0: a,c,b???????????´???????????¶
int ccw(const Point &a, const Point &b, const Point &c) {
  const Point nb(b - a);
  const Point nc(c - a);
  if (cross(nb, nc) > eps)
    return 1; // a,b,c??????????¨???¨?????????????????¶
  if (cross(nb, nc) < -eps)
    return -1; // a,b,c???????¨???¨?????????????????¶
  if (dot(nb, nc) < 0)
    return 2; // c,a,b???????????´???????????¶
  if (norm(nb) < norm(nc))
    return -2; // a,b,c???????????´???????????¶
  return 0;    // a,c,b???????????´???????????¶
}

/* ???????????? */

// ??´?????¨??´??????????????????
bool isis_ll(const Line &l, const Line &m) {
  return !eq(cross(l.b - l.a, m.b - m.a), 0);
}

// ??´?????¨?????????????????????
bool isis_ls(const Line &l, const Line &s) {
  return isis_ll(l, s) &&
         (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);
}

// ????????¨?????????????????????
bool isis_ss(const Line &s, const Line &t) {
  return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
         ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}

// ????????´????????????
bool isis_lp(const Line &l, const Point &p) {
  return (abs(cross(l.b - p, l.a - p)) < eps);
}

// ?????????????????????
bool isis_sp(const Line &s, const Point &p) {
  return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);
}

// ??????????¶?
Point proj(const Line &l, const Point &p) {
  ld t = dot(p - l.a, l.b - l.a) / norm(l.a - l.b);
  return l.a + t * (l.b - l.a);
}

//???????±??????????????????????
Point reflect(const Line &l, const Point &p) {
  Point pr = proj(l, p);
  return pr * 2.0 - p;
}

// ??´?????¨??´????????????
Point is_ll(const Line &s, const Line &t) {
  Point sv = s.b - s.a, tv = t.b - t.a;
  assert(cross(sv, tv) != 0);
  return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}
// ??´?????¨??´????????????
vector<Point> is_ll2(const Line &s, const Line &t) {
  Point sv = s.b - s.a, tv = t.b - t.a;
  if (cross(sv, tv) != 0)
    return vector<Point>(1, is_ll(s, t));
  else {
    vector<Point> ans;
    for (int k = 0; k < 2; ++k) {
      if (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end())
        ans.push_back(t[k]);
      if (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end())
        ans.push_back(s[k]);
    }
    return ans;
  }
}
// ????????¨???????????????
//???????????£????????¨???????????¨assert(false)
Point is_ss(const Line &s, const Line &t) {
  if (isis_ss(s, t)) {
    for (int k = 0; k < 2; ++k) {
      for (int l = 0; l < 2; ++l) {
        if (s[k] == t[l])
          return s[k];
      }
    }
    return is_ll(s, t);
  } else {
    //??????isis_ss?????????
    assert(false);
    return Point(0, 0);
  }
}
// ????????¨???????????????
vector<Point> is_ss2(const Line &s, const Line &t) {
  vector<Point> kouho(is_ll2(s, t));
  vector<Point> ans;
  for (auto p : kouho) {
    if (isis_sp(s, p) && isis_sp(t, p))
      ans.emplace_back(p);
  }
  return ans;
}
// ??´?????¨???????????¢
ld dist_lp(const Line &l, const Point &p) { return abs(p - proj(l, p)); }

//??´?????¨??´???????????¢
ld dist_ll(const Line &l, const Line &m) {
  return isis_ll(l, m) ? 0 : dist_lp(l, m.a);
}

// ??´?????¨??????????????¢
ld dist_ls(const Line &l, const Line &s) {
  return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));
}

// ????????¨???????????¢
ld dist_sp(const Line &s, const Point &p) {
  Point r = proj(s, p);
  return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
}

// ????????¨??????????????¢
ld dist_ss(const Line &s, const Line &t) {
  return min(min(dist_sp(s, t.a), dist_sp(s, t.b)),
             min(dist_sp(t, s.a), dist_sp(t, s.b)));
}

int main() {
  ld ans = 1e18;
  int N;
  cin >> N;
  vector<Line> ls;
  map<pair<int, int>, int> mp;
  for (int i = 0; i < N; ++i) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (c < a) {
      swap(a, c);
      swap(b, d);
    }
    ls.push_back(Line(Point(a, b), Point(c, d)));
    mp[make_pair(a, b)]++;
    mp[make_pair(c, d)]++;
  }
  random_device rnd;
  mt19937 mt(rnd());
  shuffle(ls.begin(), ls.end(), mt);
  bool flag = false;
  for (auto m : mp) {
    if (m.second >= 2)
      flag = true;
  }

  if (flag) {
    ans = 0;
  } else {
    for (int i = 0; i < N; ++i) {
      for (int j = i + 1; j < N; ++j) {
        if (ls[i].b.real() + ans - eps < ls[j].a.real()) {
          continue;
        }
        if (isis_ss(ls[i], ls[j]))
          ans = 0;
        else {
          ans = min(ans, dist_ss(ls[i], ls[j]));
        }
      }
      if (ans < eps)
        break;
    }
  }
  cout << setprecision(10) << fixed << ans << endl;

  return 0;
}