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


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2300

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

struct Lab {
  double L;
  double A;
  double B;
  Lab(double l, double a, double b) : L(l), A(a), B(b) {}
};

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first1, BidirectionalIterator last1,
                      BidirectionalIterator first2,
                      BidirectionalIterator last2) {
  if ((first1 == last1) || (first2 == last2)) {
    return false;
  }
  BidirectionalIterator m1 = last1;
  BidirectionalIterator m2 = last2;
  --m2;
  while (--m1 != first1 && !(*m1 < *m2)) {
  }
  bool result = (m1 == first1) && !(*first1 < *m2);
  if (!result) {
    while (first2 != m2 && !(*m1 < *first2)) {
      ++first2;
    }
    first1 = m1;
    std ::iter_swap(first1, first2);
    ++first1;
    ++first2;
  }
  if ((first1 != last1) && (first2 != last2)) {
    m1 = last1;
    m2 = first2;
    while ((m1 != first1) && (m2 != last2)) {
      std ::iter_swap(--m1, m2);
      ++m2;
    }
    std ::reverse(first1, m1);
    std ::reverse(first1, last1);
    std ::reverse(m2, last2);
    std ::reverse(first2, last2);
  }
  return !result;
}

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first, BidirectionalIterator middle,
                      BidirectionalIterator last) {
  return next_combination(first, middle, middle, last);
}

int main() {
  cout << setprecision(20) << fixed;

  int N, M;
  cin >> N >> M;
  vector<Lab> labs;
  rep(nth, N) {
    double l, a, b;
    cin >> l >> a >> b;
    labs.push_back(Lab(l, a, b));
  }

  vector<int> num;
  rep(i, N) { num.push_back(i); }

  double ans = 0.0;
  do {

    double tmp = 0.0;

    rep(i, M) {
      REP(j, i, M) {
        if (i != j) {
          double li = labs[num[i]].L - labs[num[j]].L;
          double ai = labs[num[i]].A - labs[num[j]].A;
          double bi = labs[num[i]].B - labs[num[j]].B;
          tmp += li * li + ai * ai + bi * bi;
        }
      }
    }
    ans = max(tmp, ans);

  } while (next_combination(num.begin(), num.begin() + M, num.end()));

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2300

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

struct Lab {
  double L;
  double A;
  double B;
  Lab(double l, double a, double b) : L(l), A(a), B(b) {}
};

int main() {
  cout << setprecision(20) << fixed;

  int N, M;
  cin >> N >> M;
  vector<Lab> labs;
  rep(nth, N) {
    double l, a, b;
    cin >> l >> a >> b;
    labs.push_back(Lab(l, a, b));
  }

  vector<int> num;
  rep(i, N) { num.push_back(i); }

  double ans = 0.0;
  do {
    double tmp = 0.0;
    rep(i, M) {
      double li = labs[num[i]].L - labs[num[(i + 1) % M]].L;
      double ai = labs[num[i]].A - labs[num[(i + 1) % M]].A;
      double bi = labs[num[i]].B - labs[num[(i + 1) % M]].B;
      tmp += li * li + ai * ai + bi * bi;
      if (M == 2) {
        break;
      }
    }
    ans = max(tmp, ans);
  } while (next_permutation(num.begin(), num.begin() + N));

  cout << ans << endl;

  return 0;
}