Problem p00192 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

int main() {
  int m, n;
  while (scanf("%d%d", &m, &n), m) {
    pii car[10][2];
    rep(i, 10) rep(j, 2) car[i][j] = pii(0, 0);

    vi ans;
    queue<pii> Q;
    rep(i, 2000) {
      rep(j, m) {
        rep(k, 2) {
          if (car[j][0].first != 0) {
            --car[j][0].second;
            if (car[j][0].second <= 0) {
              ans.push_back(car[j][0].first);
              car[j][0] = car[j][1];
              car[j][1] = pii(0, 0);
            } else
              k = 2;
          }
        }
        car[j][1].second = max(0, car[j][1].second - 1);
      }
      if (i % 10 == 0 && i / 10 < n) {
        int t;
        scanf("%d", &t);
        Q.push(pii(i / 10 + 1, t));
      }
      while (!Q.empty()) {
        int x = -1;
        rep(j, m) {
          if (car[j][0].first == 0) {
            x = j;
            break;
          }
        }
        int t = Q.front().second;
        if (x == -1) {
          int best = INF;
          rep(j, m) {
            if (car[j][1].first == 0) {
              int diff = car[j][0].second - t;
              if (diff < 0)
                diff = INF / 2 - diff;
              if (diff < best) {
                best = diff;
                x = j;
              }
            }
          }
          if (x == -1)
            break;
          car[x][1] = car[x][0];
        }
        car[x][0] = Q.front();
        Q.pop();
      }
    }
    rep(i, n) printf("%d%c", ans[i], i == n - 1 ? '\\n' : ' ');
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

int main() {
  int m, n;
  while (scanf("%d%d", &m, &n), m) {
    int empty = 20;
    pii car[10][2];
    rep(i, 10) rep(j, 2) car[i][j] = pii(0, 0);

    vi ans;
    queue<pii> Q;
    rep(i, 2000) {
      rep(j, m) {
        rep(k, 2) {
          if (car[j][0].first != 0) {
            --car[j][0].second;
            if (car[j][0].second <= 0) {
              ans.push_back(car[j][0].first);
              car[j][0] = car[j][1];
              car[j][1] = pii(0, 0);
              ++empty;
            } else
              k = 2;
          }
        }
        car[j][1].second = max(0, car[j][1].second - 1);
      }
      if (i % 10 == 0 && i / 10 < n) {
        int t;
        scanf("%d", &t);
        Q.push(pii(i / 10 + 1, t));
      }
      while (empty && !Q.empty()) {
        int x = -1;
        rep(j, m) {
          if (car[j][0].first == 0) {
            x = j;
            break;
          }
        }
        int t = Q.front().second;
        if (x == -1) {
          int best = INF;
          rep(j, m) {
            if (car[j][1].first == 0) {
              int diff = car[j][0].second - t;
              if (diff < 0)
                diff = INF / 2 - diff;
              if (diff < best) {
                best = diff;
                x = j;
              }
            }
          }
          car[x][1] = car[x][0];
        }
        car[x][0] = Q.front();
        Q.pop();
        --empty;
      }
      /*
      if(i%10 == 0){
              printf("i = %d, empty = %d\\n", i, empty);
              rep(j, 2){
                      rep(k, m)
                              printf("%3d:%3d ", car[k][j].first,
      car[k][j].second); puts("");
              }
              getchar();
      }
      */
    }
    rep(i, n) printf("%d%c", ans[i], i == n - 1 ? '\\n' : ' ');
  }
  return 0;
}