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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 15000

struct Data {
  Data() { time = car_id = -1; }

  Data(int arg_time, int arg_car_id) {
    time = arg_time;
    car_id = arg_car_id;
  }
  int time, car_id;
};

int M, N;
int out_time[100], length[100], park[10][2];

bool is_out[15000];

void func() {

  queue<Data> IN_Q;

  for (int i = 0; i < N; i++)
    is_out[i] = false;

  for (int i = 0; i < N; i++) {
    scanf("%d", &length[i]);
    IN_Q.push(Data(10 * i, i));
  }

  for (int i = 0; i < M; i++) {
    park[i][0] = -1;
    park[i][1] = -1;
  }

  int now_time = 0, out_num = 0, minimum, min_index;
  vector<int> ANS;

  bool is_FULL = false, FLG;
  Data current;

  int tmp;

  while (true) {

    if (is_out[now_time]) {
      for (int i = 0; i < M; i++) {
        if (park[i][0] != -1) {
          if (out_time[park[i][0]] <= now_time) {
            ANS.push_back(park[i][0]);
            out_num++;
            park[i][0] = -1;
            is_FULL = false;
            if (park[i][1] != -1) {
              if (out_time[park[i][1]] <= now_time) {
                ANS.push_back(park[i][1]);
                out_num++;
              } else {
                park[i][0] = park[i][1];
              }
              park[i][1] = -1;
            }
          }
        }
      }
    }

    if (out_num == N)
      break;

    while (IN_Q.size() > 0 && IN_Q.front().time <= now_time &&
           is_FULL == false) {

      current = IN_Q.front();

      FLG = false;

      for (int i = 0; i < M; i++) {
        if (park[i][0] == -1) {
          park[i][0] = current.car_id;
          out_time[current.car_id] = now_time + length[current.car_id];
          is_out[out_time[current.car_id]] = true;
          IN_Q.pop();
          FLG = true;
          break;
        }
      }
      if (FLG)
        continue;

      minimum = BIG_NUM;
      for (int i = 0; i < M; i++) {
        if (park[i][1] != -1)
          continue;
        if (out_time[park[i][0]] >= now_time + length[current.car_id]) {
          tmp = out_time[park[i][0]] - (now_time + length[current.car_id]);
          if (tmp < minimum) {
            minimum = tmp;
            min_index = i;
          }
        }
      }

      if (minimum != BIG_NUM) {
        park[min_index][1] = park[min_index][0];
        park[min_index][0] = current.car_id;
        out_time[current.car_id] = now_time + length[current.car_id];
        is_out[out_time[current.car_id]] = true;
        IN_Q.pop();
        continue;
      }

      for (int i = 0; i < M; i++) {
        if (park[i][1] != -1)
          continue;
        if (out_time[park[i][0]] < now_time + length[current.car_id]) {
          tmp = now_time + length[current.car_id] - out_time[park[i][0]];
          if (tmp < minimum) {
            minimum = tmp;
            min_index = i;
          }
        }
      }

      if (minimum != BIG_NUM) {
        park[min_index][1] = park[min_index][0];
        park[min_index][0] = current.car_id;
        out_time[current.car_id] = now_time + length[current.car_id];
        is_out[out_time[current.car_id]] = true;
        IN_Q.pop();
        continue;
      }

      is_FULL = true;
    }
    now_time++;
  }

  printf("%d", ANS[0] + 1);
  for (int i = 1; i < ANS.size(); i++) {
    printf(" %d", ANS[i] + 1);
  }
  printf("\\n");
}

int main() {

  while (true) {
    scanf("%d %d", &M, &N);
    if (M == 0 && N == 0)
      break;

    func();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Data {
  Data() { time = car_id = -1; }

  Data(int arg_time, int arg_car_id) {
    time = arg_time;
    car_id = arg_car_id;
  }
  int time, car_id;
};

int M, N;
int out_time[100], length[100], park[10][2];

void func() {

  queue<Data> IN_Q;

  for (int i = 0; i < N; i++) {
    scanf("%d", &length[i]);
    IN_Q.push(Data(10 * i, i));
  }

  for (int i = 0; i < M; i++) {
    park[i][0] = -1;
    park[i][1] = -1;
  }

  int now_time = 0, out_num = 0, minimum, min_index;
  vector<int> ANS;

  bool is_FULL = false, FLG;
  Data current;

  int tmp;

  while (true) {

    for (int i = 0; i < M; i++) {
      if (park[i][0] != -1) {
        if (out_time[park[i][0]] <= now_time) {
          ANS.push_back(park[i][0]);
          out_num++;
          park[i][0] = -1;
          if (park[i][1] != -1) {
            if (out_time[park[i][1]] <= now_time) {
              ANS.push_back(park[i][1]);
              out_num++;
            } else {
              park[i][0] = park[i][1];
            }
            park[i][1] = -1;
          }
        }
      }
    }

    if (out_num == N)
      break;

    while (IN_Q.size() > 0 && IN_Q.front().time <= now_time &&
           is_FULL == false) {

      current = IN_Q.front();

      FLG = false;

      for (int i = 0; i < M; i++) {
        if (park[i][0] == -1) {
          park[i][0] = current.car_id;
          out_time[current.car_id] = now_time + length[current.car_id];
          IN_Q.pop();
          FLG = true;
          break;
        }
      }
      if (FLG)
        continue;

      minimum = BIG_NUM;
      for (int i = 0; i < M; i++) {
        if (park[i][1] != -1)
          continue;
        if (out_time[park[i][0]] >= now_time + length[current.car_id]) {
          tmp = out_time[park[i][0]] - (now_time + length[current.car_id]);
          if (tmp < minimum) {
            minimum = tmp;
            min_index = i;
          }
        }
      }

      if (minimum != BIG_NUM) {
        park[min_index][1] = park[min_index][0];
        park[min_index][0] = current.car_id;
        out_time[current.car_id] = now_time + length[current.car_id];
        IN_Q.pop();
        continue;
      }

      for (int i = 0; i < M; i++) {
        if (park[i][1] != -1)
          continue;
        if (out_time[park[i][0]] < now_time + length[current.car_id]) {
          tmp = now_time + length[current.car_id] - out_time[park[i][0]];
          if (tmp < minimum) {
            minimum = tmp;
            min_index = i;
          }
        }
      }

      if (minimum != BIG_NUM) {
        park[min_index][1] = park[min_index][0];
        park[min_index][0] = current.car_id;
        out_time[current.car_id] = now_time + length[current.car_id];
        IN_Q.pop();
        continue;
      }

      is_FULL = true;
    }
    now_time++;
  }

  printf("%d", ANS[0] + 1);
  for (int i = 1; i < ANS.size(); i++) {
    printf(" %d", ANS[i] + 1);
  }
  printf("\\n");
}

int main() {

  while (true) {
    scanf("%d %d", &M, &N);
    if (M == 0 && N == 0)
      break;

    func();
  }
  return 0;
}