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

#include <cstdio>
#include <iostream>
#define N 25
#define inf 1
#define db double
#define swap(T, a, b)                                                          \\
  ({                                                                           \\
    T ttt = a;                                                                 \\
    a = b;                                                                     \\
    b = ttt;                                                                   \\
  })
using namespace std;
int n, a[N], L, R;
db t = 0, Ans = inf;
int abs(int x) { return x > 0 ? x : -x; }
db Up(int k, int x) {
  if (k == n + 1)
    return 0;
  int L = x, R = 2 * a[k] - 1, j = x;
  while (j + x <= R)
    j += x;
  db ans = inf, t;
  for (; j >= L; j -= x) {
    t = (db)abs(j - a[k]) / a[k];
    if (t < Ans)
      ans = min(ans, max(t, Up(k + 1, j)));
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] != 1)
      Ans = min(Ans, (db)(a[i] - 1) / a[i]);
  }
  int L = 1, R = 2 * a[1] - 1;
  db t;
  for (int j = R; j >= L; j--) {
    t = (db)abs(a[1] - j) / a[1];
    if (t > Ans)
      continue;
    t = max(t, Up(2, j));
    Ans = min(Ans, t);
  }
  if (Ans == inf)
    Ans = 0;
  printf("%.12lf", Ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#define N 25
#define inf 1
#define db double
#define swap(T, a, b)                                                          \\
  ({                                                                           \\
    T ttt = a;                                                                 \\
    a = b;                                                                     \\
    b = ttt;                                                                   \\
  })
using namespace std;
int n, a[N], L, R;
db t = 0, Ans = inf;
int abs(int x) { return x > 0 ? x : -x; }
db Up(int k, int x) {
  if (k == n + 1)
    return 0;
  /*int t = a[k] / x, tA = abs(x * t - a[k]), tB = abs(x * (t + 1) - a[k]);
  db c, d; c = d = inf;
  if (t && (db)tA / a[k] < Ans) c = max((db)tA / a[k], Up(k + 1, x * t));
  if ((db)tB / a[k] < Ans) d = max((db)tB / a[k], Up(k + 1, x * (t + 1)));
  return min(c, d);*/
  int L = x, R = 2 * a[k] - 1, j = x, t;
  while (j < L)
    j += x;
  double ans = inf;
  for (; j <= R; j += x) {
    t = abs(j - a[k]);
    if (j % x == 0 && (db)t / a[k] < Ans)
      ans = min(ans, max((db)t / a[k], Up(k + 1, j)));
  }
  return ans;
}
db Down(int k, int x) {
  if (!k)
    return 0;
  int t = x / a[k], tA = abs(a[k] * t - x), tB = abs(a[k] * (t + 1) - x);
  if (tA <= tB)
    return max((db)tA / a[k], Down(k - 1, x / t));
  else
    return max((db)tB / a[k], Down(k - 1, x / (t + 1)));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= 1; i++) {
    L = 1;
    R = 2 * a[i] - 1;
    for (int j = L; j <= R; j++) {
      t = (db)abs(a[i] - j) / a[i];
      if (i > 1)
        t = max(t, Down(i - 1, j));
      if (i < n)
        t = max(t, Up(i + 1, j));
      Ans = min(Ans, t);
    }
  }
  if (Ans == inf)
    Ans = 0;
  printf("%.12lf", Ans);
  return 0;
}