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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

int cnt;
bool first;
const int INF = 1000000;

class Park {
public:
  int leftTime[2];
  int ID[2];
  bool car[2];

  Park() { rep(i, 2) car[i] = false; }

  void next() {
    rep(i, 2) {
      if (car[i]) {
        leftTime[i]--;
      }
    }
    rep(i, 2) {
      if (car[i] && leftTime[i] <= 0) {
        if (first) {
          first = false;
        } else
          cout << ' ';
        cnt++;
        cout << ID[i] + 1;
        car[i] = false;
      }
      if (car[i])
        return;
    }
    return;
  }

  void put(int id, int time) {
    int tmp = 1;
    if (car[1])
      tmp = 0;
    car[tmp] = true;
    ID[tmp] = id;
    leftTime[tmp] = time;
    return;
  }

  int diff(int time) { return abs(time - leftTime[1]); }
};

main() {

  int n, m;
  while (cin >> m >> n) {
    if (m + n == 0)
      break;
    Park p[m];
    first = true;
    int time = 0, id = 0;
    cnt = 0;
    queue<int> ID, Data;
    while (cnt < n) {
      if (id < n && time == 0) {
        int tmp;
        cin >> tmp;
        ID.push(id);
        Data.push(tmp);
        id++;
      }

      while (!ID.empty()) {
        int next = -1;
        rep(i, m) {
          if (p[i].car[1])
            continue;
          next = i;
          break;
        }
        if (next != -1) {
          p[next].put(ID.front(), Data.front());
          ID.pop();
          Data.pop();
          continue;
        }

        int mini = INF, nTime = Data.front();
        rep(i, m) {
          if (p[i].car[0])
            continue;
          if (p[i].leftTime[1] < nTime)
            continue;
          if (mini > p[i].diff(nTime)) {
            mini = p[i].diff(nTime);
            next = i;
          }
        }
        if (next != -1) {
          p[next].put(ID.front(), Data.front());
          ID.pop();
          Data.pop();
          continue;
        }

        rep(i, m) {
          if (p[i].car[0])
            continue;
          if (mini > p[i].diff(nTime)) {
            mini = p[i].diff(nTime);
            next = i;
          }
        }
        if (next != -1) {
          p[next].put(ID.front(), Data.front());
          ID.pop();
          Data.pop();
          continue;
        }
        break;
      }
      time++;
      if (time == 10)
        time = 0;
      rep(i, m) p[i].next();
    }
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e10, EPS = 1e-9;

int n, m;
int ans[100], na;

struct S {
  int t[2], id[2];
  S() { rep(i, 2) id[i] = 0; }
  void next() {
    rep(i, 2) if (id[i]) t[i]--;
    rep(i, 2) if (id[i]) {
      if (t[i] <= 0)
        ans[na++] = id[i], id[i] = 0;
      return;
    }
  }
  void put(int cid, int time) {
    int p = id[1] ? 0 : 1;
    id[p] = cid;
    t[p] = time;
  }
  int diff(int time) { return abs(time - t[1]); }
};

S spc[10];

int pfind(queue<int> &Data) {
  rep(i, m) if (!spc[i].id[1]) return i;

  int mn = inf, nTime = Data.front(), ret = -1;
  rep(i, m) if (!spc[i].id[0] &&
                spc[i].t[1] >= nTime) if (mn > spc[i].diff(nTime)) {
    mn = spc[i].diff(nTime);
    ret = i;
  }
  if (ret != -1)
    return ret;

  rep(i, m) if (!spc[i].id[0] && mn > spc[i].diff(nTime)) mn =
      spc[i].diff(nTime),
                                                          ret = i;

  return ret;
}

int main() {
  while (scanf("%d%d", &m, &n), m) {
    int time = 0, id = 1;
    na = 0;
    queue<int> ID, Data;
    while (na < n) {
      if (id <= n && time == 0) {
        int tmp;
        scanf("%d", &tmp);
        ID.push(id);
        Data.push(tmp);
        id++;
      }
      while (!ID.empty()) {
        int next = pfind(Data);

        if (next != -1) {
          spc[next].put(ID.front(), Data.front());
          ID.pop();
          Data.pop();
        }
      }
      time++;
      if (time == 10)
        time = 0;
      rep(i, m) spc[i].next();
    }
    rep(i, na) printf("%d%c", ans[i], i == n - 1 ? '\\n' : ' ');
  }
  return 0;
}