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

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, s, e) for (int i = s; i <= e; i++)
#define repd(i, s, e) for (int i = s; i >= e; i--)

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

const int INF = 1000000009;
const int MAX_M = 11;
int m, n, t;
int ct;
stack<P> park[MAX_M];
queue<P> wait;
vector<int> out;

bool parking() {
  if (wait.empty())
    return false;

  P car = wait.front();
  // park
  rep(i, m) {
    if (park[i].empty()) {
      // printf("car %d parks at %d\\n", car.second, i);
      park[i].push(P(car.first + ct, car.second));
      wait.pop();
      return true;
    }
  }

  // situation 1
  int p = -1;
  int minval = INF;
  rep(i, m) {
    if (park[i].size() == 1 && park[i].top().first >= ct + car.first) {
      if (minval > park[i].top().first - (ct + car.first)) {
        minval = park[i].top().first - (ct + car.first);
        p = i;
      }
    }
  }
  if (p >= 0) {
    // printf("car %d parks at %d\\n", car.second, p);
    park[p].push(P(ct + car.first, car.second));
    wait.pop();
    return true;
  }

  // situation 2
  rep(i, m) {
    if (park[i].size() == 1 && park[i].top().first <= ct + car.first) {
      if (minval > ct + car.first - park[i].top().first) {
        minval = ct + car.first - park[i].top().first;
        p = i;
      }
    }
  }
  if (p >= 0) {
    // printf("car %d parks at %d\\n", car.second, p);
    park[p].push(P(ct + car.first, car.second));
    wait.pop();
    return true;
  }
  return false;
}

bool leave() {
  int minval = INF;
  rep(i, m) {
    if (!park[i].empty() && minval > park[i].top().first) {
      minval = park[i].top().first;
    }
  }

  if (minval > ct)
    return false;

  bool update = false;
  rep(i, m) {
    if (!park[i].empty() && park[i].top().first == minval) {
      // printf("car %d leaves from %d\\n", park[i].top().second, i);
      out.push_back(park[i].top().second);
      park[i].pop();
      update = true;
    }

    if (!park[i].empty() && park[i].top().first <= minval) {
      // printf("car %d leaves from %d\\n", park[i].top().second, i);
      out.push_back(park[i].top().second);
      park[i].pop();
    }
  }
  return update;
}

int main() {
  while (scanf("%d%d", &m, &n), m | n) {
    ct = 0;
    out.clear();
    while (out.size() != n) {
      if (ct < 10 * n && ct % 10 == 0) {
        scanf("%d", &t);
        wait.push(P(t, ct / 10 + 1));
      }
      while (leave()) {
      }
      while (parking()) {
      }
      ct++;
    }

    rep(i, n) { printf("%d%c", out[i], i == n - 1 ? '\\n' : ' '); }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, s, e) for (int i = s; i <= e; i++)
#define repd(i, s, e) for (int i = s; i >= e; i--)

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

const int INF = 1000000009;
const int MAX_M = 11;
int m, n, t;
int ct, lf;
stack<P> park[MAX_M];
queue<P> wait;

bool parking() {
  if (wait.empty())
    return false;

  P car = wait.front();
  // park
  rep(i, m) {
    if (park[i].empty()) {
      // printf("car %d parks at %d\\n", car.second, i);
      park[i].push(P(car.first + ct, car.second));
      wait.pop();
      return true;
    }
  }

  // situation 1
  int p = -1;
  int minval = INF;
  rep(i, m) {
    if (park[i].size() == 1 && park[i].top().first >= ct + car.first) {
      if (minval > park[i].top().first - (ct + car.first)) {
        minval = park[i].top().first - (ct + car.first);
        p = i;
      }
    }
  }
  if (p >= 0) {
    // printf("car %d parks at %d\\n", car.second, p);
    park[p].push(P(ct + car.first, car.second));
    wait.pop();
    return true;
  }

  // situation 2
  rep(i, m) {
    if (park[i].size() == 1 && park[i].top().first <= ct + car.first) {
      if (minval > ct + car.first - park[i].top().first) {
        minval = ct + car.first - park[i].top().first;
        p = i;
      }
    }
  }
  if (p >= 0) {
    // printf("car %d parks at %d\\n", car.second, p);
    park[p].push(P(ct + car.first, car.second));
    wait.pop();
    return true;
  }
  return false;
}

bool leave() {
  int minval = INF;
  rep(i, m) {
    if (!park[i].empty() && minval > park[i].top().first) {
      minval = park[i].top().first;
    }
  }

  if (minval > ct)
    return false;

  bool update = false;
  rep(i, m) {
    if (!park[i].empty() && park[i].top().first == minval) {
      // printf("car %d leaves from %d\\n", park[i].top().second, i);
      printf("%d ", park[i].top().second);
      park[i].pop();
      lf++;
      update = true;
    }

    if (!park[i].empty() && park[i].top().first <= minval) {
      // printf("car %d leaves from %d\\n", park[i].top().second, i);
      printf("%d ", park[i].top().second);
      park[i].pop();
      lf++;
    }
  }
  return update;
}

int main() {
  while (scanf("%d%d", &m, &n), m | n) {
    ct = 0;
    lf = 0;
    rep(i, n) {
      scanf("%d", &t);
      wait.push(P(t, i + 1));
      while (leave()) {
      }
      while (parking()) {
      }
      ct += 10;
    }
    while (lf != n) {
      while (leave()) {
      }
      ct += 10;
    }
    printf("\\n");
  }
}