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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;

struct Event {
  int time;
  int type; // 0 - custermer on, 1 - custermer off, 2 - operation end,
  int id;
};
bool operator<(const Event &e, const Event &f) { // !!! reversed !!!
  if (e.time != f.time)
    return e.time > f.time;
  if (e.type != f.type)
    return e.type > f.type;
  return e.id > f.id;
}

bool check(int OPE, int T, const vector<int> &M, const vector<int> &L,
           const vector<int> &K) {
  int N = M.size();
  priority_queue<Event> que;
  // priority_queue<int, vector<int>, greater<int>> wait;
  set<int> wait;

  vector<int> state(N);
  REP(i, N) {
    wait.insert(i);
    if (K[i] != 1)
      que.push({L[i] + 1, 1, i});
  }

  int rest = N;

  for (int t = 0; t <= T; t++) {
    while (!que.empty() && que.top().time == t) {
      auto e = que.top();
      que.pop();
      // printf("{time:%d, type:%d, id:%d}\\n", e.time, e.type, e.id);
      if (e.type == 0) {
        // wait.push(e.id);
        wait.insert(e.id);
        que.push({t + L[e.id] + 1, 1, e.id});
        state[e.id] = 0;
      } else if (e.type == 1 && state[e.id] == 0) {
        wait.erase(wait.find(e.id));
        que.push({t + K[e.id] - 1, 0, e.id});
        state[e.id] = 1;
      } else if (e.type == 2) {
        rest--;
        OPE++;
      }
    }

    while (OPE > 0 && !wait.empty()) {
      auto it = wait.begin();
      int id = *it;
      wait.erase(it);
      // int id = wait.top(); wait.pop();
      if (state[id] != 0)
        continue;
      state[id] = 2;
      que.push({t + M[id], 2, id});
      OPE--;
    }
  }

  // cout << rest << endl;
  return rest == 0;
}

int main() {
  int N, T;
  while (cin >> N >> T && (N > 0)) {
    vector<int> M(N), L(N), K(N);
    REP(i, N) cin >> M[i] >> L[i] >> K[i];

    for (int i = 1; i <= N; i++) {
      if (check(i, T, M, L, K)) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;

struct Event {
  int time;
  int type; // 0 - custermer on, 1 - custermer off, 2 - operation end,
  int id;
};
bool operator<(const Event &e, const Event &f) { // !!! reversed !!!
  if (e.time != f.time)
    return e.time > f.time;
  if (e.type != f.type)
    return e.type > f.type;
  return e.id > f.id;
}

bool check(int OPE, int T, const vector<int> &M, const vector<int> &L,
           const vector<int> &K) {
  int N = M.size();
  priority_queue<Event> que;
  priority_queue<int, vector<int>, greater<int>> wait;

  vector<int> state(N);
  REP(i, N) {
    wait.push(i);
    que.push({L[i] + 1, 1, i});
  }

  int rest = N;

  for (int t = 0; t <= T; t++) {
    while (!que.empty() && que.top().time == t) {
      auto e = que.top();
      que.pop();
      // printf("{time:%d, type:%d, id:%d}\\n", e.time, e.type, e.id);
      if (e.type == 0) {
        wait.push(e.id);
        que.push({t + L[e.id], 1, e.id});
        state[e.id] = 0;
      } else if (e.type == 1 && state[e.id] == 0) {
        que.push({t + K[e.id] - 1, 0, e.id});
        state[e.id] = 1;
      } else if (e.type == 2) {
        rest--;
        OPE++;
      }
    }

    while (OPE > 0 && !wait.empty()) {
      int id = wait.top();
      wait.pop();
      if (state[id] != 0)
        continue;
      state[id] = 2;
      // printf("get %d (%d -> %d)\\n", id, t, t + M[id]);
      que.push({t + M[id], 2, id});
      OPE--;
    }
  }

  // cout << rest << endl;
  return rest == 0;
}

int main() {
  int N, T;
  while (cin >> N >> T && (N > 0)) {
    vector<int> M(N), L(N), K(N);
    REP(i, N) cin >> M[i] >> L[i] >> K[i];

    for (int i = 1; i <= N; i++) {
      if (check(i, T, M, L, K)) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}