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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

struct cust {
  int m;
  int l;
  int k;
};

struct query {
  int type;
  int time;
  int id;
};
struct aa {
  int id;
  int time;
};
class Compare {
public:
  bool operator()(const query &l, const query &r) {
    return l.time == r.time ? l.type == r.type ? l.id > r.id : l.type > r.type
                            : l.time > r.time;
  }
  bool operator()(const aa &l, aa &r) { return l.time > r.time; }
};

bool check(const int T, const vector<cust> &cus, const int n) {
  int waitnum = n;
  priority_queue<query, vector<query>, Compare> que;
  // vector<int>waits(cus.size());
  set<int> waits;
  for (int i = 0; i < cus.size(); ++i) {
    que.push(query{0, 0, i});
  }
  while (!que.empty()) {
    query q(que.top());
    que.pop();
    if (q.type == 1) {
      auto it = waits.begin();
      if (it == waits.end()) {
        waitnum++;
      } else {
        const int cid = *it;
        const int nexttime = q.time + cus[cid].m;
        if (nexttime < T) {
          waits.erase(it);
          que.push(query{1, nexttime, 0});
        } else if (nexttime == T) {
          waits.erase(it);
        } else {
          return false;
        }
      }
    } else if (q.type == 0) {
      const int cid = q.id;
      if (waitnum) {
        waitnum--;
        const int nexttime = q.time + cus[cid].m;
        if (nexttime < T) {
          que.push(query{1, nexttime, 0});
        } else if (nexttime == T) {
        } else {
          return false;
        }
      } else {
        waits.emplace(cid);
        const int nexttime = q.time + cus[cid].l;
        que.push(query{2, nexttime, cid});
      }
    } else if (q.type == 2) {
      const int cid = q.id;
      auto it = waits.find(cid);
      if (it != waits.end()) {
        waits.erase(it);
        const int nexttime = q.time + cus[cid].k;
        if (nexttime < T) {
          que.push(query{0, nexttime, cid});
        } else {
          return false;
        }
      } else {
      }
    }
  }
  return true;
}

int main() {
  while (1) {
    int N, T;
    cin >> N >> T;
    if (!N)
      break;
    vector<cust> cus;
    int totalm = 0;
    for (int i = 0; i < N; ++i) {
      int m, l, k;
      cin >> m >> l >> k;
      totalm += m;
      cus.push_back(cust{m, l, k});
    }
    /*int amin = 0;
    int amax = N;
    while (amin + 1 != amax) {
            int amid = (amin + amax) / 2;
            if (check(T, cus, amid)) {
                    amax = amid;
            }
            else {
                    amin = amid;
            }
    }
    cout << amax << endl;*/
    int ans = 0;
    int amin = (totalm - 1) / T + 1;
    for (int n = amin; n <= N; ++n) {
      if (check(T, cus, n)) {
        ans = n;
        break;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

struct cust {
  int m;
  int l;
  int k;
};

struct query {
  int type;
  int time;
  int id;
};
struct aa {
  int id;
  int time;
};
class Compare {
public:
  bool operator()(const query &l, const query &r) {
    return l.time == r.time ? l.type == r.type ? l.id > r.id : l.type > r.type
                            : l.time > r.time;
  }
  bool operator()(const aa &l, aa &r) { return l.time > r.time; }
};

bool check(const int T, const vector<cust> &cus, const int n) {
  int waitnum = n;
  priority_queue<query, vector<query>, Compare> que;
  // vector<int>waits(cus.size());
  set<int> waits;
  for (int i = 0; i < cus.size(); ++i) {
    que.push(query{0, 0, i});
  }
  while (!que.empty()) {
    query q(que.top());
    que.pop();
    if (q.type == 1) {
      auto it = waits.begin();
      if (it == waits.end()) {
        waitnum++;
      } else {
        const int cid = *it;
        const int nexttime = q.time + cus[cid].m;
        if (nexttime < T) {
          waits.erase(it);
          que.push(query{1, nexttime, 0});
        } else if (nexttime == T) {
          waits.erase(it);
        } else {
          return false;
        }
      }
    } else if (q.type == 0) {
      const int cid = q.id;
      if (waitnum) {
        waitnum--;
        const int nexttime = q.time + cus[cid].m;
        if (nexttime < T) {
          que.push(query{1, nexttime, 0});
        } else if (nexttime == T) {
        } else {
          return false;
        }
      } else {
        waits.emplace(cid);
        const int nexttime = q.time + cus[cid].l;
        que.push(query{2, nexttime, cid});
      }
    } else if (q.type == 2) {
      const int cid = q.id;
      auto it = waits.find(cid);
      if (it != waits.end()) {
        waits.erase(it);
        const int nexttime = q.time + cus[cid].k;
        if (nexttime < T) {
          que.push(query{0, nexttime, cid});
        } else {
          return false;
        }
      } else {
      }
    }
  }
  return true;
}

int main() {
  while (1) {
    int N, T;
    cin >> N >> T;
    if (!N)
      break;
    vector<cust> cus;
    for (int i = 0; i < N; ++i) {
      int m, l, k;
      cin >> m >> l >> k;
      cus.push_back(cust{m, l, k});
    }
    /*int amin = 0;
    int amax = N;
    while (amin + 1 != amax) {
            int amid = (amin + amax) / 2;
            if (check(T, cus, amid)) {
                    amax = amid;
            }
            else {
                    amin = amid;
            }
    }
    cout << amax << endl;*/
    int ans = 0;
    for (int n = 1; n <= N; ++n) {
      if (check(T, cus, n)) {
        ans = n;
        break;
      }
    }
    cout << ans << endl;
  }

  return 0;
}