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


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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#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;

const int MAX = 1000100;

vector<int> list;
vector<int> odd;

void init() {
  for (int i = 1;; i++) {
    int num = i * (i + 1) * (i + 2) / 6;
    if (num > MAX) {
      break;
    }
    list.push_back(num);
    if (num % 2) {
      odd.push_back(num);
    }
  }

  return;
}

int main() {
  init();

  vector<int> dp(MAX, MAX);
  vector<int> odd_dp(MAX, MAX);

  dp[0] = 0;
  odd_dp[0] = 0;

  rep(i, list.size()) {
    REP(j, list[i], MAX) { dp[j] = min(dp[j], dp[j - list[i]] + 1); }
  }

  rep(i, odd.size()) {
    REP(j, odd[i], MAX) { odd_dp[j] = min(odd_dp[j], odd_dp[j - odd[i]] + 1); }
  }

  int N;
  while (cin >> N && N) {
    cout << dp[N] << " " << odd_dp[N] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:


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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#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;

const int MAX = 200;

int ans;

vector<int> tetra;
vector<int> odd;

void init() {
  rep(i, MAX) { tetra.push_back(i * (i + 1) * (i + 2) / 6); }

  rep(i, tetra.size()) {
    if (tetra[i] % 2 != 0) {
      odd.push_back(tetra[i]);
    }
  }
}

void dfs(int n, int num, int deep, vector<int> &list, int limit) {
  if (deep > limit) {
    return;
  }

  int tmp = num;
  rep(i, list.size()) {
    tmp += list[i];

    if (deep > ans) {
      return;
    }
    if (n == tmp) {
      ans = min(ans, deep);
      return;
    }
    if (n < tmp) {
      return;
    }

    dfs(n, tmp, deep + 1, list, limit);
    tmp = num;
  }

  return;
}

int main() {
  int N;

  init();

  while (cin >> N && N) {
    int ans_tet, ans_odd;

    ans = INT_MAX;
    dfs(N, 0, 1, tetra, 5);
    ans_tet = ans;

    ans = INT_MAX;
    dfs(N, 0, 1, odd, INT_MAX);
    ans_odd = ans;

    cout << ans_tet << " " << ans_odd << endl;
  }

  return 0;
}