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

#include <cctype>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, l, n) for (int(i) = (int)(l); (i) < (int)(n); (i)++)
#define d_arr(arr, h, w)                                                       \\
  rep(i, (h)) {                                                                \\
    cout << "[";                                                               \\
    rep(j, (w)) { cout << (arr)[i][j] << ", "; }                               \\
    cout << "]" << endl;                                                       \\
  }
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x);
#define pb push_back
#define priv(v)                                                                \\
  rep(i, v.size()) { cout << v << ", "; }                                      \\
  puts("");

typedef pair<int, int> P;

void vpri(vector<int> v) {
  rep(i, v.size()) { cout << v[i] << ","; }
  puts("fin");
}
int main() {
  while (true) {
    int x, y, z;
    cin >> x >> y >> z;
    if (x == 0 && y == 0 && z == 0) {
      break;
    }
    vector<int> jump;
    jump.clear();
    int tmp;
    rep(i, x) {
      cin >> tmp;
      jump.pb(tmp);
    }
    vector<int> n, e, a;
    n.clear();
    e.clear();
    a.clear();
    n.resize(y + 1, 0);
    e.resize(y + 1, 0);
    a.resize(y + 1, 0);
    double dp[51][5001];
    rep(i, 51) rep(j, 5001) dp[i][j] = 0.0;

    rep(i, z) {
      int n;
      int t_e, t_a;
      cin >> n;
      cin >> t_e;
      e[n] = t_e;
      cin >> t_a;
      a[n] = t_a;
    }

    // cout << "input finish" << endl;
    // cout<< "e:"<<endl;
    // vpri(e);
    // cout<< "a:"<<endl;
    // vpri(a);

    // 配るDPなので配った先でお金は計算する:
    dp[0][0] = 1.0;
    rep(i, y) {
      rep(j, 5001) {
        if (dp[i][j] == 0.0)
          continue;
        // cout << "start distributing from dp: " << i<<","<<j <<" pro:
        // "<<dp[i][j]<<endl;
        rep(k, x) {
          // k is index of jump
          int skip, gain, money;
          int next = min(i + jump[k], y);
          money = j;
          if (e[next] == 1) {
            // cout<<"Jump!"<<endl;
            next = min(y, next + a[next]);
          } else {
            gain = 0;
            if (e[next] == 2) {
              gain = a[next];
            } else if (e[next] == 3) {
              gain = -a[next];
            }
            money = max(0, j + gain);
          }
          // cout << "to dp: " << next<<","<<money<<" add
          // pro:"<<1.0/double(x)<<endl;
          dp[next][money] += dp[i][j] / double(x);
        }
      }
    }
    double ev = 0.0;
    rep(j, 5001) { ev += double(j) * dp[y][j]; }
    cout << int(ev) << endl;
  }
}

Error: TLE

Fixed submission:

#include <cctype>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, l, n) for (int(i) = (int)(l); (i) < (int)(n); (i)++)
#define d_arr(arr, h, w)                                                       \\
  rep(i, (h)) {                                                                \\
    cout << "[";                                                               \\
    rep(j, (w)) { cout << (arr)[i][j] << ", "; }                               \\
    cout << "]" << endl;                                                       \\
  }
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x);
#define pb push_back
#define priv(v)                                                                \\
  rep(i, v.size()) { cout << v << ", "; }                                      \\
  puts("");

typedef pair<int, int> P;

void vpri(vector<int> v) {
  rep(i, v.size()) { cout << v[i] << ","; }
  puts("fin");
}
int main() {
  while (true) {
    int x, y, z;
    cin >> x >> y >> z;
    if (x == 0 && y == 0 && z == 0) {
      break;
    }
    vector<int> jump;
    jump.clear();
    int tmp;
    rep(i, x) {
      cin >> tmp;
      jump.pb(tmp);
    }
    vector<int> n, e, a;
    n.clear();
    e.clear();
    a.clear();
    n.resize(y + 1, 0);
    e.resize(y + 1, 0);
    a.resize(y + 1, 0);
    double dp[51][5001];
    rep(i, 51) rep(j, 5001) dp[i][j] = 0.0;

    rep(i, z) {
      int n;
      int t_e, t_a;
      cin >> n;
      cin >> t_e;
      e[n] = t_e;
      cin >> t_a;
      a[n] = t_a;
    }

    // cout << "input finish" << endl;
    // cout<< "e:"<<endl;
    // vpri(e);
    // cout<< "a:"<<endl;
    // vpri(a);

    // 配るDPなので配った先でお金は計算する:
    dp[0][0] = 1.0;
    rep(i, y) {
      rep(j, 5001) {
        if (dp[i][j] == 0.0)
          continue;
        cout << "start distributing from dp: " << i << "," << j
             << " pro: " << dp[i][j] << endl;
        rep(k, x) {
          // k is index of jump
          int skip, gain, money;
          int next = min(i + jump[k], y);
          money = j;
          if (e[next] == 1) {
            cout << "Jump!" << endl;
            next = min(y, next + a[next]);
          } else {
            gain = 0;
            if (e[next] == 2) {
              gain = a[next];
            } else if (e[next] == 3) {
              gain = -a[next];
            }
            money = max(0, j + gain);
          }
          cout << "to dp: " << next << "," << money
               << " add pro:" << 1.0 / double(x) << endl;
          dp[next][money] += dp[i][j] / double(x);
        }
      }
    }
    double ev = 0.0;
    rep(j, 5001) { ev += double(j) * dp[y][j]; }
    cout << int(ev) << endl;
  }
}