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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

typedef vector<vvd> vvvd;

double calc(int h, int n, int add, vi &bars, int start) {
  vi bcount(h + 1);
  for (int i = h; i >= 0; i--)
    bcount[i] = (bars[i] == -1) + (i == h ? 0 : bcount[i + 1]);

  vvvd dp(h + 1, vvd(n, vd(add + 1))); // [i][j][drawed_line]
  dp[0][start][0] = 1;
  repi(i, 1, h + 1) {
    if (bars[i] != -1) {
      dp[i] = dp[i - 1];
      swap(dp[i][bars[i]], dp[i][bars[i] + 1]);
      continue;
    }
    rep(j, n) rep(k, add) {
      double p = 1.0 / (n - 1) * (add - k) / bcount[i];
      if (bcount[i] < (add - k))
        continue;
      if (j > 0)
        dp[i][j][k + 1] += dp[i - 1][j - 1][k] * p;
      if (j < n - 1)
        dp[i][j][k + 1] += dp[i - 1][j + 1][k] * p;
      dp[i][j][k + 1] +=
          dp[i - 1][j][k] * p * (n - 1 - ((j > 0) + (j < n - 1)));
    }
    rep(j, n) rep(k, add + 1) dp[i][j][k] +=
        dp[i - 1][j][k] * (1 - (double)(add - k) / bcount[i]);
  }

  double res = 0;
  rep(i, n) res = max(res, dp[h][i][add]);
  return res;
}

int main() {
  for (int h, n, p, m, k; cin >> h >> n >> p >> m >> k && h | n | p | m | k;) {
    vi bars(h + 1, -1);
    rep(i, m) {
      int a, b;
      cin >> a >> b;
      bars[a] = b - 1;
    }

    reverse(1 + allof(bars));
    printf("%.10f\\n", calc(h, n, k, bars, p - 1));
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

typedef vector<vvd> vvvd;

double calc(int h, int n, int goal, int add, vi &bars, int start) {
  vi bcount(h + 1);
  for (int i = h; i >= 0; i--)
    bcount[i] = (bars[i] == -1) + (i == h ? 0 : bcount[i + 1]);

  vvvd dp(h + 1, vvd(n, vd(add + 1))); // [i][j][drawed_line]
  dp[0][start][0] = 1;
  repi(i, 1, h + 1) {
    if (bars[i] != -1) {
      dp[i] = dp[i - 1];
      swap(dp[i][bars[i]], dp[i][bars[i] + 1]);
      continue;
    }
    // rep(j,n-1) rep(jj,n) rep(k,add){
    //	double p=1.0/(n-1)*(add-k)/bcount[i];
    //	if(bcount[i]<(add-k))
    //		continue;
    //	else if(jj==j)
    //		dp[i][jj][k+1]+=dp[i-1][jj+1][k]*p;
    //	else if(jj==j+1)
    //		dp[i][jj][k+1]+=dp[i-1][jj-1][k]*p;
    //	else
    //		dp[i][jj][k+1]+=dp[i-1][jj][k]*p;
    // }
    rep(j, n) rep(k, add) {
      double p = 1.0 / (n - 1) * (add - k) / bcount[i];
      if (bcount[i] < (add - k))
        continue;
      if (j > 0)
        dp[i][j][k + 1] += dp[i - 1][j - 1][k] * p;
      if (j < n - 1)
        dp[i][j][k + 1] += dp[i - 1][j + 1][k] * p;
      dp[i][j][k + 1] +=
          dp[i - 1][j][k] * p * (n - 1 - ((j > 0) + (j < n - 1)));
    }
    rep(j, n) rep(k, add + 1) dp[i][j][k] +=
        dp[i - 1][j][k] * (1 - (double)(add - k) / bcount[i]);
  }
  return dp[h][goal][add];
}

int main() {
  for (int h, n, p, m, k; cin >> h >> n >> p >> m >> k && h | n | p | m | k;) {
    vi bars(h + 1, -1);
    rep(i, m) {
      int a, b;
      cin >> a >> b;
      bars[a] = b - 1;
    }

    double res = -1;
    rep(i, n) res = max(res, calc(h, n, p - 1, k, bars, i));
    printf("%.10f\\n", res);
  }

  return 0;
}