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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> P;
const int INF = 1000000000;

int M, N;
vector<int> t;

bool input() {
  cin >> M >> N;
  if (!M && !N) {
    return false;
  }
  t.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> t[i];
  }
  return true;
}

void solve() {
  int time = 0;
  vector<int> answer;
  queue<int> tyusha_machi;
  vector<P> park_id;
  vector<P> park_time;
  park_id.resize(M);
  park_time.resize(M);
  for (int i = 0; i < M; ++i) {
    park_id[i].first = park_id[i].second = -1;
    park_time[i].first = park_time[i].second = -1;
  }

  while (answer.size() < N) {
    if (time % 10 == 0 && time / 10 < N) {
      tyusha_machi.push(time / 10);
    }

    // shukko
    for (int i = 0; i < M; ++i) {
      if (park_id[i].first != -1 && park_time[i].first <= time) {
        answer.push_back(park_id[i].first);
        park_id[i].first = -1;
        park_time[i].first = -1;
      }
      if (park_id[i].first == -1 && park_id[i].second != -1 &&
          park_time[i].second <= time) {
        answer.push_back(park_id[i].second);
        park_id[i].second = -1;
        park_time[i].second = -1;
      }
    }

    // tyusha (try second floor)
    while (!tyusha_machi.empty()) {
      int space_id = -1;
      for (int i = 0; i < M; ++i) {
        if (park_id[i].first == -1 && park_id[i].second == -1) {
          space_id = i;
          break;
        }
      }
      if (space_id == -1) {
        break;
      }
      int car_id = tyusha_machi.front();
      tyusha_machi.pop();
      park_id[space_id].second = car_id;
      park_time[space_id].second = time + t[car_id];
    }

    // tyusha (try first floor)
    while (!tyusha_machi.empty()) {
      int space_id = -1;
      bool b = false;
      int tt = time + t[tyusha_machi.front()];
      for (int i = 0; i < M; ++i) {
        if (park_id[i].first != -1) {
          continue;
        }
        if (tt <= park_time[i].second) {
          b = true;
          break;
        }
      }
      if (b) {
        int min_t = INF;
        for (int i = 0; i < M; ++i) {
          if (park_id[i].first != -1) {
            continue;
          }
          if (tt <= park_time[i].second) {
            if (min_t > park_time[i].second) {
              min_t = park_time[i].second;
              space_id = i;
            }
          }
        }
      } else {
        int max_t = -1;
        for (int i = 0; i < M; ++i) {
          if (park_id[i].first != -1) {
            continue;
          }
          if (max_t < park_time[i].second) {
            max_t = park_time[i].second;
            space_id = i;
          }
        }
      }
      if (space_id == -1) {
        break;
      }
      int car_id = tyusha_machi.front();
      tyusha_machi.pop();
      park_id[space_id].first = car_id;
      park_time[space_id].first = time + t[car_id];
    }

    ++time;
  }

  for (int i = 0; i < N; ++i) {
    if (i)
      cout << " ";
    cout << answer[i] + 1;
  }
  cout << endl;
}

int main() {
  while (input()) {
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

typedef pair<int, int> P;
const int INF = 1000000000;

int M, N;
vector<int> t;

bool input() {
  cin >> M >> N;
  if (!M && !N) {
    return false;
  }
  t.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> t[i];
  }
  return true;
}

bool exec_tyusha(int time, int car_id, vector<P> &park, vector<int> &space,
                 const vector<int> &shukko_time) {
  for (int i = 0; i < M; ++i) {
    if (park[i].first == -1 && park[i].second == -1) {
      park[i].second = car_id;
      space[car_id] = 2 * i + 1;
      return true;
    }
  }
  int my_shukko_time = time + t[car_id];
  int min_d = INF;
  int min_id = -1;
  bool machi_flag = true;
  for (int i = 0; i < M; ++i) {
    if (park[i].first != -1 && park[i].second != -1) {
      continue;
    }
    int d = shukko_time[park[i].second] - my_shukko_time;
    if (d > 0) {
      if (machi_flag) {
        min_d = d;
        min_id = i;
        machi_flag = false;
      } else {
        if (min_d > d) {
          min_d = d;
          min_id = i;
        }
      }
    } else {
      if (min_d > -d) {
        min_d = -d;
        min_id = i;
      }
    }
  }
  if (min_id != 1) {
    park[min_id].first = car_id;
    space[car_id] = 2 * min_id;
    return true;
  }
  return false;
}

void solve() {
  vector<P> park;
  vector<int> space;
  vector<int> answer;
  priority_queue<P, vector<P>, greater<P>> tyusha;
  priority_queue<P, vector<P>, greater<P>> shukko;
  vector<int> shukko_time;
  queue<int> tyusha_machi;
  set<int> shukko_machi;
  park.resize(M);
  space.resize(N);
  shukko_time.resize(N);
  for (int i = 0; i < M; ++i) {
    park[i].first = park[i].second = -1;
  }
  for (int i = 0; i < N; ++i) {
    tyusha.push(P(i * 10, i));
  }

  priority_queue<P, vector<P>, greater<P>> shukko_buf;
  while (!tyusha.empty() || !shukko.empty()) {
    int st = INF;
    int tt = INF;
    if (!shukko.empty()) {
      st = shukko.top().first;
    }
    if (!tyusha.empty()) {
      tt = tyusha.top().first;
    }
    if (st < tt) {
      while (!shukko.empty() && shukko.top().first == st) {
        int id = shukko.top().second;
        shukko_buf.push(P(space[id], id));
        shukko.pop();
      }
      while (!shukko_buf.empty()) {
        int space_id = shukko_buf.top().first >> 1;
        bool is_second = shukko_buf.top().first & 1;
        int car_id = shukko_buf.top().second;
        shukko_buf.pop();
        if (is_second) {
          if (park[space_id].first == -1) {
            answer.push_back(car_id);
            park[space_id].second = -1;
          } else {
            shukko_machi.insert(car_id);
          }
        } else { // first floor
          answer.push_back(car_id);
          park[space_id].first = -1;
          if (shukko_machi.find(park[space_id].second) != shukko_machi.end()) {
            answer.push_back(park[space_id].second);
            park[space_id].second = -1;
            shukko_machi.erase(car_id);
          }
        }
      }
      while (!tyusha_machi.empty()) {
        int car_id = tyusha_machi.front();
        bool success = exec_tyusha(st, car_id, park, space, shukko_time);
        if (success) {
          tyusha_machi.pop();
          shukko_time[car_id] = st + t[car_id];
          shukko.push(P(shukko_time[car_id], car_id));
        } else {
          break;
        }
      }
    } else {
      int car_id = tyusha.top().second;
      tyusha.pop();
      bool success = exec_tyusha(tt, car_id, park, space, shukko_time);
      if (success) {
        shukko_time[car_id] = tt + t[car_id];
        shukko.push(P(shukko_time[car_id], car_id));
      } else {
        tyusha_machi.push(car_id);
      }
    }
  }

  for (int i = 0; i < N; ++i) {
    if (i)
      cout << " ";
    cout << answer[i] + 1;
  }
  cout << endl;
}

int main() {
  while (input()) {
    solve();
  }
  return 0;
}