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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef pair<int, int> pii;

bool isCalling[2001];
bool isTelling[2001];
bool isFin[2001];
int N, T;
int Mi[2001];
int Li[2001];
int Ki[2001];
pii datas[2001];

class Event {
public:
  // 0->qªhang up
  // 1->qªdbJn
  // 2->operatorªdbðI¹
  int cid;
  int time;
  Event(int c, int t) { cid = c, time = t; }
  Event();
  bool operator<(const Event &e) const { return (this->time > e.time); }
};
// nlÌIy[^ªvéÆ«ÌV~[V
bool check(int n) {
  memset(isFin, 0, sizeof(isFin));
  priority_queue<Event> pq;
  int curTime = 0;
  for (int i = 0; i < n; i++) {
    pq.push(Event(i, Mi[i]));
    isFin[i] = true;
  }
  // »Ýq}ÈopÌ
  int opr = 0;
  int cnt = N;
  while (pq.size()) {
    // ¡ñÌCxgªI¹µ½opðæèo·
    Event e = pq.top();
    pq.pop();
    curTime = e.time;
    cnt--;
    opr++;
    // óÅ©ÂÜ¾IíÁÄ¢È¢Cxgª¶Ý
    if (pq.size() == 0) {
      if (cnt != 0 && opr != 0) {
        // ÊbÂ\\ÉÈéÜÅª¢ÔÉÀ×é
        int idx = 0;
        for (int i = 0; i < N; i++) {
          if (isFin[i])
            continue;
          int a = e.time % (Ki[i] + Li[i]);
          if (a >= 0 && a <= Li[i]) {
            if (opr > 0) {
              pq.push(Event(i, curTime + Mi[i]));
              isFin[i] = true;
              opr--;
              if (opr == 0)
                break;
            }
          } else {
            int b = Ki[i] - (a - Li[i]);
            datas[idx].first = b;
            datas[idx].second = -i;
            idx++;
          }
        }
        sort(datas, datas + idx, greater<pii>());
        int nxt = opr;
        for (int i = 0; i < min(idx, opr); i++) {
          pq.push(Event(-datas[i].second,
                        curTime + datas[i].first + Mi[-datas[i].second]));
          isFin[-datas[i].second] = true;
          nxt--;
        }
        opr = nxt;
      }
    }
    // ñÌCxgªI¹·éÜÅÌÔÜÅÉAdbÂ\\ÔÉÈéàÌð·×Ä·
    else {
      if (opr == 0)
        continue;
      int neft = pq.top().time;
      // ÊbÂ\\ÉÈéÜÅª¢ÔÉÀ×é
      int idx = 0;
      for (int i = 0; i < N; i++) {
        if (isFin[i])
          continue;
        int a = e.time % (Ki[i] + Li[i]);
        if (a >= 0 && a <= Li[i]) {
          if (opr > 0) {
            pq.push(Event(i, curTime + Mi[i]));
            isFin[i] = true;
            opr--;
            if (opr == 0)
              break;
          }
        } else {
          int b = Ki[i] - (a - Li[i]);
          // ÊbÂ\\ÉÈéÔªneftæè¬³¢ÈçÎ
          if (b + curTime < neft) {
            datas[idx].first = b;
            datas[idx].second = -i;
            idx++;
          }
        }
      }
      sort(datas, datas + idx, greater<pii>());
      int nxt = opr;
      for (int i = 0; i < min(idx, opr); i++) {
        pq.push(Event(-datas[i].second,
                      curTime + datas[i].first + Mi[-datas[i].second]));
        isFin[-datas[i].second] = true;
        nxt--;
      }
      opr = nxt;
    }
  }
  return curTime <= T;
}

int main() {

  while (cin >> N >> T && (N | T)) {
    for (int i = 0; i < N; i++)
      cin >> Mi[i] >> Li[i] >> Ki[i];
    for (int i = 1; i <= N; i++) {
      if (check(i)) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef pair<int, int> pii;

bool isCalling[2001];
bool isTelling[2001];
bool isFin[2001];
int N, T;
int Mi[2001];
int Li[2001];
int Ki[2001];
pii datas[2001];

class Event {
public:
  // 0->qªhang up
  // 1->qªdbJn
  // 2->operatorªdbðI¹
  int cid;
  int time;
  Event(int c, int t) { cid = c, time = t; }
  Event();
  bool operator<(const Event &e) const { return (this->time > e.time); }
};
// nlÌIy[^ªvéÆ«ÌV~[V
bool check(int n) {
  memset(isFin, 0, sizeof(isFin));
  priority_queue<Event> pq;
  int curTime = 0;
  for (int i = 0; i < n; i++) {
    pq.push(Event(i, Mi[i]));
    isFin[i] = true;
  }
  // »Ýq}ÈopÌ
  int opr = 0;
  int cnt = N;
  while (pq.size()) {
    // ¡ñÌCxgªI¹µ½opðæèo·
    Event e = pq.top();
    pq.pop();
    curTime = e.time;
    cnt--;
    opr++;
    // óÅ©ÂÜ¾IíÁÄ¢È¢Cxgª¶Ý
    if (pq.size() == 0) {
      if (cnt != 0 && opr != 0) {
        // ÊbÂ\\ÉÈéÜÅª¢ÔÉÀ×é
        int idx = 0;
        for (int i = 0; i < N; i++) {
          if (isFin[i])
            continue;
          int a = e.time % (Ki[i] + Li[i]);
          if (a >= 0 && a <= Li[i]) {
            if (opr > 0) {
              pq.push(Event(i, curTime + Mi[i]));
              isFin[i] = true;
              opr--;
            }
          } else {
            int b = Ki[i] - (a - Li[i]);
            datas[idx].first = b;
            datas[idx].second = -i;
            idx++;
          }
          // ·ÅÉcèÌopðz¦Ä¢é
          // if(idx>=opr)break;
        }
        sort(datas, datas + idx, greater<pii>());
        int nxt = opr;
        for (int i = 0; i < min(idx, opr); i++) {
          pq.push(Event(-datas[i].second,
                        curTime + datas[i].first + Mi[-datas[i].second]));
          isFin[-datas[i].second] = true;
          nxt--;
        }
        opr = nxt;
      }
    }
    // ñÌCxgªI¹·éÜÅÌÔÜÅÉAdbÂ\\ÔÉÈéàÌð·×Ä·
    else {
      if (opr == 0)
        continue;
      int neft = pq.top().time;
      // ÊbÂ\\ÉÈéÜÅª¢ÔÉÀ×é
      int idx = 0;
      for (int i = 0; i < N; i++) {
        if (isFin[i])
          continue;
        int a = e.time % (Ki[i] + Li[i]);
        if (a >= 0 && a <= Li[i]) {
          if (opr > 0) {
            pq.push(Event(i, curTime + Mi[i]));
            isFin[i] = true;
            opr--;
          }
        } else {
          int b = Ki[i] - (a - Li[i]);
          // ÊbÂ\\ÉÈéÔªneftæè¬³¢ÈçÎ
          if (b + curTime < neft) {
            datas[idx].first = b;
            datas[idx].second = -i;
            idx++;
          }
        }
        // ·ÅÉcèÌopðz¦Ä¢é
        // if(idx>=opr)break;
      }
      sort(datas, datas + idx, greater<pii>());
      int nxt = opr;
      for (int i = 0; i < min(idx, opr); i++) {
        pq.push(Event(-datas[i].second,
                      curTime + datas[i].first + Mi[-datas[i].second]));
        isFin[-datas[i].second] = true;
        nxt--;
      }
      opr = nxt;
    }
  }
  return curTime <= T;
}

int main() {

  while (cin >> N >> T && (N | T)) {
    for (int i = 0; i < N; i++)
      cin >> Mi[i] >> Li[i] >> Ki[i];
    for (int i = 1; i <= N; i++) {
      if (check(i)) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}