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

#include <climits>
#include <cstdio>
#include <deque>
#include <iostream>
#include <queue>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

int m[1000];
int l[1000];
int k[1000];
int t;
int n;

int cando(int p) {
  vector<bool> ok(n, false);
  int cnt = 0;
  priority_queue<int> op;

  REP(i, p) op.push(0);

  while (op.size() && cnt != n) {
    int tm = -op.top();
    op.pop();

    if (tm >= t)
      break;

    pair<int, int> process = make_pair(INT_MAX, INT_MAX);

    int tmp = 0;

    REP(i, n) if (!ok[i]) {
      int next;
      if (tm % (l[i] + k[i]) <= l[i])
        next = tm;
      else
        next = (l[i] + k[i]) * ((tm + k[i]) / (l[i] + k[i]));
      process = min(process, make_pair(next, i));
      tmp += m[i];
    }

    if (tmp > p * (t - tm))
      break;

    if (process.first != INT_MAX) {
      if (process.first + m[process.second] <= t) {
        op.push(-(process.first + m[process.second]));
        ok[process.second] = true;
        cnt++;
      } else {
        break;
      }
    }
  }

  return cnt == n;
}

int main() {
  while (true) {
    n = getInt();
    t = getInt();

    if (n + t == 0)
      break;

    int mm = 0;

    REP(i, n) {
      m[i] = getInt();
      l[i] = getInt();
      k[i] = getInt();

      mm += m[i];
    }

    int low = 1;
    int high = n;

    while (low + 1 < high) {
      int mid = (low + high) / 2;

      if (cando(mid)) {
        high = mid;
      } else {
        low = mid;
      }
    }

    // printf("mid = %d\\n", (low + high) / 2);
    for (int i = max(1, low - 2);; i++) {
      if (cando(i)) {
        printf("%d\\n", i);
        break;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <cstdio>
#include <deque>
#include <iostream>
#include <queue>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

int m[1000];
int l[1000];
int k[1000];

int main() {
  while (true) {
    int n = getInt();
    int t = getInt();

    if (n + t == 0)
      break;

    int mm = 0;

    REP(i, n) {
      m[i] = getInt();
      l[i] = getInt();
      k[i] = getInt();

      mm += m[i];
    }

    for (int p = max(1, mm / t);; p++) {
      vector<bool> ok(n, false);
      int cnt = 0;
      priority_queue<int> op;

      REP(i, p) op.push(0);

      while (op.size() && cnt != n) {
        int tm = -op.top();
        op.pop();

        if (tm >= t)
          break;

        pair<int, int> process = make_pair(INT_MAX, INT_MAX);

        int tmp = 0;

        REP(i, n) if (!ok[i]) {
          int next;
          if (tm % (l[i] + k[i]) <= l[i])
            next = tm;
          else
            next = (l[i] + k[i]) * ((tm + k[i]) / (l[i] + k[i]));
          process = min(process, make_pair(next, i));
          tmp += m[i];
        }

        if (tmp > p * (t - tm))
          break;

        if (process.first != INT_MAX) {
          if (process.first + m[process.second] <= t) {
            op.push(-(process.first + m[process.second]));
            ok[process.second] = true;
            cnt++;
          } else {
            break;
          }
        }
      }

      if (cnt == n) {
        printf("%d\\n", p);
        break;
      }
    }
  }

  return 0;
}