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


#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef long long LL;
const double pi = 3.141592653;
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const int MOD = 10007;
const int MAXN = 220;
struct people {
  double pp[MAXN], time[MAXN];
} peo[MAXN];

int n, m, L, p[MAXN], t[MAXN], v[MAXN];
LL Com[MAXN][MAXN];

int main() {
  Com[0][0] = 1;
  for (int i = 1; i <= 50; i++) {
    Com[i][0] = Com[i - 1][0];
    for (int j = 1; j <= i; j++)
      Com[i][j] = Com[i - 1][j] + Com[i - 1][j - 1];
  }
  while (scanf("%d%d%d", &n, &m, &L) != EOF) {
    for (int i = 0; i < n; i++)
      scanf("%d%d%d", &p[i], &t[i], &v[i]);
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= m; j++) {
        double x = Com[m][j];
        int k;
        for (k = 1; k <= j; k++)
          x *= p[i] / 100.0;
        for (; k <= m; k++)
          x *= (100 - p[i]) / 100.0;
        peo[i].pp[j] = x;
        peo[i].time[j] = (L + 0.0) / v[i] + j * t[i];
      }
    for (int i = 0; i < n; i++) {
      double ans = 0;
      for (int j = 0; j <= m; j++) {
        double tmp = 1;
        for (int k = 0; k < n; k++) {
          if (k == i)
            continue;
          double x = 0;
          for (int l = 0; l <= m; l++)
            if (peo[k].time[l] > peo[i].time[j])
              x += peo[k].pp[l];
          tmp *= x;
        }
        ans += tmp * peo[i].pp[j];
      }
      printf("%.8lf\\n", ans);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef long long LL;
const double pi = 3.141592653;
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const int MOD = 10007;
const int MAXN = 220;
struct people {
  double pp[MAXN], time[MAXN];
} peo[MAXN];

int n, m, L, p[MAXN], t[MAXN], v[MAXN];
LL Com[MAXN][MAXN];

LL C(LL n, LL m) {
  LL a = 1, b = 1;
  for (int i = m; i >= m - n + 1; i--)
    a *= i;
  for (int i = 1; i <= n; i++)
    b *= i;
  return a / b;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
  Com[0][0] = 1;
  for (int i = 1; i <= 50; i++) {
    Com[i][0] = Com[i - 1][0];
    for (int j = 1; j <= i; j++)
      Com[i][j] = Com[i - 1][j] + Com[i - 1][j - 1];
  }
  while (scanf("%d%d%d", &n, &m, &L) != EOF) {
    for (int i = 0; i < n; i++)
      scanf("%d%d%d", &p[i], &t[i], &v[i]);
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= m; j++) {

        ///                double x=C(j,m);
        double x = Com[m][j];
        int k;
        for (k = 1; k <= j; k++)
          x *= p[i] / 100.0;
        for (; k <= m; k++)
          x *= (100 - p[i]) / 100.0;
        peo[i].pp[j] = x;
        peo[i].time[j] = (L + 0.0) / v[i] + j * t[i];
      }
    for (int i = 0; i < n; i++) {
      double ans = 0;
      for (int j = 0; j <= m; j++) {
        double tmp = 1;
        for (int k = 0; k < n; k++) {
          if (k == i)
            continue;
          double x = 0;
          for (int l = 0; l <= m; l++)
            if (peo[k].time[l] > peo[i].time[j])
              x += peo[k].pp[l];
          tmp *= x;
        }
        ans += tmp * peo[i].pp[j];
      }
      printf("%.8lf\\n", ans);
    }
  }
  return 0;
}