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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 1e9;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
using namespace std;
typedef long long ll;
// typedef pair<int, int> P;
struct P {
  int x, y, n;
  P(int n, int x, int y) : n(n), x(x), y(y) {}
  P() {}
};

/** Problem1306 : Ballon Collecting **/
vector<int> T, P;
int N;
int NG;
int dp[41][4];

int solve(int i, int s) {
  if (i == N)
    return P[N - 1];

  if (dp[i][s] > -1)
    return dp[i][s];

  int res = INF;

  int cT, cP;
  if (i == 0)
    cT = 0, cP = 0;
  else
    cT = T[i - 1], cP = P[i - 1];

  bool ok = false;
  if (s + 1 <= 3 && cT + (s + 1) * abs(cP - P[i]) <= T[i]) {
    res = min(res, abs(cP - P[i]) + solve(i + 1, s + 1));
    ok = true;
  }

  // 一度帰る
  if (cT + (s + 1) * cP + P[i] <= T[i]) {
    res = min(res, cP + P[i] + solve(i + 1, 1));
    ok = true;
  }

  if (!ok)
    NG = max(NG, i + 1);

  return dp[i][s] = res;
}

int main() {
  while (cin >> N, N) {
    T.resize(N), P.resize(N);
    NG = -1;
    memset(dp, -1, sizeof(dp));

    rep(i, N) { cin >> P[i] >> T[i]; }
    int ans = solve(0, 0);

    if (ans < INF)
      cout << "OK " << ans << endl;
    else
      cout << "NG " << NG << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 1e9;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
using namespace std;
typedef long long ll;
// typedef pair<int, int> P;
struct P {
  int x, y, n;
  P(int n, int x, int y) : n(n), x(x), y(y) {}
  P() {}
};

/** Problem1306 : Ballon Collecting **/
vector<int> T, P;
int N;
int NG;

int solve(int i, int s) {
  if (i == N)
    return P[N - 1];

  int res = INF;

  int cT, cP;
  if (i == 0)
    cT = 0, cP = 0;
  else
    cT = T[i - 1], cP = P[i - 1];

  bool ok = false;
  if (s + 1 <= 3 && cT + (s + 1) * abs(cP - P[i]) <= T[i]) {
    res = min(res, abs(cP - P[i]) + solve(i + 1, s + 1));
    ok = true;
  }

  // 一度帰る
  if (cT + (s + 1) * cP + P[i] <= T[i]) {
    res = min(res, cP + P[i] + solve(i + 1, 1));
    ok = true;
  }

  if (!ok)
    NG = max(NG, i + 1);

  return res;
}

int main() {
  while (cin >> N, N) {
    T.resize(N), P.resize(N);
    NG = -1;

    rep(i, N) { cin >> P[i] >> T[i]; }
    int ans = solve(0, 0);

    if (ans < INF)
      cout << "OK " << ans << endl;
    else
      cout << "NG " << NG << endl;
  }
}