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

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
template <typename T> using vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-5;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int d, n, b;
  cin >> d >> n >> b;
  vector<long double> p(n), h(n);
  rep(i, 0, n) cin >> p[i] >> h[i];

  auto ok = [&](long double vx, long double vy) {
    bool res = true;
    rep(i, 0, n) {
      int l = p[i] / (2 * vx * vy);
      long double t = (long double)p[i] / vx - 2 * vy * l;
      if ((long double)-t * t / 2 + vy * t >= h[i])
        continue;
      res = false;
      break;
    }
    return res;
  };

  long double pi = 3.14159265359, ans = infll;
  rep(i, 0, b + 1) {
    long double lb = pi / 4, ub = pi / 2;
    rep(j, 0, 64) {
      long double m = (lb + ub) / 2;
      long double v = sqrtl(d / sinl(2 * m) / (i + 1));
      long double vx = v * cosl(m), vy = v * sinl(m);
      if (ok(vx, vy)) {
        ub = m;
        ans = min(ans, v);
      } else
        lb = m;
    }
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
template <typename T> using vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-5;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int d, n, b;
  cin >> d >> n >> b;
  vector<long double> p(n), h(n);
  long double h_max = 0;
  rep(i, 0, n) {
    cin >> p[i] >> h[i];
    h_max = max(h_max, h[i]);
  }

  int loop_num = 1e8;
  long double ans = infll;
  long double lb = sqrtl(2 * h_max) - eps, ub = d * log10l(d);
  rep(i, 0, loop_num + 1) {
    long double vy = lb + (long double)i / loop_num * (ub - lb);
    if (ans <= vy * vy)
      break;
    for (int j = b + 1; j >= 1; --j) {
      long double vx = (long double)d / 2 / vy / j;
      if (ans <= vx * vx)
        break;
      long double tmp = vx * vx + vy * vy;
      if (ans <= tmp)
        continue;
      bool ok = true;
      rep(k, 0, n) {
        int l = p[k] / (2 * vx * vy);
        long double t = (long double)p[k] / vx - 2 * vy * l;
        if ((long double)-t * t / 2 + vy * t >= h[k])
          continue;
        ok = false;
        break;
      }
      if (ok)
        ans = min(ans, tmp);
    }
  }
  cout << sqrtl(ans) << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}