alexjercan/bugnet huggingface dataset:
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int n;
vi v;
double dp[22][222222];

ld solve(int i, int t) {
  if (i == n)
    return 0;
  if (dp[i][t] >= 0)
    return dp[i][t];
  ld tmp = (abs(t - v[i]) / (ld)v[i]);
  ld res = INF;
  for (int j = 1;; j++) {
    ld nx = t * j;
    if (nx > v.back() * 2)
      break;
    chmin(res, max(solve(i + 1, nx), tmp));
  }
  return dp[i][t] = res;
}

int main() {
  MS(dp, -1);
  cin >> n;
  v.resize(n);
  REP(i, n) cin >> v[i];
  ld ans = INF;
  FOR(i, 1, v.back() + 1) { chmin(ans, solve(0, i)); }
  cout << D10 << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int n;
vi v;
ld dp[22][222222];

ld solve(int i, int t) {
  if (i == n)
    return 0;
  if (dp[i][t] >= 0)
    return dp[i][t];
  ld tmp = (abs(t - v[i]) / (ld)v[i]);
  ld res = INF;
  for (int j = 1;; j++) {
    ld nx = t * j;
    if (nx > v.back())
      break;
    chmin(res, max(solve(i + 1, nx), tmp));
  }
  return dp[i][t] = res;
}

int main() {
  MS(dp, -1);
  cin >> n;
  v.resize(n);
  REP(i, n) cin >> v[i];
  ld ans = INF;
  FOR(i, 1, v.back() + 1) { chmin(ans, solve(0, i)); }
  cout << D10 << ans << endl;
  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

namespace {
typedef double real;

int N;
vector<int> A;
void input() {
  cin >> N;
  A.clear();
  A.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
}

const int L = int(1e5) * 2 + 5;
const real INF = 1e9;

real calc(int a, int b) {
  // ある貨幣を価値aから価値bに変更するときのコスト
  return abs(real(a - b)) / real(a);
}

void solve() {
  /* dp[i][j] :: i番の貨幣価値をjにして、0 ~
   * i番までの貨幣体系がbeautifulになるよう調整する最小コスト */
  vector<vector<real>> dp(N, vector<real>(L, INF));
  for (int j = 1; j <= 2 * A[0]; j++) {
    dp[0][j] = calc(A[0], j);
  }
  for (int i = 1; i < N; i++) {
    for (int j = 1; j <= 2 * A[i]; j++) { // コストが1を越えることはないので,
                                          // もとの価値の高々2倍までみればよい
      int x = j;
      real ncost = calc(A[i], j);
      for (int k = 1; k * k <= x; k++) {
        if (x % k == 0) {
          int a = k, b = x / k;
          dp[i][j] = min(dp[i][j], max(dp[i - 1][a], ncost));
          dp[i][j] = min(dp[i][j], max(dp[i - 1][b], ncost));
        }
      }
    }
  }
  real ans = 1.0;
  for (int j = 1; j <= 2 * A[N - 1]; j++) {
    ans = min(ans, dp[N - 1][j]);
  }
  printf("%.12lf\\n", ans);
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

namespace {
typedef long double real;

int N;
vector<int> A;
void input() {
  cin >> N;
  A.clear();
  A.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
}

const int L = int(1e5) * 2;
const real INF = 1e9;

real calc(int a, int b) {
  // ある貨幣を価値aから価値bに変更するときのコスト
  return abs(real(a - b)) / a;
}

void solve() {
  /* dp[i][j] :: i番の貨幣価値をjにして、0 ~
   * i番までの貨幣体系がbeautifulになるよう調整する最小コスト */
  vector<vector<real>> dp(N, vector<real>(L, INF));
  for (int j = 1; j <= 2 * A[0]; j++) {
    dp[0][j] = calc(A[0], j);
  }
  for (int i = 1; i < N; i++) {
    for (int j = 1; j <= 2 * A[i]; j++) { // コストが1を越えることはないので,
                                          // もとの価値の高々2倍までみればよい
      int x = j;
      for (int k = 1; k * k <= x; k++) {
        if (x % k == 0) {
          int a = k, b = x / k;
          dp[i][j] = min(dp[i][j], max(dp[i - 1][a], calc(A[i], j)));
          dp[i][j] = min(dp[i][j], max(dp[i - 1][b], calc(A[i], j)));
        }
      }
    }
  }
  real ans = INF;
  for (int j = 1; j <= 2 * A[N - 1]; j++) {
    ans = min(ans, dp[N - 1][j]);
  }
  printf("%.12Lf\\n", ans);
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

// Enjoy your stay.

#include <bits/stdc++.h>

#define ECH(it, A)                                                             \\
  for (__typeof((A).begin()) it = (A).begin(); it != (A).end(); ++it)
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

typedef istringstream iss;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;

int N, a[22], L[22], H[22];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> N;
  rep(i, N) cin >> a[i];
  double lo = 0, hi = 1;
  rep(_, 40) {
    double mi = (lo + hi) / 2;
    rep(i, N) {
      L[i] = ceil(a[i] * (1 - mi));
      H[i] = floor(a[i] * (1 + mi));
    }
    set<int> cur, nx;
    rep2(i, L[0], H[0] + 1) { cur.insert(i); }
    rep2(i, 1, N) {
      if (sz(cur) == 0)
        break;
      nx.clear();
      ECH(it, cur) {
        int t = *it;
        for (int k = (L[i] + t - 1) / t * t; k <= H[i] / t * t; k += t) {
          // if(!memo[k]){
          nx.insert(k);
          // memo[k] = 1;
          //}
        }
      }
      cur = nx;
    }
    if (sz(cur))
      hi = mi;
    else
      lo = mi;
  }
  cout << fixed << setprecision(16) << hi << endl;
}

Error: MLE

Fixed submission:

// Enjoy your stay.

#include <bits/stdc++.h>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

typedef istringstream iss;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;

int N, a[22], L[22], H[22];
int memo[200010];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> N;
  rep(i, N) cin >> a[i];
  double lo = 0, hi = 1;
  rep(_, 40) {
    double mi = (lo + hi) / 2;
    rep(i, N) {
      L[i] = ceil(a[i] * (1 - mi));
      H[i] = floor(a[i] * (1 + mi));
    }
    vector<int> cur, nx;
    rep2(i, L[0], H[0] + 1) { cur.pb(i); }
    rep2(i, 1, N) {
      if (sz(cur) == 0)
        break;
      nx.clear();
      memset(memo, 0, sizeof(memo));
      rep(j, sz(cur)) {
        for (int k = (L[i] + cur[j] - 1) / cur[j] * cur[j];
             k <= H[i] / cur[j] * cur[j]; k += cur[j]) {
          // if(!memo[k]){
          nx.pb(k);
          // memo[k] = 1;
          //}
        }
      }
      cur = nx;
    }
    if (sz(cur) > 0)
      hi = mi;
    else
      lo = mi;
  }
  cout << fixed << setprecision(16) << hi << endl;
}
Problem p01422 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1e9 + 1)

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];

  static double dp[21][250011];
  rep(i, 21) rep(j, 250011) dp[i][j] = INF;
  dp[0][1] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 250010; j++) {
      double diff = (double)abs(v[i - 1] - j) / v[i - 1];
      int sq = sqrt(j);
      for (int k = 1; k <= sq; k++) {
        if (j % k == 0) {
          dp[i][j] = min(dp[i][j], max(dp[i - 1][k], diff));
          dp[i][j] = min(dp[i][j], max(dp[i - 1][j / k], diff));
        }
      }
    }
  }
  double ans = INF;
  rep(i, 250011) ans = min(ans, dp[n][i]);
  printf("%.20lf\\n", ans);
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1e9 + 1)

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];

  static double dp[21][300011];
  rep(i, 21) rep(j, 300011) dp[i][j] = INF;
  dp[0][1] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 300010; j++) {
      double diff = (double)abs(v[i - 1] - j) / v[i - 1];
      int sq = sqrt(j);
      for (int k = 1; k <= sq; k++) {
        if (j % k == 0) {
          dp[i][j] = min(dp[i][j], max(dp[i - 1][k], diff));
          dp[i][j] = min(dp[i][j], max(dp[i - 1][j / k], diff));
        }
      }
    }
  }
  double ans = INF;
  rep(i, 300011) ans = min(ans, dp[n][i]);
  printf("%.20lf\\n", ans);
}
Problem p01422 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define INF 0x7fffffff
#define Max 200000
#define eps 1e-10

int arr[21];

double dp[22][Max];

double getval(double a, double b) {
  double temp = (b - a) / a;
  if (temp < eps)
    return -temp;
  else
    return temp;
}

double MIN(double a, double b) { return a < b ? a : b; }

int main() {
  int n, i, j, k;
  while (scanf("%d", &n) == 1) {
    for (i = 1; i <= n; ++i)
      scanf("%d", &arr[i]);

    for (i = 1; i <= Max; i++)
      dp[0][i] = INF;
    dp[0][1] = 0.0;
    for (i = 1; i <= n; ++i) {
      for (k = 1; k <= Max; k++)
        dp[i][k] = INF;
      for (j = 1; j <= Max; ++j) {
        for (k = 1; j * k <= Max; ++k) {
          double temp = max(getval(arr[i], j * k), dp[i - 1][j]);
          dp[i][j * k] = min(temp, dp[i][j * k]);
        }
      }
    }

    double ans = INF;
    for (i = 1; i <= Max; ++i)
      ans = min(ans, dp[n][i]);
    printf("%.12lf\\n", ans);
  }
}
/*
3
6 11 12
3
6 11 24
3
6 11 30
*/

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define INF 10500000000000ll
#define eps 1e-8
#define inf 999999999
#define LL long long
#define M 2005
double dp[25][100005];
int abs1(int x, int y) {
  if (x - y > 0)
    return x - y;
  else
    return y - x;
}
double min(double x, double y) {
  if (x > y)
    return y;
  else
    return x;
}
double max(double x, double y) {
  if (x > y)
    return x;
  else
    return y;
}
int path[25][100005];
int a[25];
double ans;
void Print(int n, int x) {
  if (path[n][x] == -1)
    return;
  Print(n - 1, path[n][x]);
  ans = max(ans, abs1(x, a[n]) / (a[n] * 1.0));
}
int main() {
  int n;
  while (scanf("%d", &n) == 1) {
    int i, j, k;
    for (i = 1; i <= n; i++)
      scanf("%d", &a[i]);
    memset(dp, 0x7f, sizeof(dp));
    for (i = 0; i <= 100000; i++)
      dp[0][i] = 0;
    dp[1][a[1]] = 0;
    memset(path, -1, sizeof(path));
    int xx = min(a[n] * 2, 100000);
    for (i = 1; i <= n; i++)
      for (j = xx; j >= 1; j--) {
        for (k = 1; k <= j; k++)
          if (j % k == 0) {
            if (dp[i][j] > dp[i - 1][j / k] + abs1(a[i], j) / (a[i] * 1.0))
              dp[i][j] = dp[i - 1][j / k] + abs1(a[i], j) / (a[i] * 1.0),
              path[i][j] = j / k;
          }
      }
    ans = inf;
    int kk = 0;
    for (j = xx; j >= 1; j--)
      if (ans > dp[n][j])
        ans = dp[n][j], kk = j;
    ans = 0;
    Print(n, kk);
    printf("%.9lf\\n", ans);
  }
}
Problem p01422 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <ctype.h>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define LL long long
#define MAXN 200000
int f[21];
double dp[29][400009];

double deal(int p, int val) {
  double rs = 1.0;
  for (int i = 1; (i * val) <= (MAXN); i++) {
    rs = min(rs, dp[p + 1][i * val]);
  }
  return rs;
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < n; i++)
      cin >> f[i];
    sort(f, f + n);
    for (int i = 0; i <= n; i++)
      for (int j = 1; j <= MAXN; j++)
        dp[i][j] = 1.0;
    for (int i = 1; i <= MAXN; i++)
      dp[n - 1][i] = 1.0 * abs(f[n - 1] - i) / f[n - 1];
    for (int i = n - 2; i >= 0; i--) {
      for (int j = 1; j <= MAXN; j++) {
        dp[i][j] = max(deal(i, j), 1.0 * abs(f[i] - j) / f[i]);
      }
    }
    double ans = 1.0;
    for (int i = 1; i <= MAXN; i++) {
      ans = min(dp[0][i], ans);
    }
    printf("%.19lf\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define INF 10500000000000ll
#define eps 1e-8
#define inf 999999999
#define LL long long
#define M 2005
double dp[25][100005];
int abs1(int x, int y) {
  if (x - y > 0)
    return x - y;
  else
    return y - x;
}
double min(double x, double y) {
  if (x > y)
    return y;
  else
    return x;
}
double max(double x, double y) {
  if (x > y)
    return x;
  else
    return y;
}
int path[25][100005];
int a[25];
double ans;
void Print(int n, int x) {
  if (path[n][x] == -1)
    return;
  Print(n - 1, path[n][x]);
  ans = max(ans, abs1(x, a[n]) / (a[n] * 1.0));
}
int main() {
  int n;
  while (scanf("%d", &n) == 1) {
    int i, j, k;
    for (i = 1; i <= n; i++)
      scanf("%d", &a[i]);
    memset(dp, 0x7f, sizeof(dp));
    for (i = 0; i <= 100000; i++)
      dp[0][i] = 0;
    int xx = dp[0][0];
    dp[1][a[1]] = 0;
    for (i = 1; i <= n; i++)
      for (j = 100000; j >= 1; j--) {
        dp[i][j] = inf;
        for (k = 1; k <= j; k++) {
          if (j % k == 0)
            dp[i][j] =
                min(dp[i][j], max(dp[i - 1][k], abs1(a[i], j) / (a[i] * 1.0)));
        }
      }
    ans = inf;
    int kk = 0;
    for (j = 100000; j >= 1; j--)
      if (ans > dp[n][j] && dp[n][j] != xx)
        ans = dp[n][j];
    printf("%.9lf\\n", ans);
  }
}
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <cstring>

inline int gcd(int a, int b) {
  while (a != 0) {
    int t = b % a;
    b = a;
    a = t;
  }
  return b;
}

inline int abs(int x) { return x < 0 ? -x : x; }

struct Frac {
  int a, b;
  Frac() : a(1), b(1) {}
  Frac(int a, int b) : a(a), b(b) { reduce(); }
  void reduce() {
    int g = gcd(a, b);
    a /= g;
    b /= g;
  }
  bool operator<(const Frac &f) const {
    return (long long)a * f.b < (long long)b * f.a;
  }
};

inline void checkMin(Frac &ret, const Frac &val) {
  if (val < ret) {
    ret = val;
  }
}

inline void checkMax(Frac &ret, const Frac &val) {
  if (ret < val) {
    ret = val;
  }
}

int n;
int a[100000];
Frac dp[2][200001];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= a[0] * 2; ++i) {
    dp[0][i] = Frac(0, 1);
  }
  for (int i = 0; i < n; ++i) {
    int lim = i + 1 == n ? 0 : a[i + 1] * 2;
    for (int j = 1; j <= a[i] * 2; ++j) {
      checkMax(dp[i % 2][j], Frac(abs(j - a[i]), a[i]));
      for (int k = j; k <= lim; k += j) {
        checkMin(dp[1 - i % 2][k], dp[i % 2][j]);
      }
    }
  }
  Frac res;
  for (int i = 1; i <= a[n - 1] * 2; ++i) {
    checkMin(res, dp[(n - 1) % 2][i]);
  }
  printf("%.13lf\\n", (double)res.a / res.b);
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>

inline int gcd(int a, int b) {
  while (a != 0) {
    int t = b % a;
    b = a;
    a = t;
  }
  return b;
}

inline int abs(int x) { return x < 0 ? -x : x; }

struct Frac {
  int a, b;
  Frac() : a(1), b(1) {}
  Frac(int a, int b) : a(a), b(b) { reduce(); }
  void reduce() {
    int g = gcd(a, b);
    a /= g;
    b /= g;
  }
  bool operator<(const Frac &f) const {
    return (long long)a * f.b < (long long)b * f.a;
  }
};

inline void checkMin(Frac &ret, const Frac &val) {
  if (val < ret) {
    ret = val;
  }
}

inline void checkMax(Frac &ret, const Frac &val) {
  if (ret < val) {
    ret = val;
  }
}

int n;
int a[100000];
Frac dp[21][200001];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= a[0] * 2; ++i) {
    dp[0][i] = Frac(0, 1);
  }
  for (int i = 0; i < n; ++i) {
    int lim = i + 1 == n ? 0 : a[i + 1] * 2;
    for (int j = 1; j <= a[i] * 2; ++j) {
      checkMax(dp[i][j], Frac(abs(j - a[i]), a[i]));
      for (int k = j; k <= lim; k += j) {
        checkMin(dp[i + 1][k], dp[i][j]);
      }
    }
  }
  Frac res;
  for (int i = 1; i <= a[n - 1] * 2; ++i) {
    checkMin(res, dp[n - 1][i]);
  }
  printf("%.13lf\\n", (double)res.a / res.b);
  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Time Limit Exceeded

// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define pb(a) push_back(a)
#define INF 0x1f1f1f1f
#define lson idx << 1, l, mid
#define rson idx << 1 | 1, mid + 1, r
#define PI 3.1415926535898
void debug() {
#ifdef ONLINE_JUDGE
#else
  freopen("d:\\\\in.txt", "r", stdin);
  freopen("d:\\\\out1.txt", "w", stdout);
#endif
}
char getch() {
  char ch;
  while ((ch = getchar()) != EOF) {
    if (ch != ' ' && ch != '\\n')
      return ch;
  }
  return EOF;
}
double dp[25][200100];
int da[25];
double minn = INF;
int n;
double f(int k, int fa) {
  if (dp[k][fa] >= 0)
    return dp[k][fa];
  if (k == n + 1)
    return 0;
  double minx = INF;
  for (int i = 1; fa * i <= 200000; i++) {
    int v = fa * i;
    if (da[k] < v && ((v - da[k]) * 1.0 / (da[k] * 1.0) > minx))
      break;
    if (da[k] > v && ((da[k] - v) * 1.0 / (da[k] * 1.0) > minx))
      continue;
    minx = min(minx, max(f(k + 1, v), abs(v - da[k]) * 1.0 / (da[k] * 1.0)));
  }
  return dp[k][fa] = minx;
}
int main() {
  // int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; i++)
      scanf("%d", &da[i]);
    memset(dp, -1, sizeof(dp));
    double x = f(1, 1);
    printf("%.12lf\\n", x);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int maxn = 20;
const double eps = 1e-10;
vector<int> p[maxn];
int s[maxn];
int n;
bool dp[maxn][100005];
inline int dcmp(const double &d) { return d < -eps ? -1 : d > eps; }
void checkmin(double &a, double b) {
  if (a == -1 || a > b)
    a = b;
}
bool gao(double x) {
  int i, j, k;
  int ll, rr;
  int ll2, rr2;
  memset(dp, false, sizeof(dp));
  for (i = 0; i < n; i++) {
    if (i == 0) {
      ll = (int)ceil(s[i] - s[i] * x);
      rr = (int)floor(s[i] + s[i] * x);
      for (j = ll; j <= rr; j++)
        dp[i][j] = true;
    } else {
      bool flag = false;
      ll2 = (int)ceil(s[i] - s[i] * x);
      rr2 = (int)floor(s[i] + s[i] * x);
      for (j = ll; j <= rr; j++) {
        if (dp[i - 1][j]) {
          for (k = ll2; k <= rr2; k++) {
            if (k % j == 0)
              dp[i][k] = true, flag = true;
          }
        }
      }
      if (!flag)
        return false;
      ll = ll2;
      rr = rr2;
    }
  }
  return true;
}
int main() {
  int i, j, k;
  double l, r;
  while (~scanf("%d", &n)) {
    for (i = 0; i < n; i++)
      scanf("%d", &s[i]);
    l = 0, r = 1;
    double res = 0;
    while (r - l > eps) {
      double m = (l + r) / 2;
      //	cout<<"m="<<m<<endl;
      if (gao(m)) {
        r = m;
        res = m;
      } else {
        l = m;
      }
    }
    printf("%.10f\\n", res);
  }
}
Problem p01422 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int a[25], n;
double dp[25][200005];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  rep(j, 25) rep(i, 200005) dp[j][i] = 1e9;
  dp[0][1] = 0;
  double ans = 1e9;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < 200005; j++) {
      for (int k = j; k < 200005; k += j) {
        if (dp[i - 1][j] < 1e8) {
          dp[i][k] = min(dp[i][k], max(dp[i - 1][j], (double)(abs(k - a[i])) /
                                                         (double)(a[i])));
          if (i == n)
            ans = min(ans, dp[i][k]);
        }
      }
    }
  }
  printf("%.12f\\n", ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int a[25], n;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  double lb = 0.0, ub = 1e5 + 5;
  rep(w, 50) {
    double mid = (lb + ub) / 2;
    vector<int> hoge;
    double f = (double)a[1] * mid;
    f = min(f, 1e6);
    int F = floor(f);
    F = min(F, 200000);
    int L = max(1, a[1] - F), R = a[1] + F;
    for (int i = L; i <= R; i++)
      hoge.pb(i);
    for (int i = 2; i <= n; i++) {
      vector<int> hoge2;
      f = (double)a[i] * mid;
      f = min(f, 1e6);
      F = floor(f);
      F = min(F, 200000);
      for (int d = max(1, a[i] - F); d <= a[i] + F; d++) {
        rep(e, hoge.size()) {
          if (d % hoge[e] == 0) {
            hoge2.pb(d);
            break;
          }
        }
      }
      hoge = hoge2;
    }
    if (hoge.size())
      ub = mid;
    else
      lb = mid;
  }
  printf("%.12f\\n", ub);
}