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

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
int bsr(int x) { return 31 - __builtin_clz(x); }

using R = long double;
const R PI = acos(R(-1));

R solve(vector<R> v) {
  int n = (int)v.size();
  v.push_back(v[0]);
  R l = 0, r = PI, area;
  for (int fe = 0; fe < 50; fe++) {
    R md = (l + r) / 2;
    R sm = md, th = md;
    area = v[0] * v[1] * sin(th);
    for (int i = 1; i < n; i++) {
      R x = cos(th) * v[i - 1] / v[i + 1];
      if (1 < x) {
        sm = 0;
        break;
      }
      if (x < -1) {
        sm = 3 * PI;
        break;
      }
      th = acos(x);
      sm += th;
      area += v[i] * v[i + 1] * sin(th);
    }
    if (sm < 2 * PI) {
      l = md;
    } else {
      r = md;
    }
    if (abs(sm - 2 * PI) <= 1e-9) {
      return area / 2;
    }
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vector<R> r(n);
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  R ans = 0;
  sort(begin(r), end(r));
  for (int i = 0; i < (1 << n); i++) {
    if (__builtin_popcount(i) < 3)
      continue;
    vector<R> v;
    for (int j = 0; j < n; j++) {
      if (i & (1 << j)) {
        v.push_back(r[j]);
      }
    }
    sort(v.begin(), v.end());
    do {
      ans = max(ans, solve(v));
    } while (next_permutation(v.begin() + 1, v.end()));
  }
  printf("%.20Lf\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
int bsr(int x) { return 31 - __builtin_clz(x); }

using R = double;
const R PI = acos(R(-1));

R solve(vector<R> v) {
  int n = (int)v.size();
  v.push_back(v[0]);
  R l = 0, r = PI, area;
  for (int fe = 0; fe < 70; fe++) {
    R md = (l + r) / 2;
    R sm = md, th = md;
    area = v[0] * v[1] * sin(th);
    for (int i = 1; i < n; i++) {
      R x = cos(th) * v[i - 1] / v[i + 1];
      if (1 < x) {
        sm = 0;
        break;
      }
      if (x < -1) {
        sm = 3 * PI;
        break;
      }
      th = acos(x);
      sm += th;
      area += v[i] * v[i + 1] * sin(th);
    }
    if (sm < 2 * PI) {
      l = md;
    } else {
      r = md;
    }
  }
  area /= 2;
  return area;
}

int main() {
  int n;
  cin >> n;
  vector<R> r(n);
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  R ans = 0;
  sort(begin(r), end(r));
  do {
    for (int i = 0; i < (1 << n); i++) {
      if (__builtin_popcount(i) < 3)
        continue;
      vector<R> v;
      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          v.push_back(r[j]);
        }
      }
      ans = max(ans, solve(v));
    }
  } while (next_permutation(begin(r), end(r)));
  printf("%.20lf\\n", ans);
  return 0;
}