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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const double EPS = 1.0e-10;

double determinant(vector<vector<double>> mat) {
  const int n = mat.size();
  double ret = 1.0;
  for (int i = 0; i < n; ++i) {
    int tmp = i;
    for (int j = i + 1; j < n; ++j) {
      if (abs(mat[j][i]) > abs(mat[tmp][i]))
        tmp = j;
    }
    if (abs(mat[tmp][i]) < EPS)
      return 0.0;
    if (tmp != i) {
      mat[i].swap(mat[tmp]);
      ret *= -1.0;
    }

    for (int j = i + 1; j < n; ++j) {
      for (int k = n - 1; k >= i; --k) {
        mat[j][k] -= mat[i][k] * (mat[j][i] / mat[i][i]);
      }
    }
    ret *= mat[i][i];
  }
  return ret;
}

double tetrahedronVolume(const vector<double> &len) {
  const int a[][3] = {{0, 1, 3}, {1, 2, 4}, {0, 2, 5}, {3, 4, 5}};
  for (int i = 0; i < 4; ++i) {
    vector<double> b(3);
    for (int j = 0; j < 3; ++j)
      b[j] = len[a[i][j]];
    sort(b.begin(), b.end());
    if (b[2] > b[0] + b[1] - EPS)
      return 0.0;
  }

  const int y[] = {0, 3, 1, 3, 2, 3, 0, 1, 1, 2, 0, 2};
  const int x[] = {3, 0, 3, 1, 3, 2, 1, 0, 2, 1, 2, 0};

  vector<vector<double>> mat(5, vector<double>(5, 1.0));
  for (int i = 0; i < 12; ++i)
    mat[y[i]][x[i]] = pow(len[i / 2], 2.0);
  for (int i = 0; i < 5; ++i)
    mat[i][i] = 0.0;

  return sqrt(determinant(mat) / 288.0);
}

template <size_t T> bool nextBitset(bitset<T> &bs, int digit) {
  if (bs.none())
    return false;
  bitset<T> x, y, z;
  x = bs.to_ulong() & (~(bs.to_ulong()) + 1ULL);
  y = bs.to_ulong() + x.to_ulong() + 0ULL;
  z = bs & ~y;
  if (bs[digit - 1] && bs == z)
    return false;
  bs = ((z.to_ulong() / x.to_ulong()) >> 1) + 0ULL;
  bs |= y;
  return true;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<double> len(n);
    for (int i = 0; i < n; ++i)
      cin >> len[i];

    double ret = 0.0;
    bitset<15> used;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < i; ++j) {
        for (int k = 0; k < j; ++k) {
          used[i] = used[j] = used[k] = true;

          for (int a = 0; a < n; ++a) {
            for (int b = 0; b < n; ++b) {
              for (int c = 0; c < n; ++c) {
                if (a == b || b == c || c == a)
                  continue;
                if (used[a] || used[b] || used[c])
                  continue;

                vector<double> len2(6);
                len2[0] = len[a];
                len2[1] = len[b];
                len2[2] = len[c];
                len2[3] = len[i];
                len2[4] = len[j];
                len2[5] = len[k];
                ret = max(ret, tetrahedronVolume(len2));
                ;
              }
            }
          }

          used[i] = used[j] = used[k] = false;
        }
      }
    }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const double EPS = 1.0e-10;

double determinant(vector<vector<double>> &mat) {
  const int n = mat.size();
  double ret = 1.0;
  for (int i = 0; i < n; ++i) {
    int tmp = i;
    for (int j = i + 1; j < n; ++j) {
      if (abs(mat[j][i]) > abs(mat[tmp][i]))
        tmp = j;
    }
    if (abs(mat[tmp][i]) < EPS)
      return 0.0;
    if (tmp != i) {
      mat[i].swap(mat[tmp]);
      ret *= -1.0;
    }

    for (int j = i + 1; j < n; ++j) {
      for (int k = n - 1; k >= i; --k) {
        mat[j][k] -= mat[i][k] * (mat[j][i] / mat[i][i]);
      }
    }
    ret *= mat[i][i];
  }
  return ret;
}

double tetrahedronVolume(const vector<double> &len) {
  const int a[][3] = {{0, 1, 3}, {1, 2, 4}, {0, 2, 5}, {3, 4, 5}};
  for (int i = 0; i < 4; ++i) {
    vector<double> b(3);
    for (int j = 0; j < 3; ++j)
      b[j] = len[a[i][j]];
    sort(b.begin(), b.end());
    if (b[2] > b[0] + b[1] - EPS)
      return 0.0;
  }

  const int y[] = {0, 3, 1, 3, 2, 3, 0, 1, 0, 2, 1, 2};
  const int x[] = {3, 0, 3, 1, 3, 2, 1, 0, 2, 0, 2, 1};

  vector<vector<double>> mat(5, vector<double>(5, 1.0));
  for (int i = 0; i < 12; ++i)
    mat[y[i]][x[i]] = pow(len[i / 2], 2.0);
  for (int i = 0; i < 5; ++i)
    mat[i][i] = 0.0;

  return sqrt(determinant(mat) / 288.0);
}

template <size_t T> bool nextBitset(bitset<T> &bs, int digit) {
  if (bs.none())
    return false;
  bitset<T> x, y, z;
  x = bs.to_ulong() & (~(bs.to_ulong()) + 1ULL);
  y = bs.to_ulong() + x.to_ulong() + 0ULL;
  z = bs & ~y;
  if (bs[digit - 1] && bs == z)
    return false;
  bs = ((z.to_ulong() / x.to_ulong()) >> 1) + 0ULL;
  bs |= y;
  return true;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<double> a(n);
    for (int i = 0; i < n; ++i)
      cin >> a[i];

    double ret = 0.0;
    bitset<16> bs((1 << 6) - 1);
    do {
      vector<double> b;
      for (int i = 0; i < n; ++i) {
        if (bs[i])
          b.push_back(a[i]);
      }
      sort(b.begin(), b.end());

      do {
        ret = max(ret, tetrahedronVolume(b));
      } while (next_permutation(b.begin(), b.end()));
    } while (nextBitset(bs, n));

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