Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
#include <time.h>

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl

using namespace std;

typedef long long LL;

const LL INF = 1LL << 60;

template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[ ";
  REP(i, vec.size()) { os << vec[i] << " "; }
  os << "]";

  return os;
}

int dfs(int n, const vector<int> &hs) {
  if (n == hs.size() - 1) {
    return 0;
  }

  int cost1 =
      n + 1 < hs.size() ? abs(hs[n] - hs[n + 1]) + dfs(n + 1, hs) : int(1e5);
  int cost2 =
      n + 2 < hs.size() ? abs(hs[n] - hs[n + 2]) + dfs(n + 2, hs) : int(1e5);

  int ret = min(cost1, cost2);

  return ret;
}

int dfs_memo(int n, const vector<int> &hs, vector<LL> &memo) {
  if (memo[n] < INF) {
    return memo[n];
  }

  if (n == hs.size() - 1) {
    return 0;
  }

  LL cost1 = n + 1 < hs.size() ? abs(hs[n] - hs[n + 1]) + dfs(n + 1, hs) : INF;
  LL cost2 = n + 2 < hs.size() ? abs(hs[n] - hs[n + 2]) + dfs(n + 2, hs) : INF;

  int ret = min(cost1, cost2);

  memo[n] = ret;

  return ret;
}

template <typename T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int dp1(const vector<int> &hs) {
  vector<int> costs(hs.size());

  costs[0] = 0;
  costs[1] = abs(hs[0] - hs[1]);

  FOR(i, 2, hs.size()) {
    costs[i] = min(costs[i - 2] + abs(hs[i - 2] - hs[i]),
                   costs[i - 1] + abs(hs[i - 1] - hs[i]));
  }

  return costs.back();
}

int dp2(const vector<int> &hs) {
  vector<LL> costs(hs.size(), 1LL << 60);

  costs[0] = 0;
  REP(i, costs.size()) {
    if (i + 1 < costs.size())
      chmin(costs[i + 1], costs[i] + abs(hs[i] - hs[i + 1]));
    if (i + 2 < costs.size())
      chmin(costs[i + 2], costs[i] + abs(hs[i] - hs[i + 2]));
  }

  return costs.back();
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;

  vector<int> h(N);
  REP(i, N) cin >> h[i];

  //    cout << dfs(0, h) << endl;

  vector<LL> memo(N, INF);
  cout << dfs_memo(0, h, memo) << endl;

  //  cout << dp1(h) << endl;

  //    cout << dp2(h) << endl;
}


Fixed submission:

#include <bits/stdc++.h>
#include <time.h>

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define dump(x) cerr << #x << " = " << (x) << endl
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl

using namespace std;

typedef long long LL;

const LL INF = 1LL << 60;

template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[ ";
  REP(i, vec.size()) { os << vec[i] << " "; }
  os << "]";

  return os;
}

int dfs(int n, const vector<int> &hs) {
  if (n == hs.size() - 1) {
    return 0;
  }

  int cost1 =
      n + 1 < hs.size() ? abs(hs[n] - hs[n + 1]) + dfs(n + 1, hs) : int(1e5);
  int cost2 =
      n + 2 < hs.size() ? abs(hs[n] - hs[n + 2]) + dfs(n + 2, hs) : int(1e5);

  int ret = min(cost1, cost2);

  return ret;
}

int dfs_memo(int n, const vector<int> &hs, vector<LL> &memo) {
  if (memo[n] < INF) {
    return memo[n];
  }

  if (n == hs.size() - 1) {
    return 0;
  }

  LL cost1 = n + 1 < hs.size()
                 ? abs(hs[n] - hs[n + 1]) + dfs_memo(n + 1, hs, memo)
                 : INF;
  LL cost2 = n + 2 < hs.size()
                 ? abs(hs[n] - hs[n + 2]) + dfs_memo(n + 2, hs, memo)
                 : INF;

  int ret = min(cost1, cost2);

  memo[n] = ret;

  return ret;
}

template <typename T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int dp1(const vector<int> &hs) {
  vector<int> costs(hs.size());

  costs[0] = 0;
  costs[1] = abs(hs[0] - hs[1]);

  FOR(i, 2, hs.size()) {
    costs[i] = min(costs[i - 2] + abs(hs[i - 2] - hs[i]),
                   costs[i - 1] + abs(hs[i - 1] - hs[i]));
  }

  return costs.back();
}

int dp2(const vector<int> &hs) {
  vector<LL> costs(hs.size(), 1LL << 60);

  costs[0] = 0;
  REP(i, costs.size()) {
    if (i + 1 < costs.size())
      chmin(costs[i + 1], costs[i] + abs(hs[i] - hs[i + 1]));
    if (i + 2 < costs.size())
      chmin(costs[i + 2], costs[i] + abs(hs[i] - hs[i + 2]));
  }

  return costs.back();
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;

  vector<int> h(N);
  REP(i, N) cin >> h[i];

  //    cout << dfs(0, h) << endl;

  vector<LL> memo(N, INF);
  cout << dfs_memo(0, h, memo) << endl;

  //  cout << dp1(h) << endl;

  //    cout << dp2(h) << endl;
}