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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int d, n, b;
int p[SIZE], h[SIZE];

double calc(int sp) {
  double g = 1.0;

  double w = (double)d / sp;

  double l = sqrt(sqrt(w * w / 4)), r = 1e10;

  while (r - l > 1e-9) {
    double vy = (l + r) / 2;
    double t = vy / g;
    double vx = w / (t * 2);

    bool flag = true;

    for (int i = 0; i < n; i++) {
      double spp = p[i] - floor(p[i] / w) * w;

      if (spp > w / 2) {
        spp = w - spp;
      }

      if (spp < 1e-11) {
        flag = false;
        break;
      }

      if (h[i] > vy * spp / vx - g * (spp * spp) / (vx * vx) / 2) {
        flag = false;
        break;
      }
    }

    if (flag) {
      r = vy;
    } else {
      l = vy;
    }
  }

  double vy = l;
  double t = vy / g;
  double vx = w / (t * 2);

  return sqrt(vx * vx + vy * vy);
}

int main() {

  scanf("%d%d%d", &d, &n, &b);

  for (int i = 0; i < n; i++)
    scanf("%d%d", p + i, h + i);

  double ans = 1e15;

  for (int i = 0; i <= b; i++) {
    ans = min(ans, calc(i + 1));
  }

  printf("%.10lf\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int d, n, b;
int p[SIZE], h[SIZE];

double calc(int sp) {
  double g = 1.0;

  double w = (double)d / sp;

  double l = sqrt(sqrt(w * w / 4)), r = 1e10;

  while (r - l > 1e-13) {
    double vy = (l + r) / 2;
    double t = vy / g;
    double vx = w / (t * 2);

    bool flag = true;

    for (int i = 0; i < n; i++) {
      double spp = p[i] - floor(p[i] / w) * w;

      if (spp > w / 2) {
        spp = w - spp;
      }

      if (spp < 1e-11) {
        flag = false;
        break;
      }

      if (h[i] > vy * spp / vx - g * (spp * spp) / (vx * vx) / 2) {
        flag = false;
        break;
      }
    }

    if (flag) {
      r = vy;
    } else {
      l = vy;
    }
  }

  double vy = l;
  double t = vy / g;
  double vx = w / (t * 2);

  return sqrt(vx * vx + vy * vy);
}

int main() {

  scanf("%d%d%d", &d, &n, &b);

  for (int i = 0; i < n; i++)
    scanf("%d%d", p + i, h + i);

  double ans = 1e15;

  for (int i = 0; i <= b; i++) {
    ans = min(ans, calc(i + 1));
  }

  printf("%.10lf\\n", ans);

  return 0;
}