Problem p01411 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int h, n, p, m, k, bar[600];
double dp[2][110][510];

int main() {
  cin >> h >> n >> p >> m >> k;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    bar[a] = b;
  }
  dp[0][p][0] = 1;
  int cur = 0, next = 1;

  for (int i = h; i > 0; i--) {
    memset(dp[next], 0, sizeof(dp[next]));
    for (int j = 1; j <= n; j++)
      rep(l, k + 1) {
        if (dp[cur][j][l] == 0)
          continue;
        double P = dp[cur][j][l];
        if (bar[i]) {
          int nj = j == bar[i] ? j + 1 : j == bar[i] + 1 ? j - 1 : j;
          dp[next][nj][l] = P;
        } else {
          double t = k * 1.0 / (h - m);
          int cnt = 0;
          if (j < n) {
            dp[next][j + 1][l + 1] += P * t / (n - 1);
            cnt++;
          }
          if (j > 1) {
            dp[next][j - 1][l + 1] += P * t / (n - 1);
            cnt++;
          }
          dp[next][j][l + 1] += P * t * (n - 1 - cnt) / (n - 1);
          dp[next][j][l] += P * (1 - t);
        }
      }
    swap(cur, next);
  }
  double ans = 0, s = 0;
  rep(i, n) {
    ans = max(ans, dp[cur][i + 1][k]);
    s += dp[cur][i + 1][k];
  }
  printf("%.9f\\n", ans / s);

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int h, n, p, m, k, bar[600];
double dp[510][110][510];

int main() {
  cin >> h >> n >> p >> m >> k;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    bar[a] = b;
  }
  dp[h][p][0] = 1;

  for (int i = h; i > 0; i--)
    for (int j = 1; j <= n; j++)
      rep(l, k + 1) {
        if (dp[i][j][l] == 0)
          continue;
        // cerr<<i<<" "<<j<<" "<<l<<endl;
        if (bar[i]) {
          int nj = j == bar[i] ? j + 1 : j == bar[i] + 1 ? j - 1 : j;
          dp[i - 1][nj][l] = dp[i][j][l];
        } else {
          double t = k * 1.0 / (h - m);
          int cnt = 0;
          if (j < n) {
            dp[i - 1][j + 1][l + 1] += dp[i][j][l] * t / (n - 1);
            cnt++;
          }
          if (j > 1) {
            dp[i - 1][j - 1][l + 1] += dp[i][j][l] * t / (n - 1);
            cnt++;
          }
          dp[i - 1][j][l + 1] += dp[i][j][l] * t * (n - 1 - cnt) / (n - 1);
          dp[i - 1][j][l] += dp[i][j][l] * (1 - t);
        }
      }
  double ans = 0, s = 0;
  rep(i, n) {
    ans = max(ans, dp[0][i + 1][k]);
    s += dp[0][i + 1][k];
  }
  printf("%.9f\\n", ans / s);

  return 0;
}