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

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

double EPS = 1e-8;
typedef complex<double> P, point;

struct C {
  P p;
  double r;
  C(const P &p, double r) : p(p), r(r) {}
  C() {}
};

typedef double number;
const number eps = 1e-8;
typedef vector<number> Array;
typedef vector<Array> matrix;

// O( n )
matrix identity(int n) {
  matrix A(n, Array(n));
  for (int i = 0; i < n; ++i)
    A[i][i] = 1;
  return A;
}
// O( n )
number inner_product(const Array &a, const Array &b) {
  number ans = 0;
  for (int i = 0; i < a.size(); ++i)
    ans += a[i] * b[i];
  return ans;
}
// O( n^2 )
Array mul(const matrix &A, const Array &x) {
  Array y(A.size());
  for (int i = 0; i < A.size(); ++i)
    for (int j = 0; j < A[0].size(); ++j)
      y[i] = A[i][j] * x[j];
  return y;
}
// O( n^3 )
matrix mul(const matrix &A, const matrix &B) {
  matrix C(A.size(), Array(B[0].size()));
  for (int i = 0; i < C.size(); ++i)
    for (int j = 0; j < C[i].size(); ++j)
      for (int k = 0; k < A[i].size(); ++k)
        C[i][j] += A[i][k] * B[k][j];
  return C;
}
// O( n^3 log e )
matrix pow(const matrix &A, int e) {
  return e == 0       ? identity(A.size())
         : e % 2 == 0 ? pow(mul(A, A), e / 2)
                      : mul(A, pow(A, e - 1));
}
// O( n^3 )
number det(matrix A) {
  const int n = A.size();
  number D = 1;
  for (int i = 0; i < n; ++i) {
    int pivot = i;
    for (int j = i + 1; j < n; ++j)
      if (abs(A[j][i]) > abs(A[pivot][i]))
        pivot = j;
    swap(A[pivot], A[i]);
    D *= A[i][i] * (i != pivot ? -1 : 1);
    if (abs(A[i][i]) < eps)
      break;
    for (int j = i + 1; j < n; ++j)
      for (int k = n - 1; k >= i; --k)
        A[j][k] -= A[i][k] * A[j][i] / A[i][i];
  }
  return D;
}
// O(n)
number tr(const matrix &A) {
  number ans = 0;
  for (int i = 0; i < A.size(); ++i)
    ans += A[i][i];
  return ans;
}
// O( n^3 ).
int rank(matrix A) {
  const int n = A.size(), m = A[0].size();
  int r = 0;
  for (int i = 0; r < n && i < m; ++i) {
    int pivot = r;
    for (int j = r + 1; j < n; ++j)
      if (abs(A[j][i]) > abs(A[pivot][i]))
        pivot = j;
    swap(A[pivot], A[r]);
    if (abs(A[r][i]) < eps)
      continue;
    for (int k = m - 1; k >= i; --k)
      A[r][k] /= A[r][i];
    for (int j = r + 1; j < n; ++j)
      for (int k = i; k < m; ++k)
        A[j][k] -= A[r][k] * A[j][i];
    ++r;
  }
  return r;
}

double chk(double a, double b, double c) {
  if (a > b)
    swap(a, b);
  if (b > c)
    swap(b, c);
  if (a > b)
    swap(a, b);
  return a + b >= c;
}
double calc(double a, double b, double c, double d, double e, double f) {
  double x = a * a * d * d * (b * b + c * c + e * e + f * f - a * a - d * d) +
             b * b * e * e * (c * c + a * a + f * f + d * d - b * b - e * e) +
             c * c * f * f * (a * a + b * b + d * d + e * e - c * c - f * f) -
             a * a * b * b * c * c - a * a * e * e * f * f -
             d * d * b * b * f * f - d * d * e * e * c * c;
  return x > 0 ? sqrt(x / 144) : 0;
}
int main() {
  int N;
  while (cin >> N && N) {
    vector<int> t(N);
    double ans = 0;
    vector<int> reflesh(6, 0);

    int cnt = 0;
    // cout << calc(P(0,0),P(0,1),P(1,1),1,1,1) << endl;
    for (int i = 0; i < N; i++)
      cin >> t[i];
    sort(t.begin(), t.end());
    for (int i = 0; i < (1 << N); i++) {
      if (__builtin_popcount(i) == 6) {
        vector<int> v;
        for (int j = 0; j < N; j++)
          if (i >> j & 1)
            v.push_back(t[j]);
        do {
          double a, b, c, d, e, f;
          tie(a, b, c, d, e, f) =
              tuple<double, double, double, double, double, double>{
                  v[0], v[1], v[2], v[3], v[4], v[5]};
          if (chk(a, e, f) && chk(a, b, c) && chk(e, d, c) && chk(d, b, f)) {
            ans = max(ans, calc(v[0], v[1], v[2], v[3], v[4], v[5]));
          }
        } while (next_permutation(v.begin(), v.end()));
      }
    }
    // cout << cnt << endl;
    if (ans == 0) {
      for (int i = 0; i < N; i++)
        cout << t[i] << " ";
      cout << endl;
    }
    printf("%.10lf%c", ans, 10);
  }
}

Error: TLE

Fixed submission:

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

double EPS = 1e-8;
typedef complex<double> P, point;

struct C {
  P p;
  double r;
  C(const P &p, double r) : p(p), r(r) {}
  C() {}
};

typedef double number;
const number eps = 1e-8;
typedef vector<number> Array;
typedef vector<Array> matrix;

// O( n )
matrix identity(int n) {
  matrix A(n, Array(n));
  for (int i = 0; i < n; ++i)
    A[i][i] = 1;
  return A;
}
// O( n )
number inner_product(const Array &a, const Array &b) {
  number ans = 0;
  for (int i = 0; i < a.size(); ++i)
    ans += a[i] * b[i];
  return ans;
}
// O( n^2 )
Array mul(const matrix &A, const Array &x) {
  Array y(A.size());
  for (int i = 0; i < A.size(); ++i)
    for (int j = 0; j < A[0].size(); ++j)
      y[i] = A[i][j] * x[j];
  return y;
}
// O( n^3 )
matrix mul(const matrix &A, const matrix &B) {
  matrix C(A.size(), Array(B[0].size()));
  for (int i = 0; i < C.size(); ++i)
    for (int j = 0; j < C[i].size(); ++j)
      for (int k = 0; k < A[i].size(); ++k)
        C[i][j] += A[i][k] * B[k][j];
  return C;
}
// O( n^3 log e )
matrix pow(const matrix &A, int e) {
  return e == 0       ? identity(A.size())
         : e % 2 == 0 ? pow(mul(A, A), e / 2)
                      : mul(A, pow(A, e - 1));
}
// O( n^3 )
number det(matrix A) {
  const int n = A.size();
  number D = 1;
  for (int i = 0; i < n; ++i) {
    int pivot = i;
    for (int j = i + 1; j < n; ++j)
      if (abs(A[j][i]) > abs(A[pivot][i]))
        pivot = j;
    swap(A[pivot], A[i]);
    D *= A[i][i] * (i != pivot ? -1 : 1);
    if (abs(A[i][i]) < eps)
      break;
    for (int j = i + 1; j < n; ++j)
      for (int k = n - 1; k >= i; --k)
        A[j][k] -= A[i][k] * A[j][i] / A[i][i];
  }
  return D;
}
// O(n)
number tr(const matrix &A) {
  number ans = 0;
  for (int i = 0; i < A.size(); ++i)
    ans += A[i][i];
  return ans;
}
// O( n^3 ).
int rank(matrix A) {
  const int n = A.size(), m = A[0].size();
  int r = 0;
  for (int i = 0; r < n && i < m; ++i) {
    int pivot = r;
    for (int j = r + 1; j < n; ++j)
      if (abs(A[j][i]) > abs(A[pivot][i]))
        pivot = j;
    swap(A[pivot], A[r]);
    if (abs(A[r][i]) < eps)
      continue;
    for (int k = m - 1; k >= i; --k)
      A[r][k] /= A[r][i];
    for (int j = r + 1; j < n; ++j)
      for (int k = i; k < m; ++k)
        A[j][k] -= A[r][k] * A[j][i];
    ++r;
  }
  return r;
}

double calc(double a, double b, double c, double d, double e, double f) {
  double x = a * a * d * d * (b * b + c * c + e * e + f * f - a * a - d * d) +
             b * b * e * e * (c * c + a * a + f * f + d * d - b * b - e * e) +
             c * c * f * f * (a * a + b * b + d * d + e * e - c * c - f * f) -
             a * a * b * b * c * c - a * a * e * e * f * f -
             d * d * b * b * f * f - d * d * e * e * c * c;
  return x > 0 ? sqrt(x / 144) : 0;
}
int main() {
  int N;
  while (cin >> N && N) {
    vector<int> t(N);
    double ans = 0;
    vector<int> reflesh(6, 0);

    int cnt = 0;
    // cout << calc(P(0,0),P(0,1),P(1,1),1,1,1) << endl;
    for (int i = 0; i < N; i++)
      cin >> t[i];
    sort(t.begin(), t.end());
    for (int i = 0; i < (1 << N); i++) {
      if (__builtin_popcount(i) == 6) {
        vector<int> v;
        for (int j = 0; j < N; j++)
          if (i >> j & 1)
            v.push_back(t[j]);

        set<multiset<int>> used;
        do {
          multiset<int> t;
          t.insert(v[0]);
          t.insert(v[1]);
          t.insert(v[2]);
          if (used.count(t))
            continue;
          used.insert(t);
          ans = max(ans, calc(v[0], v[1], v[2], v[3], v[4], v[5]));
        } while (next_permutation(v.begin(), v.end()));
        // cout << i << endl;
      }
    }
    // cout << cnt << endl;
    printf("%.10lf%c", ans, 10);
  }
}