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

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

const int INF = 1 << 30;

class Parking {
public:
  int id, time;

  Parking(int _id = -1, int _time = INF) : id(_id), time(_time) {}
};

int main() {
  int n, m;
  while (cin >> m >> n) {
    if ((n | m) == 0)
      break;

    vector<int> cars(n);
    for (int i = 0; i < n; ++i)
      cin >> cars[i];

    int id = 0;
    vector<int> ans;
    vector<Parking> p1(m), p2(m);
    for (int time = 0; ans.size() < n; ++time) {
      for (int i = 0; i < m; ++i) {
        if (p1[i].id != -1)
          --p1[i].time;
        if (p2[i].id != -1)
          --p2[i].time;
      }

      for (int i = 0; i < m; ++i) {
        bool out = false;
        if (p1[i].id != -1 && p1[i].time <= 0) {
          ans.push_back(p1[i].id);
          p1[i].id = -1;
          p1[i].time = INF;
          out = true;
        }

        if (p1[i].id == -1 && p2[i].id != -1 && p2[i].time <= 0) {
          ans.push_back(p2[i].id);
          p2[i].id = -1;
          p2[i].time = INF;
        }

        if (out && p2[i].id != -1) {
          p1[i].id = p2[i].id;
          p1[i].time = p2[i].time;
          p2[i].id = -1;
          p2[i].time = INF;
        }
      }

      while (id < n && id * 10 <= time) {
        bool in = false;
        for (int i = 0; i < m; ++i) {
          if (p1[i].id == -1) {
            p1[i].id = id;
            p1[i].time = cars[id++];
            in = true;
            break;
          }
        }

        if (in)
          continue;

        int minIndex = -1;
        for (int i = 0; i < m; ++i) {
          if (p2[i].id != -1)
            continue;

          if (p1[i].time >= cars[id]) {
            if (minIndex == -1 || p1[i].time < p1[minIndex].time)
              minIndex = i;
          }
        }

        if (minIndex != -1) {
          p2[minIndex].id = p1[minIndex].id;
          p2[minIndex].time = p1[minIndex].time;
          p1[minIndex].id = id;
          p1[minIndex].time = cars[id++];
          continue;
        }

        for (int i = 0; i < m; ++i) {
          if (p2[i].id != -1)
            continue;

          if (p1[i].time < cars[id]) {
            if (minIndex == -1 || p1[i].time > p1[minIndex].time)
              minIndex = i;
          }
        }

        if (minIndex != -1) {
          p2[minIndex].id = p1[minIndex].id;
          p2[minIndex].time = p1[minIndex].time;
          p1[minIndex].id = id;
          p1[minIndex].time = cars[id++];
          continue;
        }

        break;
      }
    }

    for (int i = 0; i < n - 1; ++i)
      cout << ans[i] + 1 << " ";
    cout << ans[n - 1] + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const long long int INF = 1LL << 60;

class Parking {
public:
  int id;
  long long int time;

  Parking(int _id = -1, long long int _time = INF) : id(_id), time(_time) {}
};

int main() {
  int n, m;
  while (cin >> m >> n) {
    if ((n | m) == 0)
      break;

    vector<long long int> cars(n);
    for (int i = 0; i < n; ++i)
      cin >> cars[i];

    int empty = 2 * m;
    long long int time = 0;
    vector<int> ans;
    vector<Parking> p1(m), p2(m);
    for (int i = 0; i < n; ++i) {
      long long int min_time = INF;
      for (int j = 0; j < m; ++j)
        min_time = min(min_time, p1[j].time);

      while (min_time < i * 10LL) {
        for (int j = 0; j < m; ++j) {
          bool out = false;
          if (p1[j].time <= time) {
            ans.push_back(p1[j].id);
            p1[j].id = -1;
            p1[j].time = INF;
            out = true;
            ++empty;
          }

          if (p1[j].id == -1 && p2[j].time <= time) {
            ans.push_back(p2[j].id);
            p2[j].id = -1;
            p2[j].time = INF;
            ++empty;
          } else if (out) {
            p1[j].id = p2[j].id;
            p1[j].time = p2[j].time;
            p2[j].id = -1;
            p2[j].time = INF;
          }
        }

        min_time = INF;
        for (int j = 0; j < m; ++j)
          min_time = min(min_time, p1[j].time);
      }

      time = max(time, i * 10LL);

      for (;;) {
        for (int j = 0; j < m; ++j) {
          bool out = false;
          if (p1[j].time <= time) {
            ans.push_back(p1[j].id);
            p1[j].id = -1;
            p1[j].time = INF;
            out = true;
            ++empty;
          }

          if (p1[j].id == -1 && p2[j].time <= time) {
            ans.push_back(p2[j].id);
            p2[j].id = -1;
            p2[j].time = INF;
            ++empty;
          } else if (out) {
            p1[j].id = p2[j].id;
            p1[j].time = p2[j].time;
            p2[j].id = -1;
            p2[j].time = INF;
          }
        }

        if (empty > m) {
          for (int j = 0; j < m; ++j) {
            if (p1[j].id == -1) {
              p1[j].id = i;
              p1[j].time = time + cars[i];
              break;
            }
          }
          --empty;
          break;
        }

        if (empty > 0) {
          int minIndex = -1;
          for (int j = 0; j < m; ++j) {
            if (p2[j].id != -1)
              continue;

            if (p1[j].time >= time + cars[i]) {
              if (minIndex == -1 || p1[j].time < p1[minIndex].time)
                minIndex = j;
            }
          }

          if (minIndex != -1) {
            p2[minIndex].id = p1[minIndex].id;
            p2[minIndex].time = p1[minIndex].time;
            p1[minIndex].id = i;
            p1[minIndex].time = time + cars[i];
            --empty;
            break;
          }

          for (int j = 0; j < m; ++j) {
            if (p2[j].id != -1)
              continue;

            if (p1[j].time < time + cars[i]) {
              if (minIndex == -1 || p1[j].time > p1[minIndex].time)
                minIndex = j;
            }
          }

          if (minIndex != -1) {
            p2[minIndex].id = p1[minIndex].id;
            p2[minIndex].time = p1[minIndex].time;
            p1[minIndex].id = i;
            p1[minIndex].time = time + cars[i];
            --empty;
            break;
          }
        }

        if (empty == 0) {
          min_time = INF;
          for (int j = 0; j < m; ++j)
            min_time = min(min_time, p1[j].time);
          time = min_time;
        }
      }
    }

    while (ans.size() < n) {
      for (int i = 0; i < m; ++i) {
        bool out = false;
        if (p1[i].time <= time) {
          ans.push_back(p1[i].id);
          p1[i].id = -1;
          p1[i].time = INF;
          out = true;
        }

        if (p1[i].id == -1 && p2[i].time <= time) {
          ans.push_back(p2[i].id);
          p2[i].id = -1;
          p2[i].time = INF;
        } else if (out) {
          p1[i].id = p2[i].id;
          p1[i].time = p2[i].time;
          p2[i].id = -1;
          p2[i].time = INF;
        }
      }

      long long int min_time = INF;
      for (int i = 0; i < m; ++i)
        min_time = min(min_time, p1[i].time);
      time = min_time;
    }

    for (int i = 0; i < n - 1; ++i)
      cout << ans[i] + 1 << " ";
    cout << ans.back() + 1 << endl;
  }
  return 0;
}