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


#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long double ld;
const ld EPS = 10e-12;

struct data_t {
  ld l, r;
  int e, d;
  data_t() {}
  data_t(ld vl, ld vr, int ve, int vd) {
    l = vl;
    r = vr;
    e = ve;
    d = vd;
  }
};

int solve(void);

int main(void) {

  // FILE *fpin = freopen("data.txt", "r", stdin);
  while (solve()) {
  }

  return 0;
}

int solve(void) {

  int K, R, L;
  ld P, E, T;
  cin >> K >> R >> L >> P >> E >> T;

  /*
          v[?????\\??????][?????????????????????] =
     ??????????????????????????????????????´????????°
  */
  vector<vector<int>> v(K + 1, vector<int>(K + 1, 0));
  stack<data_t> s;
  s.push(data_t((double)L, (double)R, 0, 0));

  while (!s.empty()) {
    data_t current = s.top();
    data_t right(current), wrong(current);
    double h = (current.l + current.r) / 2.0;
    s.pop();
    if (abs(current.r - T) + EPS <= E && abs(current.l - T) + EPS <= E) {
      v[current.d][current.e]++;
      continue;
    } else if (current.r > T + E || current.l < T - E) {
      continue;
    }
    if (current.d == K) {
      if (abs(h - T) <= E) {
        v[K][current.e]++;
      }
      continue;
    }

    right.d = current.d + 1;
    wrong.d = current.d + 1;
    wrong.e = current.e + 1;

    //????????????????????????
    if (h >= T) {
      wrong.r = h;
      right.l = h;
    } else {
      wrong.l = h;
      right.r = h;
    }

    if (right.d <= K) {
      s.push(right);
      s.push(wrong);
    }
  }

  double PP = 0.0;
  for (int i = 0; i < v.size(); i++) {
    for (int j = 0; j < v[i].size(); j++) {
      if (P <= EPS) {
        if (j == 0)
          PP += (double)v[i][j] * pow(1.0 - P, (double)(i - j));
      } else if (P + EPS >= 1.0) {
        if (i == j)
          PP += (double)v[i][j] * pow(P, (double)j);
      } else {
        PP +=
            (double)v[i][j] * pow(P, (double)j) * pow(1.0 - P, (double)(i - j));
      }
    }
  }
  cout << setprecision(6) << fixed << PP << endl;

  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long double ld;
const ld EPS = 10e-12;

struct data_t {
  ld l, r;
  int e, d;
  data_t() {}
  data_t(ld vl, ld vr, int ve, int vd) {
    l = vl;
    r = vr;
    e = ve;
    d = vd;
  }
};

int solve(void);

int main(void) {

  // FILE *fpin = freopen("data.txt", "r", stdin);
  while (solve()) {
  }
  while (true)
    ;
  return 0;
}

int solve(void) {

  int K, R, L;
  ld P, E, T;
  cin >> K >> R >> L >> P >> E >> T;

  /*
          v[?????\\??????][?????????????????????] =
     ??????????????????????????????????????´????????°
  */
  vector<vector<int>> v(K + 1, vector<int>(K + 1, 0));
  stack<data_t> s;
  s.push(data_t((double)L, (double)R, 0, 0));

  while (!s.empty()) {
    data_t current = s.top();
    data_t right(current), wrong(current);
    double h = (current.l + current.r) / 2.0;
    s.pop();
    if (abs(current.r - T) + EPS <= E && abs(current.l - T) + EPS <= E) {
      v[current.d][current.e]++;
      continue;
    } else if (current.r > T + E || current.l < T - E) {
      continue;
    }
    if (current.d == K) {
      if (abs(h - T) + EPS <= E) {
        v[K][current.e]++;
      }
      continue;
    }

    right.d = current.d + 1;
    wrong.d = current.d + 1;
    wrong.e = current.e + 1;

    //????????????????????????
    if (h >= T) {
      wrong.r = h;
      right.l = h;
    } else {
      wrong.l = h;
      right.r = h;
    }

    if (right.d <= K) {
      s.push(right);
      s.push(wrong);
    }
  }

  double PP = 0.0;
  for (int i = 0; i < v.size(); i++) {
    for (int j = 0; j < v[i].size(); j++) {
      PP += (double)v[i][j] * pow(P, (double)j) * pow(1.0 - P, (double)(i - j));
    }
  }
  cout << setprecision(6) << fixed << PP << endl;

  return 0;
}