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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

struct state {
  int a, b, c;
  //	state(){}
  state(int a, int b, int c) : a(a), b(b), c(c) {}
  bool operator<(const state &x) const {
    return make_tuple(a, b, c) < make_tuple(x.a, x.b, x.c);
  }
  bool operator==(const state &x) const {
    return make_tuple(a, b, c) == make_tuple(x.a, x.b, x.c);
  }
};

random_device seed_gen;
mt19937 rng(seed_gen());

int main() {
  int n, a0, b0, c0;
  cin >> n >> a0 >> b0 >> c0;
  vector<string> s(n);
  rep(i, n) cin >> s[i];

  vector<vector<state>> S(n + 1);
  S[n].emplace_back(0, 0, 0);
  for (int i = n - 1; i >= 0; i--) {
    vector<state> tmp;

    rep(j, S[i + 1].size()) {
      int a = S[i + 1][j].a, b = S[i + 1][j].b, c = S[i + 1][j].c;
      if (s[i] == "AB") {
        tmp.emplace_back(a + 1, b, c);
        tmp.emplace_back(a, b + 1, c);
        if (b > 0)
          tmp.emplace_back(a + 1, b - 1, c);
        if (a > 0)
          tmp.emplace_back(a - 1, b + 1, c);
      } else if (s[i] == "BC") {
        tmp.emplace_back(a, b + 1, c);
        tmp.emplace_back(a, b, c + 1);
        if (c > 0)
          tmp.emplace_back(a, b + 1, c - 1);
        if (b > 0)
          tmp.emplace_back(a, b - 1, c + 1);
      } else {
        tmp.emplace_back(a + 1, b, c);
        tmp.emplace_back(a, b, c + 1);
        if (c > 0)
          tmp.emplace_back(a + 1, b, c - 1);
        if (a > 0)
          tmp.emplace_back(a - 1, b, c + 1);
      }
    }

    sort(tmp.begin(), tmp.end());
    tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());

    rep(j, tmp.size()) {
      bool ok = true;
      rep(k, tmp.size()) if (j != k && tmp[k].a <= tmp[j].a &&
                             tmp[k].b <= tmp[j].b && tmp[k].c <= tmp[j].c) ok =
          false;
      if (ok)
        S[i].emplace_back(tmp[j]);
    }
  }

  auto check = [&](int a, int b, int c, int i) {
    rep(j, S[i].size()) {
      if (a >= S[i][j].a && b >= S[i][j].b && c >= S[i][j].c)
        return true;
    }
    return false;
  };

  if (!check(a0, b0, c0, 0))
    return puts("No"), 0;

  puts("Yes");
  rep(i, n) {
    if (s[i] == "AB") {
      a0++;
      b0--;
      if (check(a0, b0, c0, i + 1)) {
        puts("A");
        continue;
      }
      a0--;
      b0++;
      b0++;
      a0--;
      if (check(a0, b0, c0, i + 1)) {
        puts("B");
        continue;
      }
      b0--;
      a0++;
    } else if (s[i] == "BC") {
      b0++;
      c0--;
      if (check(a0, b0, c0, i + 1)) {
        puts("B");
        continue;
      }
      b0--;
      c0++;
      c0++;
      b0--;
      if (check(a0, b0, c0, i + 1)) {
        puts("C");
        continue;
      }
      c0--;
      b0++;
    } else {
      a0++;
      c0--;
      if (check(a0, b0, c0, i + 1)) {
        puts("A");
        continue;
      }
      a0--;
      c0++;
      c0++;
      a0--;
      if (check(a0, b0, c0, i + 1)) {
        puts("C");
        continue;
      }
      c0--;
      a0++;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

struct state {
  int a, b, c;
  state(int a, int b, int c) : a(a), b(b), c(c) {}
  bool operator<(const state &x) const {
    return make_tuple(a, b, c) < make_tuple(x.a, x.b, x.c);
  }
  bool operator==(const state &x) const {
    return make_tuple(a, b, c) == make_tuple(x.a, x.b, x.c);
  }
};

int main() {
  int n, a0, b0, c0;
  cin >> n >> a0 >> b0 >> c0;
  vector<string> s(n);
  rep(i, n) cin >> s[i];

  vector<vector<state>> S(n + 1);
  S[n].emplace_back(0, 0, 0);
  for (int i = n - 1; i >= 0; i--) {
    vector<state> tmp;

    rep(j, S[i + 1].size()) {
      int a = S[i + 1][j].a, b = S[i + 1][j].b, c = S[i + 1][j].c;
      if (s[i] == "AB") {
        tmp.emplace_back(a + 1, b, c);
        tmp.emplace_back(a, b + 1, c);
      } else if (s[i] == "BC") {
        tmp.emplace_back(a, b + 1, c);
        tmp.emplace_back(a, b, c + 1);
      } else {
        tmp.emplace_back(a + 1, b, c);
        tmp.emplace_back(a, b, c + 1);
      }
    }

    sort(tmp.begin(), tmp.end());
    tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());

    rep(j, tmp.size()) {
      bool ok = true;
      rep(k, tmp.size()) if (j != k && tmp[k].a <= tmp[j].a &&
                             tmp[k].b <= tmp[j].b && tmp[k].c <= tmp[j].c) ok =
          false;
      if (ok)
        S[i].emplace_back(tmp[j]);
    }
  }

  auto check = [&](int a, int b, int c, int i) {
    rep(j, S[i].size()) {
      if (a >= S[i][j].a && b >= S[i][j].b && c >= S[i][j].c)
        return true;
    }
    return false;
  };

  if (!check(a0, b0, c0, 0))
    return puts("No"), 0;

  puts("Yes");
  rep(i, n) {
    if (s[i] == "AB") {
      a0++;
      b0--;
      if (check(a0, b0, c0, i + 1)) {
        puts("A");
        continue;
      }
      a0--;
      b0++;
      b0++;
      a0--;
      if (check(a0, b0, c0, i + 1)) {
        puts("B");
        continue;
      }
      b0--;
      a0++;
    } else if (s[i] == "BC") {
      b0++;
      c0--;
      if (check(a0, b0, c0, i + 1)) {
        puts("B");
        continue;
      }
      b0--;
      c0++;
      c0++;
      b0--;
      if (check(a0, b0, c0, i + 1)) {
        puts("C");
        continue;
      }
      c0--;
      b0++;
    } else {
      a0++;
      c0--;
      if (check(a0, b0, c0, i + 1)) {
        puts("A");
        continue;
      }
      a0--;
      c0++;
      c0++;
      a0--;
      if (check(a0, b0, c0, i + 1)) {
        puts("C");
        continue;
      }
      c0--;
      a0++;
    }
  }

  return 0;
}