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

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int M, N;
int T[100];
pii data[10][2]; // ????????????, ??´?????????
pii ud = pii(INF, 0);

int main() {
  while (cin >> M >> N, M || N) {
    REP(i, N) scanf("%d", T + i);

    fill(data[0], data[M], ud);

    queue<pii> in; // ???????????? time, number
    REP(i, N) in.push(pii(i * 10, i + 1));

    priority_queue<pip, vector<pip>, greater<pip>>
        out; // ????????????, ?§??????????, ???
    vector<int> ans;

    int t = 0;
    while (true) {

      while (!out.empty()) {
        int nt = out.top().fi;
        int snum = out.top().se.fi;
        int g = out.top().se.se;

        bool ok = false;

        if (nt <= t) {

          if (g == 1 && data[snum - 1][0] != ud) {
            out.pop();
            data[snum - 1][1].fi = data[snum - 1][0].fi;
            out.push(pip(data[snum - 1][0].fi, pii(snum, 1)));
          } else {
            ans.push_back(data[snum - 1][g].se);
            data[snum - 1][g] = ud;
            out.pop();
          }
        } else {
          break;
        }
      }

      while (!in.empty()) {
        int nt = in.front().fi;
        int num = in.front().se;

        if (nt <= t) {
          // ??¢????????????
          bool stop = false;

          // 1
          REP(i, M) {
            if (data[i][0] == pii(INF, 0) && data[i][1] == ud) {
              data[i][1] = pii(t + T[num - 1], num);
              out.push(pip(t + T[num - 1], pii(i + 1, 1)));
              stop = true;
              break;
            }
          }
          if (stop) {
            in.pop();
            continue;
          }
          /////

          // 2,3
          pii mi1 = ud, mi2 = ud;
          REP(i, M) if (data[i][1] != ud && data[i][0] == ud) {
            int gotime = data[i][1].fi - t;
            if (gotime >= T[num - 1])
              chmin(mi1, pii(abs(gotime - T[num - 1]), i + 1));
            else
              chmin(mi2, pii(abs(gotime - T[num - 1]), i + 1));
          }

          if (mi1 != ud) {
            data[mi1.se - 1][0] = pii(t + T[num - 1], num);
            out.push(pip(t + T[num - 1], pii(mi1.se, 0)));
            stop = true;
          } else if (mi2 != ud) {
            data[mi2.se - 1][0] = pii(t + T[num - 1], num);
            out.push(pip(t + T[num - 1], pii(mi2.se, 0)));
            stop = true;
          }

          if (stop) {
            in.pop();
            continue;
          }

          break;
        } else {
          break;
        }
      }

      int nextt = INF;
      if (!in.empty())
        chmin(nextt, in.front().fi);
      if (!out.empty())
        chmin(nextt, out.top().fi);
      if (nextt == INF)
        break;

      chmax(nextt, t + 1);
      t = nextt;
    }

    REP(i, ans.size()) printf("%d%c", ans[i], i == ans.size() - 1 ? '\\n' : ' ');
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int M, N;
int T[100];
pii data[10][2]; // ????????????, ??´?????????

int main() {
  while (cin >> M >> N, M || N) {
    REP(i, N) scanf("%d", T + i);

    fill(data[0], data[M], pii(INF, 0));

    queue<pii> in; // ???????????? time, number
    REP(i, N) in.push(pii(i * 10, i + 1));

    priority_queue<pii, vector<pii>, greater<pii>>
        out; // ????????????, ?§??????????(1???????????????)
    vector<int> ans;

    int t = 0;
    while (!in.empty() || !out.empty()) {

      while (!out.empty()) {
        int nt = out.top().fi;
        int snum = out.top().se;

        bool ok = false;

        if (nt <= t) {
          if (snum < 0) { // 1??????
            snum = -snum;

            ans.push_back(data[snum - 1][0].se);
            data[snum - 1][0] = pii(INF, 0);
            ok = true;
          } else { // 2??????
            if (data[snum - 1][0] == pii(INF, 0)) {
              ans.push_back(data[snum - 1][1].se);
              data[snum - 1][1] = pii(INF, 0);
              ok = true;
            } else {
              out.pop();
              out.push(pii(data[snum - 1][0].fi, snum));
            }
          }
        } else {
          break;
        }

        if (ok)
          out.pop();
      }

      while (!in.empty()) {
        int nt = in.front().fi;
        int num = in.front().se;

        if (nt <= t) {
          // ??¢????????????
          bool stop = false;

          // 1
          REP(i, M) {
            if (data[i][0] == pii(INF, 0) && data[i][1] == pii(INF, 0)) {
              data[i][1] = pii(t + T[num - 1], num);
              out.push(pii(t + T[num - 1], i + 1));
              stop = true;
              break;
            }
          }
          if (stop) {
            in.pop();
            continue;
          }
          /////

          // 2,3
          pii mi1(INF, 0), mi2(INF, 0);
          REP(i, M)
          if (data[i][1] != pii(INF, 0) && data[i][0] == pii(INF, 0)) {
            int gotime = data[i][1].fi - t;
            if (gotime >= T[num - 1])
              chmin(mi1, pii(abs(gotime - T[num - 1]), i + 1));
            else
              chmin(mi2, pii(abs(gotime - T[num - 1]), i + 1));
          }

          if (mi2 != pii(INF, 0)) {
            data[mi2.se - 1][0] = pii(t + T[num - 1], num);
            out.push(pii(t + T[num - 1], -mi2.se));
            stop = true;
          } else if (mi1 != pii(INF, 0)) {
            data[mi1.se - 1][0] = pii(t + T[num - 1], num);
            out.push(pii(t + T[num - 1], -mi1.se));
            stop = true;
          }

          if (stop) {
            in.pop();
            continue;
          }
        } else {
          break;
        }
      }

      int nextt = INF;
      if (!in.empty())
        chmin(nextt, in.front().fi);
      if (!out.empty())
        chmin(nextt, out.top().fi);
      t = nextt;
    }

    REP(i, ans.size()) printf("%d%c", ans[i], i == ans.size() - 1 ? '\\n' : ' ');
  }
  return 0;
}