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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int, int> PI;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back

PI park[10][2]; // goTime,carNo;
int m, n,
    inpark; // ÔãÌAÔÌAÔêàÌÔÌ

queue<PI> Q; // waitTime,carNo;

// o­·éÔÌÔðÔ·B
int left(int time) {

  rep(i, m) {
    if (park[i][0].first <= time && park[i][0].first > 0) {
      PI ret = park[i][0];
      park[i][0] = park[i][1]; // ãiÌÔðº°éB
      park[i][1] = mp(0, 0);
      inpark--;
      return ret.second;
    }
  }
  // o­·éÔªÈ¯êÎ-1
  return -1;
}

// ÔðâßéB
void stop(int cometime, int wait, int no) {
  int mini = -1, mint, maxi = -1, maxt;

  // ·ÅÉÔÅ êÎL[ÉüêéÄ^[
  if (inpark >= 2 * m) {
    Q.push(mp(wait, no));
    return;
  }

  inpark++;

  // êiÚªó¢Ä¢êÎ»±ÉüêÄ^[
  rep(i, m) {
    if (park[i][0].first == 0) {
      park[i][0] = mp(cometime + wait, no);
      return;
    }
  }

  // oÉªß¢êiÚðIèB
  rep(i, m) {
    if (park[i][1].first)
      continue;
    if ((park[i][0].first < mint || mini == -1) &&
        cometime + wait <= park[i][0].first) {
      mint = park[i][0].first;
      mini = i;
    }
    if ((park[i][0].first > maxt || maxi == -1) &&
        cometime + wait >= park[i][0].first) {
      maxt = park[i][0].first;
      maxi = i;
    }
  }

  // oÉªüêéÔæèx¢àÌª êÎA»êðãiÉµÄAºiÉüÉB
  if (mini != -1) {
    park[mini][1] = park[mini][0];
    park[mini][0] = mp(cometime + wait, no);
  } else { // »¤ÅÈ¯êÎãiÉÈéÔÌÒ@ÔªêÔZ¢Æ±ëÉüÉ
    park[maxi][1] = park[maxi][0];
    park[maxi][0] = mp(cometime + wait, no);
  }
}

int main() {
  while (cin >> m >> n) {
    if (m == 0 && n == 0)
      break;
    int time = 0, out = 0,
        f = 0; // »ÝAoÉµ½ÔA\\¦µ½©Ç¤©
    inpark = 0; // »ÝÌÔ

    // Ôãú»
    rep(i, 10) rep(j, 2) park[i][j] = mp(0, 0);

    // oÉµ½ÔªüÁÄ­éÔæè­È¢Ô[v
    while (out < n) {
      ++time;

      // »ÝÉoÉ·éÔª êÎ»êð\\¦B
      while (true) {
        int carno = left(time);
        if (carno == -1)
          break;
        else {
          if (f)
            cout << " ";
          cout << carno;
          f = 1;
          ++out;
        }
      }

      // ÔÅÈ­AÒ@Ôª éÈçÎ»êðüÉB
      while (!Q.empty() && inpark < 2 * m) {
        int wait = Q.front().first, no = Q.front().second;
        Q.pop();
        stop(time, wait, no);
      }

      // »ÝÉ½Ôª êÎüÉB
      if (time % 10 == 0 && time <= 10 * n) {
        int wait;
        cin >> wait;
        stop(time, wait, time / 10);
      }
    }
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int, int> PI;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back

PI park[10][2]; // gotime,no;
int m, n, in;

queue<PI> Q; // wait,no;

int left(int time) {
  rep(i, m) {
    if (park[i][0].first <= time && park[i][0].first) {
      PI ret = park[i][0];
      park[i][0] = park[i][1];
      park[i][1] = mp(0, 0);
      in--;
      return ret.second;
    }
  }
  return -1;
}

void stop(int cometime, int wait, int no) {
  int mini = -1, mint, maxi = -1, maxt;

  if (in == 2 * m) {
    Q.push(mp(wait, no));
    return;
  }
  in++;
  rep(i, m) {
    if (park[i][0].first == 0) {
      park[i][0] = mp(cometime + wait, no);
      return;
    }
  }

  rep(i, m) {
    if (park[i][1].first)
      continue;
    if ((park[i][0].first < mint || mini == -1) &&
        cometime + wait <= park[i][0].first) {
      mint = park[i][0].first;
      mini = i;
    }
    if ((park[i][0].first > maxt || maxi == -1) &&
        cometime + wait >= park[i][0].first) {
      maxt = park[i][0].first;
      maxi = i;
    }
  }

  if (maxi != -1) {
    park[maxi][1] = park[maxi][0];
    park[maxi][0] = mp(cometime + wait, no);
  } else {
    park[mini][1] = park[mini][0];
    park[mini][0] = mp(cometime + wait, no);
  }
}

int main() {
  while (cin >> m >> n, n) {
    int time = 0, out = 0, f = 0, in = 0;
    rep(i, 10) rep(j, 2) park[i][j] = mp(0, 0);

    int mlo = 0;
    while (out < n) {
      ++time;

      mlo++;
      if (mlo > 3000)
        return 0;

      int loop = 0;
      while (true) {

        ++loop;
        if (loop > 1200)
          return 0;

        int tout = left(time);
        if (tout == -1)
          break;
        else {
          if (f)
            cout << " ";
          cout << tout;
          f = 1;
          ++out;
        }
      }

      while (!Q.empty() && in < 2 * m) {
        int wait = Q.front().first, no = Q.front().second;
        Q.pop();
        stop(time, wait, no);
      }

      if (time % 10 == 0 && time <= 10 * n) {
        int t;
        cin >> t;
        stop(time, t, time / 10);
      }
    }
    cout << endl;
  }
}