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

#include <cmath>
#include <iostream>
#include <list>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define sqr(x) ((x) * (x))
#define INF 10000000

double dptable[1 << 13][13];
double used[1 << 13][13];
double cost[13][13];
int vertex;
int width;

double xdistance(double r1, double r2) {
  if (r1 == 0 || r2 == 0)
    return max(r1, r2);
  return sqrt(sqr(r1 + r2) - sqr(r1 - r2));
}

void pre(string str) {
  vector<int> radius;
  stringstream sstr(str);
  int tmp;

  radius.push_back(0);
  sstr >> width;
  while (sstr >> tmp)
    radius.push_back(tmp);
  vertex = radius.size();

  /* cost */
  for (int i = 0; i < radius.size(); i++) {
    for (int j = 0; j < radius.size(); j++) {
      cost[i][j] = xdistance(radius[i], radius[j]);
    }
    cost[i][i] = 0;
  }

  /* dptable */
  for (int i = 0; i < 1 << 13; i++) {
    for (int j = 0; j < 13; j++) {
      dptable[i][j] = INF;
    }
  }

  /* used */
  for (int i = 0; i < 1 << 13; i++) {
    for (int j = 0; j < 13; j++) {
      used[i][j] = false;
    }
  }

  dptable[(1 << vertex) - 1][0] = 0;
}

double solve(int S, int u) {
  if (!used[S][u]) {
    for (int i = 0; i < vertex; i++) {
      if (!(S & (1 << i))) {
        dptable[S][u] = min(dptable[S][u], solve(S | (1 << i), i) + cost[u][i]);
      }
    }
    used[S][u] = true;
  }

  return dptable[S][u];
}

int main(void) {
  string str;

  while (getline(cin, str)) {
    pre(str);
    solve(0, 0);

    if (width >= dptable[0][0])
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <list>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define sqr(x) ((x) * (x))
#define INF 10000000

double dptable[1 << 13][13];
double cost[13][13];
int vertex;
int width;

double xdistance(double r1, double r2) {
  if (r1 == 0 || r2 == 0)
    return max(r1, r2);
  return sqrt(sqr(r1 + r2) - sqr(r1 - r2));
}

void pre(string str) {
  vector<int> radius;
  stringstream sstr(str);
  int tmp;

  radius.push_back(0);
  sstr >> width;
  while (sstr >> tmp)
    radius.push_back(tmp);
  vertex = radius.size();

  /* cost */
  for (int i = 0; i < radius.size(); i++) {
    for (int j = 0; j < radius.size(); j++) {
      cost[i][j] = xdistance(radius[i], radius[j]);
    }
    cost[i][i] = 0;
  }

  /* dptable */
  for (int i = 0; i < 1 << 13; i++) {
    for (int j = 0; j < 13; j++) {
      dptable[i][j] = INF;
    }
  }

  dptable[(1 << vertex) - 1][0] = 0;
}

double solve(int S, int u) {
  for (int i = 0; i < vertex; i++) {
    if (!(S & (1 << i))) {
      dptable[S][u] = min(dptable[S][u], solve(S | (1 << i), i) + cost[u][i]);
    }
  }

  return dptable[S][u];
}

int main(void) {
  string str;

  while (getline(cin, str)) {
    pre(str);
    solve(0, 0);

    if (width >= dptable[0][0])
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }

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

// 38
#include <cmath>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

map<pair<vector<int>, int>, double> memo;

vector<int> c;

double dfs(int nr, int p) {
  if (nr == 0) {
    return p;
  } else if (memo.count(make_pair(c, p))) {
    return memo[make_pair(c, p)];
  } else {
    double md = 1 << 30;
    for (int i = 3; i <= 10; i++) {
      if (c[i]) {
        c[i]--;
        md = min(md, dfs(nr - 1, i) + 2 * sqrt(i * p));
        c[i]++;
      }
    }
    memo[make_pair(c, p)] = md;
    return md;
  }
}

int main() {
  for (int w; cin >> w;) {
    string s;
    getline(cin, s);
    stringstream ss(s);
    c = vector<int>(11);
    memo.clear();
    int nr = 0;
    for (int r; ss >> r;) {
      c[r]++;
      nr++;
    }
    int i;
    for (i = 3; i <= 10; i++) {
      if (c[i]) {
        c[i]--;
        if (dfs(nr - 1, i) + i <= w)
          break;
        c[i]++;
      }
    }
    cout << ((i <= 10) ? "OK" : "NA") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 38
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int c[11];

bool dfs(double r, int nr, int p) {
  if (nr == 0) {
    return r >= p;
  } else {
    for (int i = 3; i <= 10; i++) {
      if (c[i]) {
        c[i]--;
        if (dfs(r - 2 * sqrt(i * p), nr - 1, i))
          return true;
        c[i]++;
      }
    }
    return false;
  }
}

int main() {
  for (int w; cin >> w;) {
    string s;
    getline(cin, s);
    stringstream ss(s);
    fill(c, c + 11, 0);
    int nr = 0;
    for (int r; ss >> r;) {
      c[r]++;
      nr++;
    }
    int i;
    for (i = 3; i <= 10; i++) {
      if (c[i]) {
        c[i]--;
        if (dfs(w - i, nr - 1, i))
          break;
        c[i]++;
      }
    }
    cout << ((i <= 10) ? "OK" : "NA") << endl;
  }
  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool f;
double w;
vector<double> v;
int n;
double calc(int i, int j) {
  return sqrt((v[i] + v[j]) * (v[i] + v[j]) - (v[i] - v[j]) * (v[i] - v[j]));
}
double memo[1 << 12][12];
void dfs(double l, int b, int p) {
  if (~p) {
    if (memo[b][p] <= l)
      return;
    memo[b][p] = l;
  }
  if (b == (1 << n) - 1) {
    f |= l + v[p] <= w;
    return;
  }
  if (p < 0)
    for (int i = 0; i < n; i++)
      dfs(v[i], b + (1 << i), i);
  else
    for (int i = 0; i < n; i++)
      if (!((b >> i) & 1))
        dfs(l + calc(p, i), b + (1 << i), i);
}
int main() {
  string s;
  while (getline(cin, s)) {
    v.clear();
    stringstream ss(s);
    ss >> w;
    double t;
    while (ss >> t)
      v.push_back(t);
    n = v.size();
    f = 0;

    for (int i = 0; i < (1 << n); i++)
      for (int j = 0; j < n; j++)
        memo[i][j] = 1e9;

    dfs(0, 0, -1);
    cout << (f ? "OK" : "NA") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool f;
double w;
vector<double> v;
int n;
double calc(int i, int j) {
  return sqrt((v[i] + v[j]) * (v[i] + v[j]) - (v[i] - v[j]) * (v[i] - v[j]));
}
void dfs(double l, int b, int p) {
  if (b == (1 << n) - 1) {
    f |= l + v[p] <= w;
    return;
  }
  if (p < 0)
    for (int i = 0; i < n; i++)
      dfs(v[i], b + (1 << i), i);
  else
    for (int i = 0; i < n; i++)
      if (!((b >> i) & 1))
        dfs(l + calc(p, i), b + (1 << i), i);
}
int main() {
  string s;
  while (getline(cin, s)) {
    v.clear();
    stringstream ss(s);
    ss >> w;
    double t;
    while (ss >> t)
      v.push_back(t);
    n = v.size();
    f = 0;
    dfs(0, 0, -1);
    cout << (f ? "OK" : "NG") << endl;
  }
  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

const int MAX_N = 12 + 1;
const double INF = 1e12;
int n, W;
double dp[1 << MAX_N][MAX_N]; // DPテーブル
double d[MAX_N][MAX_N];

vector<int> ToRadius(const string &s) {
  vector<int> res;

  for (int i = 0; i < s.size(); i++) {
    if (isdigit(s[i])) {
      int r = s[i] - '0';

      while (isdigit(s[i + 1])) {
        r *= 10;
        r += s[++i] - '0';
      }
      res.push_back(r);
    }
  }
  return res;
}

void solve() {
  for (int S = 0; S < 1 << n; ++S)
    fill(dp[S], dp[S] + n, INF);
  dp[(1 << n) - 1][0] = 0;

  for (int S = (1 << n) - 2; S >= 0; --S)
    for (int v = 0; v < n; ++v)
      for (int u = 0; u < n; ++u)
        if (!(S >> u & 1))
          dp[S][v] = min(dp[S][v], dp[S | 1 << u][u] + d[v][u]);

  if (W - dp[0][0] >= 0.0)
    cout << "OK\\n";
  else
    cout << "NA\\n";
}

int main() {
  string s;

  while (getline(cin, s)) {
    vector<int> c = ToRadius(s + " ");

    W = c[0];
    n = c.size();
    c[0] = 0;

    for (int i = 0; i < n; i++)
      d[0][i] = d[i][0] = (double)c[i];

    for (int i = 1; i < n; i++)
      for (int j = 1; j < n; j++)
        d[i][j] = d[j][i] = 2 * sqrt((double)c[i] * c[j]);

    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;

double r[14]; // r[0]が箱の大きさ
int n;

int to_number(const string &s) {
  int index = 0;

  for (int i = 0; i < s.size(); i++) {
    if ('0' <= s[i] && s[i] <= '9') {
      int tmp = s[i] - '0';

      while (i < s.size() && ('0' <= s[i + 1] && s[i + 1] <= '9')) {
        tmp *= 10;
        tmp += (s[++i] - '0');
      }
      r[index++] = tmp;
    }
  }
  return index;
}

int main() {
  string s;

  while (getline(cin, s)) {
    bool flag = false;

    s += " ";
    n = to_number(s);

    do {
      double w = 0.0;

      for (int i = 1; i < n; i++)
        w += 2.0 * sqrt((double)r[i] * r[i + 1]);
      w += r[1] + r[n - 1];

      if (w <= r[0]) {
        flag = true;
        break;
      }
    } while (next_permutation(r + 1, r + n));

    if (flag)
      cout << "OK\\n";
    else
      cout << "NA\\n";
  }

  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Runtime Error

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

const double INF = 1e5;

vector<double> v;
int n;
double len;
double dp[1 << 12][12];

double search(int s, int prev) {
  if (s == (1 << n) - 1) {
    return v[prev];
  }
  if (~prev && dp[s][prev] < INF) {
    return dp[s][prev];
  }

  double res = INF;
  for (int i = 0; i < n; i++) {
    int x = 1 << i;
    if (s & x)
      continue;
    if (s == 0) {
      res = min(res, v[i] + search(x, i));
    } else {
      double t = sqrt((v[i] + v[prev]) * (v[i] + v[prev]) -
                      (v[i] - v[prev]) * (v[i] - v[prev]));
      res = min(res, t + search(s | x, i));
    }
  }

  return dp[s][prev] = res;
}

int main() {
  string str;
  while (getline(cin, str)) {
    stringstream ss(str);

    len = -1;
    v.clear();
    fill((double *)dp, (double *)dp + (1 << 12) * 12, INF);

    while (!ss.eof()) {
      double tmp;
      ss >> tmp;
      if (len != -1)
        v.push_back(tmp);
      else
        len = tmp;
      ss.ignore();
    }

    n = v.size();

    double res = search(0, -1);
    if (res < len + 1e-6)
      puts("OK");
    else
      puts("NA");
  }
  return 0;
}

Error: 0

Fixed submission:

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

struct State {
  double length;
  int done, last;
  State(double a, int b, int c) : length(a), done(b), last(c) {}
  bool operator<(const State &t) const { return length > t.length; }
};

int main() {
  string str;
  while (getline(cin, str)) {
    stringstream ss(str);
    int length = -1;
    vector<int> v;
    while (!ss.eof()) {
      int tmp;
      ss >> tmp;
      if (~length)
        v.push_back(tmp);
      else
        length = tmp;
      ss.ignore();
    }

    int n = v.size();
    if (!n) {
      puts("OK");
      continue;
    }

    vector<vector<double>> d(15, vector<double>(1 << 15, 1e25));
    bool res = false;
    priority_queue<State> que;
    for (que.push(State(0.0, 0, -1)); que.size(); que.pop()) {
      State st = que.top();
      if (d[st.last + 1][st.done] < st.length)
        continue;
      d[st.last + 1][st.done] = st.length;
      if (st.done == (1 << n) - 1) {
        // printf("%f\\n", st.length + v[st.last]);
        if (st.length + v[st.last] <= length)
          res = true;
        else
          continue;
      }

      for (int i = 0; i < n; i++) {
        int x = 1 << i;
        if (st.done & x)
          continue;

        double nlen = st.length;
        if (~st.last) {
          nlen += sqrt((v[st.last] + v[i]) * (v[st.last] + v[i]) -
                       (v[st.last] - v[i]) * (v[st.last] - v[i]));
        } else {
          nlen += v[i];
        }
        int ndone = st.done | x;
        if (d[i + 1][ndone] < nlen)
          continue;
        que.push(State(nlen, ndone, i));
      }
    }
    puts(res ? "OK" : "NA");
  }
  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>

constexpr int MAX_N = 12;
constexpr double INF = (int)1e9;

int n, w;
int a[MAX_N];
double memo[MAX_N][(1 << MAX_N)];

double func(int x1, int x2) {
  return sqrt(pow(a[x1] + a[x2], 2) - pow(abs(a[x1] - a[x2]), 2));
}

double solve(int m, int S) {
  if (memo[m][S] > 0.0)
    return memo[m][S];
  if (S == (1 << n) - 1)
    return memo[m][S] = a[m];
  double res = INF;
  for (int i = 0; i < n; ++i) {
    if (!((S >> i) & 1))
      res = std::min(res, solve(i, S | (1 << i)) + func(m, i));
  }
  return memo[m][S] = res;
}

void output() {
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < (1 << n); ++j)
      memo[i][j] = 0.0;
  }
  for (int i = 0; i < n; ++i) {
    if (solve(i, (1 << i)) + a[i] <= (double)w) {
      std::cout << "OK" << std::endl;
      return;
    }
  }
  std::cout << "NA" << std::endl;
}

int main() {
  std::string str;
  while (1) {
    std::getline(std::cin, str);
    if (std::cin.eof())
      break;
    std::stringstream ss(str);
    ss >> w;
    n = 0;
    while (!ss.eof()) {
      ss >> a[n];
      ++n;
    }
    output();
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
const int MAX_N = 12;
const double INF = (1 << 16);

int n, l;
int r[MAX_N];
double memo[1 << MAX_N][MAX_N];

double solve(int s, int v) {
  if (s == (1 << n) - 1)
    return r[v];
  if (memo[s][v] != -1.0)
    return memo[s][v];
  double res = INF;
  for (int i = 0; i < n; ++i) {
    if (((s >> i) & 1) == 0)
      res = std::min(res, solve(s | (1 << i), i) +
                              sqrt(pow(r[i] + r[v], 2) - pow(r[i] - r[v], 2)));
  }
  return memo[s][v] = res;
}

int main() {
  while (1) {
    if (std::cin.eof())
      break;
    std::string str;
    std::getline(std::cin, str);
    std::stringstream s(str);
    s >> l;
    s.ignore();

    while (!s.eof()) {
      int a;
      s >> a;
      r[n] = a;
      ++n;
      s.ignore();
    }
    for (int i = 0; i < (1 << n); ++i) {
      for (int j = 0; j < n; ++j)
        memo[i][j] = -1.0;
    }
    double ans = INF;
    for (int i = 0; i < n; ++i) {
      ans = std::min(ans, solve((1 << i), i) + r[i]);
    }
    if (ans <= l)
      std::cout << "OK" << std::endl;
    else
      std::cout << "NA" << std::endl;
  }
  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
#define MAX_N 12
#define INF 10000
using namespace std;
vector<int> cakes;
int n;
double d[MAX_N][MAX_N];
double dp[1 << MAX_N][MAX_N];

void calc_dist() {
  int n = cakes.size();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) {
        d[i][j] = 0;
      } else {
        double dist =
            sqrt(pow(cakes[i] + cakes[j], 2) - pow(cakes[i] - cakes[j], 2));
        d[i][j] = dist;
      }
    }
  }
}

void solve() {
  n = cakes.size();
  for (int s = 0; s < (1 << n); s++) {
    for (int j = 0; j < n; j++) {
      dp[s][j] = INF;
    }
  }
  for (int i = 0; i < n; i++) {
    dp[1 << i][i] = cakes[i];
  }

  for (int s = 0; s < (1 << n); s++) {
    for (int v = 0; v < n; v++) {
      for (int u = 0; u < n; u++) {
        if (!(s >> u & 1)) {
          dp[s | (1 << u)][u] = min(dp[s | (1 << u)][u], dp[s][v] + d[v][u]);
        }
      }
    }
  }
}

int main() {
  int len;

  while (cin >> len) {
    cakes.clear();
    string str;
    getline(cin, str);
    stringstream ss(str);
    while (!ss.eof()) {
      int r;
      ss >> r;
      cakes.push_back(r);
    }
    calc_dist();
    solve();
    bool ok = false;
    for (int i = 0; i < n; i++) {
      if (dp[(1 << cakes.size()) - 1][i] + cakes[i] <= len) {
        ok = true;
      }
    }
    if (ok) {
      cout << "OK" << endl;
    } else {
      cout << "NA" << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define MAX_N 12
#define INF 10000
using namespace std;
vector<int> cakes;
double d[MAX_N][MAX_N];
double dp[1 << MAX_N][MAX_N];

void calc_dist() {
  for (int i = 0; i < cakes.size() - 1; i++) {
    for (int j = i + 1; j < cakes.size(); j++) {
      double dist =
          sqrt(pow(cakes[i] + cakes[j], 2) - pow(cakes[i] - cakes[j], 2));
      d[i][j] = d[j][i] = dist;
    }
  }
}

double solve() {
  int n = cakes.size();
  for (int s = 0; s < 1 << MAX_N; s++) {
    for (int j = 0; j < MAX_N; j++) {
      dp[s][j] = INF;
    }
  }

  dp[(1 << n) - 1][0] = 0;

  for (int s = (1 << n) - 2; s >= 0; s--) {
    for (int v = 0; v < n; v++) {
      for (int u = 0; u < n; u++) {
        if (!(s >> u & 1)) {
          dp[s][v] = min(dp[s][v], dp[s | 1 << u][u] + d[u][v]);
        }
      }
    }
  }
  return dp[0][0];
}

int main() {
  int len, r;

  while (cin >> len) {

    cakes.clear();
    while (cin >> r) {
      if (cin.peek() == '\\n') {
        break;
      }
      cakes.push_back(r);
    }
    for (int i = 0; i < cakes.size(); i++) {
      d[i][i] = 0;
    }
    calc_dist();
    if (solve() <= len) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
#define rrep(i, x, n) for (int i = (x); i < (n); ++i)
#define rep(i, x) rrep(i, 0, (x))
#define fi first
#define se second
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define pb push_back
#define maxs(a, b) (a) = max(a, b)
#define mins(a, b) (a) = min(a, b)

const double INF = 114514;
const double EPS = 1e-8;
int main() {
  string str;
  while (getline(cin, str)) {
    stringstream ss(str);
    int W;
    ss >> W;
    vi r;
    int val;
    while (ss >> val) {
      r.pb(val);
    }
    int N = r.size();
    double dist[20][20] = {{0}};
    rep(i, N) dist[0][i + 1] = r[i];
    rep(i, N) rep(j, N) {
      dist[i + 1][j + 1] =
          sqrt((r[i] + r[j]) * (r[i] + r[j]) - (r[i] - r[j]) * (r[i] - r[j]));
    }

    double dp[1 << 12][13];
    rep(i, (1 << N)) rep(j, 13) dp[i][j] = INF;
    dp[0][0] = 0;

    rep(i, (1 << N)) {
      rep(j, N + 1) {

        rep(k, N) {
          if ((i >> k) & 1)
            continue;
          dp[i | (1 << k)][k + 1] =
              min(dp[i | (1 << k)][k + 1], dp[i][j] + dist[j][k + 1]);
        }
      }
    }

    double mi = INF;
    rep(i, N) mi = min(mi, dp[(1 << N) - 1][i + 1] + r[i]);

    if (mi > W + EPS)
      puts("NA");
    else
      puts("OK");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
  string input;
  while (getline(cin, input)) {
    stringstream ss(input);
    double L;
    ss >> L;
    int n = 0;
    double r[12];
    while (ss >> r[n])
      n++;
    for (int i = 0; i < n; i++)
      cout << r[i] << " ";
    cout << endl;
    double len[12][12];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 12; j++) {
        len[i][j] =
            sqrt((r[i] + r[j]) * (r[i] + r[j]) - (r[i] - r[j]) * (r[i] - r[j]));
      }
    }
    int perm[12];
    for (int i = 0; i < n; i++)
      perm[i] = i;
    double mi = 100000000;
    do {
      double sum = 0;
      for (int i = 0; i < n - 1; i++)
        sum += len[perm[i]][perm[i + 1]];
      sum += r[perm[0]];
      sum += r[perm[n - 1]];
      mi = min(mi, sum);
    } while (next_permutation(perm, perm + n));
    if (mi <= L)
      puts("OK");
    else
      puts("NA");
  }
  return 0;

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

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

const double EPS = 1e-6;

double evaluate(vector<double> &r) {
  double res = r[0] + r[r.size() - 1];
  for (int i = 0; i + 1 < r.size(); i++) {
    double h = (r[i] + r[i + 1]);
    double s = abs(r[i] - r[i + 1]);
    res += sqrt(h * h - s * s);
  }
  return res;
}

double optimize(vector<double> r) {
  double T = 10000;
  double cool = 0.99;
  double step = 1;

  double mi = evaluate(r);

  while (T > 0.00001) {
    int a, b;
    while (true) {
      a = rand() % r.size();
      b = rand() % r.size();
      if (a != b)
        break;
    }

    double score = evaluate(r);
    swap(r[a], r[b]);
    double new_score = evaluate(r);

    double p = exp(-abs(new_score - score) / T);

    if (new_score > score || rand() < p * RAND_MAX)
      ;
    else {
      swap(r[a], r[b]);
    }

    mi = min(mi, new_score);
    T *= cool;
  }
  return mi;
}

int main() {
  int l;
  while (cin >> l) {
    string str;
    getline(cin, str);
    stringstream ss(str);
    vector<double> r;
    while (!ss.eof()) {
      double t;
      ss >> t;
      r.push_back(t);
    }

    double ans;
    if (r.size() == 1)
      ans = r[0] * 2;
    else
      ans = optimize(r);

    if (ans < l + EPS)
      puts("OK");
    else
      puts("NA");
  }

  return 0;
}

Error: TLE

Fixed submission:

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

const double EPS = 1e-6;

double evaluate(vector<double> &r) {
  double res = r[0] + r[r.size() - 1];
  for (int i = 0; i + 1 < r.size(); i++) {
    double h = (r[i] + r[i + 1]);
    double s = abs(r[i] - r[i + 1]);
    res += sqrt(h * h - s * s);
  }
  return res;
}

double optimize(vector<double> r) {
  double T = 10000;
  double cool = 0.99;
  double step = 1;

  double mi = evaluate(r);

  for (int i = 0; i < 1000 && T > 0.0001; i++) {
    int a, b;
    while (true) {
      a = rand() % r.size();
      b = rand() % r.size();
      if (a != b)
        break;
    }

    double score = evaluate(r);
    swap(r[a], r[b]);
    double new_score = evaluate(r);

    double p = exp(-abs(new_score - score) / T);

    if (new_score > score || rand() < p * RAND_MAX)
      ;
    else {
      swap(r[a], r[b]);
    }

    mi = min(mi, new_score);
    T *= cool;
  }
  return mi;
}

int main() {
  int l;
  while (cin >> l) {
    cin.ignore();
    string str;
    getline(cin, str);
    stringstream ss(str);
    vector<double> r;
    while (!ss.eof()) {
      double t;
      ss >> t;
      r.push_back(t);
    }

    if (optimize(r) < l + EPS)
      puts("OK");
    else
      puts("NA");
  }

  return 0;
}