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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
using W = double;
using P = complex<W>;
using L = pair<P, P>;
using C = pair<P, W>;
using Poly = vector<P>;
#define X real()
#define Y imag()
const W EPS = (1e-7), pi = acos(-1);

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
bool cmp_y(const P &a, const P &b) {
  return a.Y != b.Y ? a.Y < b.Y : a.X < b.X;
}
bool operator==(const P &a, const P &b) { return abs(a - b) < EPS; }
} // namespace std

double dot(P a, P b) { return a.X * b.X + a.Y * b.Y; }
W cross(P a, P b) { return a.X * b.Y - a.Y * b.X; }
P projection(P a, P b) { return dot(a, b) / abs(b) / abs(b) * b; }

// 直線aと直線bの交点
P intersection(L a, L b) {
  P af = a.first, as = a.second, bf = b.first, bs = b.second;
  return af + cross(bs - bf, af - bf) /
                  (-cross(bs - bf, as - bf) + cross(bs - bf, af - bf)) *
                  (as - af);
}

int main() {
  int n;
  P a = P(1, 0) * P(cos(-2 * pi / 3), sin(-2 * pi / 3));
  P b = P(1, 0) * P(cos(-1 * pi / 3), sin(-1 * pi / 3));
  while (cin >> n, n) {
    int k;
    cin >> k;
    vector<P> ps;
    vector<W> ys;
    for (int i = 0; i < n; ++i) {
      W x, y;
      cin >> x >> y;
      ps.emplace_back(x, y);
      ys.push_back(y);
    }
    sort(ps.begin(), ps.end(), cmp_y);
    sort(ys.begin(), ys.end());
    int t = 0;
    W ans = 1e9;
    L base(P(0, 0), P(1, 0));
    vector<pair<W, int>> V;
    for (int j = 0; j < n; ++j) {
      P p = ps[j];
      L l(p, p + a), r(p, p + b);
      V.emplace_back(intersection(base, l).X, -j - 1);
      V.emplace_back(intersection(base, r).X, j + 1);
    }
    sort(V.begin(), V.end());
    vector<bool> alive(n, true);
    for (int i = 0; i < n; ++i) {
      if (i > 0 and abs(ys[i] - ys[i - 1]) < EPS)
        continue;
      while (ps[t].Y < ys[i] - EPS) {
        alive[t] = false;
        ++t;
      }
      if (t > k)
        break;
      W r = 0;
      int s = 0;
      size_t m = 0;
      vector<bool> alive_ = alive;
      vector<bool> used(n, false);
      W d = ys[i] / sqrt(3) * 2;
      for (size_t j = 0; j < V.size(); ++j) {
        W l = V[j].first;
        int idx = V[j].second;
        if (idx >= 0)
          continue;
        if (not alive_[-idx - 1])
          continue;
        while (m < V.size() and s < n - k) {
          int idx_ = V[m].second;
          if (idx_ <= 0 or (not alive_[idx_ - 1]) or V[m].first < l + d - EPS) {
            ++m;
            continue;
          }
          r = V[m].first;
          ++s;
          ++m;
          used[idx_ - 1] = true;
        }
        if (s < n - k)
          break;
        ans = min(ans, r - l - d);
        alive_[-idx - 1] = false;
        if (used[-idx - 1]) {
          --s;
          used[-idx - 1] = false;
        }
      }
    }
    printf("%.12f\\n", ans * 3);
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
using W = double;
using P = complex<W>;
using L = pair<P, P>;
using C = pair<P, W>;
using Poly = vector<P>;
#define X real()
#define Y imag()
const W EPS = (1e-10), pi = acos(-1);

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
bool cmp_y(const P &a, const P &b) {
  return a.Y != b.Y ? a.Y < b.Y : a.X < b.X;
}
bool operator==(const P &a, const P &b) { return abs(a - b) < EPS; }
} // namespace std

double dot(P a, P b) { return a.X * b.X + a.Y * b.Y; }
W cross(P a, P b) { return a.X * b.Y - a.Y * b.X; }
P projection(P a, P b) { return dot(a, b) / abs(b) / abs(b) * b; }

// 直線aと直線bの交点
P intersection(L a, L b) {
  P af = a.first, as = a.second, bf = b.first, bs = b.second;
  return af + cross(bs - bf, af - bf) /
                  (-cross(bs - bf, as - bf) + cross(bs - bf, af - bf)) *
                  (as - af);
}

int main() {
  int n;
  P a = P(1, 0) * P(cos(-2 * pi / 3), sin(-2 * pi / 3));
  P b = P(1, 0) * P(cos(-1 * pi / 3), sin(-1 * pi / 3));
  while (cin >> n, n) {
    int k;
    cin >> k;
    vector<P> ps;
    vector<W> ys;
    for (int i = 0; i < n; ++i) {
      W x, y;
      cin >> x >> y;
      ps.emplace_back(x, y);
      ys.push_back(y);
    }
    sort(ps.begin(), ps.end(), cmp_y);
    sort(ys.begin(), ys.end());
    int t = 0;
    W ans = 1e9;
    for (int i = 0; i < n; ++i) {
      if (i > 0 and abs(ys[i] - ys[i - 1]) < EPS)
        continue;
      while (ps[t].Y < ys[i] - EPS)
        ++t;
      if (t > k)
        break;
      L base(P(0, ys[i]), P(1, ys[i]));
      vector<pair<W, int>> V;
      vector<W> R(n);
      for (int j = t; j < n; ++j) {
        P p = ps[j];
        L l(p, p + a), r(p, p + b);
        V.emplace_back(intersection(base, l).X, -j - 1);
        V.emplace_back(intersection(base, r).X, j + 1);
        R[i] = intersection(base, r).X;
      }
      sort(V.begin(), V.end());
      vector<bool> alive(n, true);
      W r_ = 0;
      int s = 0;
      size_t m = 0;
      for (size_t j = 0; j < V.size(); ++j) {
        W l = V[j].first;
        int idx = V[j].second;
        if (idx >= 0)
          continue;
        W r = R[-idx - 1];
        while (m < V.size() and s < n - k) {
          int idx_ = V[m].second;
          if (idx_ < 0) {
            ++m;
            continue;
          }
          if (not alive[idx_ - 1]) {
            ++m;
            continue;
          }
          r_ = max(r_, V[m].first);
          ++s;
          ++m;
        }
        if (s < n - k)
          break;
        ans = min(ans, (max(r_, r) - l));
        alive[-idx - 1] = false;
        if (r <= r_ + EPS)
          --s;
      }
    }
    printf("%.12f\\n", ans * 3);
  }
}