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 <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(a) a.begin(), a.end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a)                                                                \\
  rep(_X, sz(a)) { printf("%d ", a[_X]); }                                     \\
  puts("");
#define DBG2(a)                                                                \\
  rep(_X, sz(a)) {                                                             \\
    rep(_Y, sz(a[_X])) printf("%d ", a[_X][_Y]);                               \\
    puts("");                                                                  \\
  }
#define bitcount(b) __builtin_popcount(b)
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

#define delete(a, n) a.erase(remove(all(a), n), a.end())
template <typename T, typename S> vector<T> &operator<<(vector<T> &a, S b) {
  a.push_back(b);
  return a;
}
template <typename T> void operator>>(vector<T> &a, int b) {
  while (b--)
    if (!a.empty())
      a.pop_back();
}
bool isprime(int n) {
  if (n < 2)
    return false;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
ll b_pow(ll x, ll n) { return n ? b_pow(x * x, n / 2) * (n % 2 ? x : 1) : 1ll; }
string itos(int n) {
  stringstream ss;
  ss << n;
  return ss.str();
}

int name[10][2];
int rem[10][2];
int m, n;
deque<pair<int, int>> wait;

int putCar(int x, int t) {
  for (int i = 0; i < m; i++) {
    if (name[i][0] == 0) {
      rem[i][0] = t;
      name[i][0] = x;
      return 1;
    }
  }
  int flag = false, diff = 1e9;
  for (int i = 0; i < m; i++) {
    if (name[i][1] == 0 && rem[i][0] >= t) {
      diff = min(abs(rem[i][0] - t), diff);
      flag = true;
    }
  }
  if (flag) {
    for (int i = 0; i < m; i++) {
      if (name[i][1] == 0 && diff == abs(rem[i][0] - t)) {
        rem[i][1] = rem[i][0];
        name[i][1] = name[i][0];
        rem[i][0] = t;
        name[i][0] = x;
        return 1;
      }
    }
  } else {
    int flag = false, diff = 1e9;
    for (int i = 0; i < m; i++) {
      if (name[i][1] == 0 && rem[i][0] < t) {
        diff = min(abs(rem[i][0] - t), diff);
        flag = true;
      }
    }
    if (flag) {
      for (int i = 0; i < m; i++) {
        if (name[i][1] == 0 && diff == abs(rem[i][0] - t)) {
          rem[i][1] = rem[i][0];
          name[i][1] = name[i][0];
          rem[i][0] = t;
          name[i][0] = x;
          return 1;
        }
      }
    } else {
      return 0;
    }
  }
  return 0;
}
vector<int> res;
int step() {
  while (wait.size() && putCar(wait[0].first, wait[0].second))
    wait.pop_front();

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < 2; j++) {
      if (rem[i][j] >= 1) {
        rem[i][j]--;
      }

      if (rem[i][j] == 0 && name[i][j]) {
        if (!(j == 1 && rem[i][0])) {
          // cout << name[i][j] << "<<<" << endl;
          res.push_back(name[i][j]);
          rem[i][j] = 0;
          name[i][j] = 0;
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    if (name[i][0] == 0 && name[i][1]) {
      rem[i][0] = rem[i][1];
      name[i][0] = name[i][1];
      rem[i][1] = 0;
      name[i][1] = 0;
    }
  }
}

int main() {
  while (cin >> m >> n && (m || n)) {
    res.clear();
    wait.clear();

    for (int i = 0; i < 10; i++)
      for (int j = 0; j < 2; j++)
        rem[i][j] = 0;
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      wait.push_back(make_pair(i + 1, a));

      for (int j = 0; j < 10; j++)
        step();
      /*for(int k = 0 ; k < 2 ; k++){
              for(int j = 0 ; j < m ; j++)
                      cout << rem[j][k] << "(" << name[j][k]<<") ";
              cout << endl;
      }
      cout << "Q:";
      for(int j = 0 ; j < wait.size() ; j++) cout << wait[j].first << " ";cout
      << endl; cout << endl;
      */
    }
    while (res.size() != n)
      step();

    for (int i = 0; i < res.size(); i++)
      cout << (i ? " " : "") << res[i];
    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 <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(a) a.begin(), a.end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a)                                                                \\
  rep(_X, sz(a)) { printf("%d ", a[_X]); }                                     \\
  puts("");
#define DBG2(a)                                                                \\
  rep(_X, sz(a)) {                                                             \\
    rep(_Y, sz(a[_X])) printf("%d ", a[_X][_Y]);                               \\
    puts("");                                                                  \\
  }
#define bitcount(b) __builtin_popcount(b)
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

#define delete(a, n) a.erase(remove(all(a), n), a.end())
template <typename T, typename S> vector<T> &operator<<(vector<T> &a, S b) {
  a.push_back(b);
  return a;
}
template <typename T> void operator>>(vector<T> &a, int b) {
  while (b--)
    if (!a.empty())
      a.pop_back();
}
bool isprime(int n) {
  if (n < 2)
    return false;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
ll b_pow(ll x, ll n) { return n ? b_pow(x * x, n / 2) * (n % 2 ? x : 1) : 1ll; }
string itos(int n) {
  stringstream ss;
  ss << n;
  return ss.str();
}

int name[10][2];
int rem[10][2];
int m, n;
deque<pair<int, int>> wait;

int putCar(int x, int t) {
  for (int i = 0; i < m; i++) {
    if (name[i][0] == 0) {
      rem[i][0] = t;
      name[i][0] = x;
      return 1;
    }
  }
  int flag = false, diff = 1e9;
  for (int i = 0; i < m; i++) {
    if (name[i][1] == 0 && rem[i][0] >= t) {
      diff = min(abs(rem[i][0] - t), diff);
      flag = true;
    }
  }
  if (flag) {
    for (int i = 0; i < m; i++) {
      if (diff == abs(rem[i][0] - t)) {
        rem[i][1] = rem[i][0];
        name[i][1] = name[i][0];
        rem[i][0] = t;
        name[i][0] = x;
        return 1;
      }
    }
  } else {
    int flag = false, diff = 1e9;
    for (int i = 0; i < m; i++) {
      if (name[i][1] == 0 && rem[i][0] < t) {
        diff = min(abs(rem[i][0] - t), diff);
        flag = true;
      }
    }
    if (flag) {
      for (int i = 0; i < m; i++) {
        if (diff == abs(rem[i][0] - t)) {
          rem[i][1] = rem[i][0];
          name[i][1] = name[i][0];
          rem[i][0] = t;
          name[i][0] = x;
          return 1;
        }
      }
    } else {
      return 0;
    }
  }
  return 0;
}
vector<int> res;
int step() {
  while (wait.size() && putCar(wait[0].first, wait[0].second))
    wait.pop_front();

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < 2; j++) {
      if (rem[i][j] >= 1) {
        rem[i][j]--;
      }

      if (rem[i][j] == 0 && name[i][j]) {
        if (!(j == 1 && rem[i][0])) {
          // cout << name[i][j] << "<<<" << endl;
          res.push_back(name[i][j]);
          rem[i][j] = 0;
          name[i][j] = 0;
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    if (rem[i][0] == 0 && rem[i][1]) {
      rem[i][0] = rem[i][1];
      name[i][0] = name[i][1];
      rem[i][1] = 0;
      name[i][1] = 0;
    }
  }
}

int main() {
  while (cin >> m >> n && (m || n)) {
    res.clear();
    wait.clear();

    for (int i = 0; i < 10; i++)
      for (int j = 0; j < 2; j++)
        rem[i][j] = 0;
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      wait.push_back(make_pair(i + 1, a));

      for (int j = 0; j < 10; j++)
        step();
      /*for(int k = 0 ; k < 2 ; k++){
              for(int j = 0 ; j < m ; j++)
                      cout << rem[j][k] << "(" << name[j][k]<<") ";
              cout << endl;
      }
      cout << "Q:";
      for(int j = 0 ; j < wait.size() ; j++) cout << wait[j].first << " ";cout
      << endl; cout << endl;
      */
    }
    while (res.size() != n)
      step();

    for (int i = 0; i < res.size(); i++)
      cout << (i ? " " : "") << res[i];
    cout << endl;
    /*cout << "QQQQQ:";
            for(int j = 0 ; j < wait.size() ; j++) cout << wait[j].first << "
       ";cout << endl; cout << endl;
    */
  }
}