alexjercan/bugnet huggingface dataset:
Problem p01422 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const double INF = 1e9;
int N, a[20];
double dp[2][150001];
int main() {

  fill_n(*dp, 2 * 150001, INF);

  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i < 150001; ++i) {
    dp[0][i] = fabs(a[0] - i) / a[0];
  }
  for (int i = 0; i < N - 1; ++i) {
    for (int j = 1; j < 150001; ++j) {
      if (dp[i & 1][j] != INF) {
        for (int k = j; k < 150001; k += j) {
          dp[(i + 1) & 1][k] =
              min(dp[(i + 1) & 1][k],
                  max(dp[i & 1][j], fabs(a[i + 1] - k) / a[i + 1]));
        }
      }
      dp[i & 1][j] = 1e9;
    }
  }
  double ret = INF;
  for (int i = 0; i < 150001; ++i)
    ret = min(ret, dp[(N - 1) & 1][i]);
  cout << fixed << setprecision(10) << ret << endl;
}

Error: WA

Fixed submission:

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

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &a) {
  return is >> a.first >> a.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &a) {
  return os << a.first << " " << a.second;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vc) {
  for (int i = 0; i < vc.size(); i++)
    is >> vc[i];
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vc) {
  for (int i = 0; i < vc.size(); i++)
    os << vc[i] << endl;
  return os;
}

#define ForEach(it, c)                                                         \\
  for (__typeof(c).begin() it = (c).begin(); it != (c).end(); it++)
#define ALL(v) (v).begin(), (v).end()
#define UNQ(s)                                                                 \\
  {                                                                            \\
    sort(ALL(s));                                                              \\
    (s).erase(unique(ALL(s)), (s).end());                                      \\
  }
#define fr first
#define sc second

typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;

typedef long long int64;
const double INF = 1e9;

int N, a[20];
double dp[2][150001];

int main() {

  fill_n(*dp, 2 * 150001, INF);

  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i < 150001; ++i) {
    dp[0][i] = fabs(a[0] - i) / a[0];
  }
  for (int i = 0; i < N - 1; ++i) {
    for (int j = 1; j < 150001; ++j) {
      if (dp[i & 1][j] != INF) {
        for (int k = j; k < 150001; k += j) {
          dp[(i + 1) & 1][k] =
              min(dp[(i + 1) & 1][k],
                  max(dp[i & 1][j], fabs(a[i + 1] - k) / a[i + 1]));
        }
      }
      dp[i & 1][j] = 1e9;
    }
  }
  cout << fixed << setprecision(10) << *min_element(dp[(N - 1) & 1], dp[N & 1])
       << endl;
}
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define IINF (INT_MAX)
#define MAX 300000

using namespace std;

double dp[20][MAX];
int N;
double input[20];

int main() {

  cin >> N;
  rep(i, N) cin >> input[i];

  rep(i, N) rep(j, MAX) dp[i][j] = IINF;

  rep(i, MAX) { dp[0][i] = abs(input[0] - i) / input[0]; }

  rep(i, N - 1) {
    REP(j, 1, MAX) {
      if (dp[i][j] == IINF)
        continue;
      for (int k = j; k < MAX; k += j) {
        dp[i + 1][k] = min(dp[i + 1][k],
                           max(dp[i][j], abs(input[i + 1] - k) / input[i + 1]));
      }
    }
  }

  double ans = IINF;
  rep(i, MAX) { ans = min(ans, dp[N - 1][i]); }

  printf("%.12f\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define IINF (INT_MAX)
#define MAX 1000000

using namespace std;

double dp[22][MAX];
int N;
double input[22];

int main() {

  cin >> N;
  rep(i, N) cin >> input[i];

  rep(i, N) rep(j, MAX) dp[i][j] = IINF;

  rep(i, MAX) { dp[0][i] = abs(input[0] - i) / input[0]; }

  rep(i, N - 1) {
    REP(j, 1, MAX) {
      if (dp[i][j] == IINF)
        continue;
      for (int k = j; k < MAX; k += j) {
        dp[i + 1][k] = min(dp[i + 1][k],
                           max(dp[i][j], abs(input[i + 1] - k) / input[i + 1]));
      }
    }
  }

  double ans = IINF;
  rep(i, MAX) { ans = min(ans, dp[N - 1][i]); }

  printf("%.12f\\n", ans);
  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int N;
int A[22];
/*
double dp[2][2*100000+1];

double solve(int id,int dv){
  fill(dp[0],dp[2],1e15);
  double res = 1e15;
  for(int i=0;i<20;i++){
    int id = i&1;
    int nd = (i+1)&1;
    for(int j=0;j<=(1<<20);j++) dp[nd][j]=1e15;
    for(int dv=1;dv<=(1<<20);dv++){
      if( dp[id][dv] < -1e-9 ) continue;
      if( dp[id][dv] > res ) continue;
      printf("dp[%d][%d] = %.9lf\\n",i,dv,dp[id][dv]);
      if( id == N ) return dp[id][dv] = 0.0f;
      for(int j=dv;j<=(1<<20);j+=dv){
        dp[nd][j] = min( dp[nd][j], max( dp[id][dv],
                                         abs(A[i]-j)/(double)A[i]) );
        res = min(dp[nd][j],res);
      }
    }
  }
  return res;
  double ret = 1e15;
  for(int i=0;i<=(1<<20);i++) ret = min( ret, dp[N&1][i] );
  return ret;
}

*/
double dp[21][200001];
double solve(int id, int dv) {
  if (dp[id][dv] > -1e-9)
    return dp[id][dv];
  if (id == N)
    return dp[id][dv] = 0.0;
  double ret = 1e15;
  for (int i = dv; i < A[id] * 2; i += dv) {
    ret = min(ret, max(solve(id + 1, i), abs(A[id] - i) / (double)A[id]));
  }
  return dp[id][dv] = ret;
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &A[i]);
  fill(dp[0], dp[N], -1.0);
  printf("%.10lf\\n", solve(0, 1));
}

Error: MLE

Fixed submission:

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

int N;
int A[22];
double dp[22][(1 << 20) + 1];

double solve(int id, int dv) {
  if (dp[id][dv] > -1e-9)
    return dp[id][dv];
  if (id == N)
    return dp[id][dv] = 0.0f;
  double ret = 1e15;
  for (int i = dv; i <= (1 << 20); i += dv) {
    ret = min(ret, max(solve(id + 1, i), abs(A[id] - i) / (double)A[id]));
  }
  return dp[id][dv] = ret;
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++)
    scanf("%d", &A[i]);
  fill(dp[0], dp[N + 1], -1.0);
  printf("%.10lf\\n", solve(0, 1));
}
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
template <class T> using Table = vector<vector<T>>;

int main() {
  int N;
  cin >> N;
  vector<int> as;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    as.push_back(a);
  }
  vector<vector<ld>> dp(N, vector<ld>(200000, 10000));
  for (int b = 1; b < 3 * as[0]; ++b) {
    dp[0][b] = ld(abs(b - as[0])) / as[0];
  }
  for (int i = 0; i < N - 1; ++i) {
    for (int j = 1; j < as[i] * 2; ++j) {
      if (dp[i][j] > 1000)
        continue;
      for (int k = j; k <= as[i + 1] * 2; k += j) {
        dp[i + 1][k] = min(dp[i + 1][k],
                           max(dp[i][j], ld(abs(k - as[i + 1])) / as[i + 1]));
      }
    }
  }
  ld ans = 3;
  for (int j = 1; j < 2e5; ++j) {
    ans = min(ans, dp[N - 1][j]);
  }
  cout << setprecision(22) << fixed << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;

int main() {
  int N;
  cin >> N;
  vector<int> as;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    as.push_back(a);
  }
  vector<vector<ld>> dp(N, vector<ld>(2e5, 10000));
  for (int b = 1; b < 3 * as[0]; ++b) {
    dp[0][b] = ld(abs(b - as[0])) / as[0];
  }
  for (int i = 0; i < N - 1; ++i) {
    for (int j = 1; j < as[i] * 2; ++j) {
      if (dp[i][j] > 1000)
        continue;
      for (int k = j; k <= as[i + 1] * 2; k += j) {
        dp[i + 1][k] = min(dp[i + 1][k],
                           max(dp[i][j], ld(abs(k - as[i + 1])) / as[i + 1]));
      }
    }
  }
  ld ans = 3;
  for (int j = 1; j < 2e5; ++j) {
    ans = min(ans, dp[N - 1][j]);
  }
  cout << setprecision(22) << fixed << ans << endl;
  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n;
int current[30];
double memo[20][200020];

double calc(int depth, int prev) {
  if (depth == n) {
    return 0.0;
  }
  if (!isnan(memo[depth][prev])) {
    return memo[depth][prev];
  }
  double ret = 1e+100;
  int nv = prev;
  while (nv <= 200010) {
    ret = min(ret, max(calc(depth + 1, nv),
                       (double)abs(current[depth] - nv) / current[depth]));
    nv += prev;
  }
  return memo[depth][prev] = ret;
}

int main() {
  while (scanf("%d", &n) > 0) {
    MEMSET(memo, -1);
    REP(i, n) { scanf("%d", &current[i]); }
    sort(current, current + n);
    double ans = 1e+100;
    FOREQ(i, 1, 200010) { ans = min(ans, calc(0, i)); }
    printf("%.9f\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n;
int current[30];
double memo[30][200200];

double calc(int depth, int prev) {
  if (depth == n) {
    return 0.0;
  }
  if (!isnan(memo[depth][prev])) {
    return memo[depth][prev];
  }
  double ret = 1e+100;
  int nv = prev;
  while (nv <= 20000) {
    ret = min(ret, max(calc(depth + 1, nv),
                       (double)abs(current[depth] - nv) / current[depth]));
    nv += prev;
  }
  return memo[depth][prev] = ret;
}

int main() {
  while (scanf("%d", &n) > 0) {
    MEMSET(memo, -1);
    REP(i, n) { scanf("%d", &current[i]); }
    sort(current, current + n);
    double ans = 1e+100;
    FOREQ(i, 1, 200100) { ans = min(ans, calc(0, i)); }
    printf("%.8f\\n", ans);
  }
}
Problem p01422 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
double dp[21][200001];
const double INF = (1LL << 50);
int a[21];
int n;
double dfs(int idx, int lastNum) {
  if (idx == -1)
    return 0.0;
  if (!EQ(INF, dp[idx][lastNum]))
    return dp[idx][lastNum];
  double minCost = INF;
  if (idx == n - 1)
    for (int i = 1; i <= 200000; i++) {
      minCost =
          min(minCost, max(dfs(idx - 1, i), (double)abs(a[idx] - i) / a[idx]));
    }
  else
    // lastNumðèØéÅJÚ
    for (int i = 1; i * i <= lastNum; i++) {
      if (lastNum % i == 0) {
        minCost = min(minCost,
                      max(dfs(idx - 1, i), (double)abs(a[idx] - i) / a[idx]));
        minCost =
            min(minCost, max(dfs(idx - 1, (lastNum / i)),
                             (double)abs(a[idx] - (lastNum / i)) / a[idx]));
      }
    }
  return dp[idx][lastNum] = minCost;
}

int main() {
  for (int i = 0; i < 21; i++)
    for (int j = 0; j < 200001; j++)
      dp[i][j] = INF;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  // for(int i = 0; i < n; i++){
  //     if(i==0){
  //         for(int j=0; j <=200000;j++)
  //             dp[i+1][j]=min(dp[i+1][j],(double)abs(a[i]-j)/a[i]);
  //         continue;
  //     }
  //     for(int j = 0; j <= 200000; j++){
  //         //if(j%
  //     }
  // }
  double res = dfs(n - 1, 0);
  printf("%.9f\\n", res);
  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
double dp[21][200001];
const double INF = (1LL << 50);
int a[21];
int n;
double dfs(int idx, int lastNum) {
  if (idx == -1)
    return 0.0;
  if (!EQ(INF, dp[idx][lastNum]))
    return dp[idx][lastNum];
  double minCost = INF;
  if (idx == n - 1)
    for (int i = 1; i <= 200000; i++)
      minCost =
          min(minCost, max(dfs(idx - 1, i), (double)abs(a[idx] - i) / a[idx]));
  else
    // lastNumðèØéÅJÚ
    for (int i = 1; i <= lastNum; i++)
      if (lastNum % i == 0)
        minCost = min(minCost,
                      max(dfs(idx - 1, i), (double)abs(a[idx] - i) / a[idx]));
  return dp[idx][lastNum] = minCost;
}

int main() {
  for (int i = 0; i < 21; i++)
    for (int j = 0; j < 200001; j++)
      dp[i][j] = INF;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  // for(int i = 0; i < n; i++){
  //     if(i==0){
  //         for(int j=0; j <=200000;j++)
  //             dp[i+1][j]=min(dp[i+1][j],(double)abs(a[i]-j)/a[i]);
  //         continue;
  //     }
  //     for(int j = 0; j <= 200000; j++){
  //         //if(j%
  //     }
  // }
  double res = dfs(n - 1, 0);
  printf("%.9f\\n", res);
  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Memory 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-9;
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 n;
  cin >> n;
  vector<int> a(n);
  rep(i, 0, n) cin >> a[i];

  int maxi = 2 * 1e5;
  // vector<vector<long double>> dp(n,vector<long double>(maxi,infll));
  // rep(i,1,maxi) dp[0][i]=(long double)(abs(a[0]-i))/a[0];
  vector<long double> curr(maxi, infll), next(maxi, infll);
  rep(i, 1, maxi) curr[i] = (long double)(abs(a[0] - i)) / a[0];
  rep(i, 0, n - 1) {
    rep(j, 1, maxi) {
      for (int k = j; k < maxi; k += j) {
        next[k] = min(
            next[k], max(curr[j], (long double)(abs(a[i + 1] - k)) / a[i + 1]));
      }
    }
    swap(curr, next);
    fill(next.begin(), next.end(), infll);
  }
  cout << *min_element(curr.begin(), curr.end()) << endl;
}

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

Error: MLE

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-9;
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 n;
  cin >> n;
  vector<int> a(n);
  rep(i, 0, n) cin >> a[i];

  int maxi = 2 * 1e5;
  vector<vector<long double>> dp(n, vector<long double>(maxi, infll));
  rep(i, 1, maxi) dp[0][i] = (long double)(abs(a[0] - i)) / a[0];
  rep(i, 0, n - 1) {
    rep(j, 1, maxi) {
      for (int k = j; k < maxi; k += j) {
        dp[i + 1][k] =
            min(dp[i + 1][k],
                max(dp[i][j], (long double)(abs(a[i + 1] - k)) / a[i + 1]));
      }
    }
  }
  cout << *min_element(dp[n - 1].begin(), dp[n - 1].end()) << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(12);
  solve();
  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for (auto &i : vec)
    cin >> i;

  double l = 0., r = 1.0;
  for (int c = 0; c < 100; c++) {
    double m = (l + r) / 2.0;
    set<int> s;
    s.insert(1);

    for (int i = 0; i < N; i++) {
      int lo_n = ceil((1.0 - m) * (double)vec[i]);
      int up_n = (1.0 + m) * vec[i];

      set<int> ns;
      for (auto j : s) {
        int v = up_n / j;
        int u = ceil((double)lo_n / j);
        for (; u <= v; u++)
          if (u != 0)
            ns.insert(j * u);
      }

      s = ns;
    }

    if (s.size() > 0)
      r = m;
    else
      l = m;
  }

  printf("%.10f\\n", l);

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for (auto &i : vec)
    cin >> i;

  double l = 0., r = 1.0;
  for (int c = 0; c < 100; c++) {
    double m = (l + r) / 2.0;
    set<int> s;
    for (int j = ceil((1.0 - m) * vec[0]); j <= (int)((1.0 + m) * vec[0]);
         j++) {
      s.insert(j);
    }

    for (int i = 0; i < N - 1; i++) {
      int lo_n = ceil((1.0 - m) * vec[i + 1]);
      int up_n = (1.0 + m) * vec[i + 1];

      set<int> ns;
      for (int j = lo_n; j <= up_n; j++) {
        for (auto k : s) {
          if (j % k == 0) {
            ns.insert(j);
          }
        }
      }

      s = ns;
    }

    if (s.size() > 0)
      r = m;
    else
      l = m;
  }

  printf("%.10f\\n", l);

  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

const double eps = 1e-9;
int N;
int a[20];
vector<double> v;

bool equals(double s, double t) { return fabs(s - t) < eps; }

double cost(int s, int t) { return (double)abs(s - t) / s; }

bool rec(int bx, int k, double lim) {
  if (k == N)
    return true;
  for (int b = bx;; b += bx) {
    double newV = cost(a[k], b);
    if (newV < lim || equals(newV, lim)) {
      if (rec(b, k + 1, lim))
        return true;
    } else {
      if (a[k] < b)
        break;
      else
        continue;
    }
  }
  return false;
}

double solve() {
  v.push_back(0.0);
  for (int m = 1; m <= 100000; ++m) {
    for (int i = 0; i < N; ++i) {
      v.push_back((double)m / a[i]);
    }
  }
  sort(v.begin(), v.end());
  int l = 0;
  int r = v.size();
  int ansi = 0;
  while (l < r) {
    int m = (l + r) / 2;
    if (rec(1, 0, v[m])) {
      ansi = r = m;
    } else {
      l = m + 1;
    }
  }
  return v[ansi];
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  printf("%.12f\\n", solve());
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

const double eps = 1e-9;
int N;
int a[20];
vector<double> v;

bool equals(double s, double t) { return fabs(s - t) < eps; }

double cost(int s, int t) { return (double)abs(s - t) / s; }

bool rec(int bx, int k, double lim) {
  if (k == N)
    return true;
  for (int b = bx;; ++b) {
    if (b % bx)
      continue;
    double newV = cost(a[k], b);
    if (newV < lim || equals(newV, lim)) {
      if (rec(b, k + 1, lim))
        return true;
    } else {
      if (a[k] < b)
        break;
      else
        continue;
    }
  }
  return false;
}

double solve() {
  v.push_back(0.0);
  for (int m = 1; m <= 100000; ++m) {
    for (int i = 0; i < N; ++i) {
      v.push_back((double)m / a[i]);
    }
  }
  sort(v.begin(), v.end());
  int l = 0;
  int r = v.size();
  int ansi = 0;
  while (l < r) {
    int m = (l + r) / 2;
    if (rec(1, 0, v[m])) {
      ansi = r = m;
    } else {
      l = m + 1;
    }
  }
  return v[ansi];
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  printf("%.12f\\n", solve());
  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

// 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;
}

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 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();
      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]) {
          nx.pb(k);
        }
      }
      cur = nx;
    }
    if (sz(cur) > 0)
      hi = mi;
    else
      lo = mi;
  }
  cout << fixed << setprecision(16) << hi << endl;
}