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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

struct Event {
  int time;
  int kind; // 0:in,1:out
  int spaceNum;
  int carNum;
  bool operator<(const Event &e) const {
    if (this->time != e.time)
      return this->time > e.time;
    if (this->kind != e.kind)
      return this->kind < e.kind;
    return this->spaceNum > e.spaceNum;
  }
  Event() {}
  Event(int time_, int kind_, int spaceNum_, int carNum_) {
    time = time_;
    kind = kind_;
    spaceNum = spaceNum_;
    carNum = carNum_;
  }
};
typedef pair<int, int> pii;
int stopTimes[201];
// どこに上か下どちらに止まっているか
pii parKingInfo[201];
// 止まっている車の番号
stack<int> parkingCars[201];
// それぞれの車の出庫予定時刻
int LeftTimes[201];
int N, M;

void InCar(priority_queue<Event> &pq, Event &p, vector<int> &ans,
           queue<int> &waitCars) {
  // 一台も駐車が無い箇所を探索
  int pos = -1;
  for (int i = 0; i < M; i++) {
    if (parkingCars[i].size() == 0) {
      pos = i;
      parkingCars[i].push(p.carNum);
      parKingInfo[p.carNum].first = i;
      parKingInfo[p.carNum].second = 0;
      LeftTimes[p.carNum] = p.time + stopTimes[p.carNum];
      pq.push(Event(LeftTimes[p.carNum], 1, pos, p.carNum));
      break;
    }
  }
  if (pos == -1) {
    int diffTime = 1000000000;
    // 出庫時刻
    for (int i = 0; i < M; i++) {
      if (parkingCars[i].size() == 1) {
        int carkind = parkingCars[i].top();
        int diff = LeftTimes[carkind] - p.time;
        if (diff >= stopTimes[p.carNum]) {
          if (diffTime > diff - stopTimes[p.carNum]) {
            diffTime = diff - stopTimes[p.carNum];
            pos = i;
          }
        }
      }
    }
    if (pos != -1) {
      parKingInfo[parkingCars[pos].top()].second = 1;
      parkingCars[pos].push(p.carNum);
      parKingInfo[p.carNum].first = pos;
      parKingInfo[p.carNum].second = 0;
      LeftTimes[p.carNum] = p.time + stopTimes[p.carNum];
      pq.push(Event(LeftTimes[p.carNum], 1, pos, p.carNum));
    } else {
      for (int i = 0; i < M; i++) {
        if (parkingCars[i].size() == 1) {
          int carkind = parkingCars[i].top();
          int diff = LeftTimes[carkind] - p.time;
          if (diff < stopTimes[p.carNum]) {
            if (diffTime > -diff + stopTimes[p.carNum]) {
              diffTime = -diff + stopTimes[p.carNum];
              pos = i;
            }
          }
        }
      }
      if (pos != -1) {
        parKingInfo[parkingCars[pos].top()].second = 1;
        parkingCars[pos].push(p.carNum);
        parKingInfo[p.carNum].first = pos;
        parKingInfo[p.carNum].second = 0;
        LeftTimes[p.carNum] = p.time + stopTimes[p.carNum];
        pq.push(Event(LeftTimes[p.carNum], 1, pos, p.carNum));
      } else {
        waitCars.push(p.carNum);
      }
    }
  }
}

int main() {
  while (cin >> M >> N && (N | M)) {
    for (int i = 0; i < 201; i++)
      parKingInfo[i].first = parKingInfo[i].second = -1;
    vector<int> ans;
    for (int i = 0; i < N; i++)
      cin >> stopTimes[i];
    priority_queue<Event> pq;
    for (int i = 0; i < N; i++)
      pq.push(Event(i * 10, 0, 0, i));
    queue<int> waitCars;
    while (pq.size()) {
      Event p = pq.top();
      pq.pop();
      // cout<<p.time<<" "<<p.kind<<endl;
      if (p.kind == 0)
        InCar(pq, p, ans, waitCars);
      // 出発
      else {
        int empSpc = 0;
        if (parkingCars[p.spaceNum].size() == 1) {
          ans.push_back(p.carNum);
          parkingCars[p.spaceNum].pop();
          empSpc++;
        } else {
          if (parKingInfo[p.carNum].second == 0) {
            ans.push_back(p.carNum);
            parkingCars[p.spaceNum].pop();
            empSpc++;
            // もし上段に時間を過ぎている車があれば、それも取り出す
            if (LeftTimes[parkingCars[p.spaceNum].top()] <= p.time) {
              empSpc++;
              ans.push_back(parkingCars[p.spaceNum].top());
              parkingCars[p.spaceNum].pop();
            }
          }
          // 時間切れになっても上段の車なら、何もしない
          else {
          }
        }
        // 今回空いたスペースの分だけ車を入れる
        int sz = waitCars.size();
        for (int i = 0; i < min(empSpc, sz); i++) {
          int a = waitCars.front();
          waitCars.pop();
          p.carNum = a;
          InCar(pq, p, ans, waitCars);
        }
      }
    }
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i] + 1;
      if (i == ans.size() - 1)
        cout << endl;
      else
        cout << " ";
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

struct Event {
  int time;
  int kind; // 0:in,1:out
  int spaceNum;
  int carNum;
  bool operator<(const Event &e) const {
    if (this->time != e.time)
      return this->time > e.time;
    if (this->kind != e.kind)
      return this->kind < e.kind;
    return this->spaceNum > e.spaceNum;
  }
  Event() {}
  Event(int time_, int kind_, int spaceNum_, int carNum_) {
    time = time_;
    kind = kind_;
    spaceNum = spaceNum_;
    carNum = carNum_;
  }
};
typedef pair<int, int> pii;
int stopTimes[201];
// どこに上か下どちらに止まっているか
pii parKingInfo[201];
// 止まっている車の番号
stack<int> parkingCars[201];
// それぞれの車の出庫予定時刻
int LeftTimes[201];
int N, M;

void InCar(priority_queue<Event> &pq, Event &p, vector<int> &ans,
           queue<int> &waitCars) {
  // 一台も駐車が無い箇所を探索
  int pos = -1;
  for (int i = 0; i < M; i++) {
    if (parkingCars[i].size() == 0) {
      pos = i;
      parkingCars[i].push(p.carNum);
      parKingInfo[p.carNum].first = i;
      parKingInfo[p.carNum].second = 0;
      LeftTimes[p.carNum] = p.time + stopTimes[p.carNum];
      pq.push(Event(LeftTimes[p.carNum], 1, pos, p.carNum));
      break;
    }
  }
  if (pos == -1) {
    int diffTime = 1000000000;
    // 出庫時刻
    for (int i = 0; i < M; i++) {
      if (parkingCars[i].size() == 1) {
        int carkind = parkingCars[i].top();
        int diff = LeftTimes[carkind] - p.time;
        if (diff >= stopTimes[p.carNum]) {
          if (diffTime > diff - stopTimes[p.carNum]) {
            diffTime = diff - stopTimes[p.carNum];
            pos = i;
          }
        }
      }
    }
    if (pos != -1) {
      parKingInfo[parkingCars[pos].top()].second = 1;
      parkingCars[pos].push(p.carNum);
      parKingInfo[p.carNum].first = pos;
      parKingInfo[p.carNum].second = 0;
      LeftTimes[p.carNum] = p.time + stopTimes[p.carNum];
      pq.push(Event(LeftTimes[p.carNum], 1, pos, p.carNum));
    } else {
      for (int i = 0; i < M; i++) {
        if (parkingCars[i].size() == 1) {
          int carkind = parkingCars[i].top();
          int diff = LeftTimes[carkind] - p.time;
          if (diff < stopTimes[p.carNum]) {
            if (diffTime > -diff + stopTimes[p.carNum]) {
              diffTime = -diff + stopTimes[p.carNum];
              pos = i;
            }
          }
        }
      }
      if (pos != -1) {
        parKingInfo[parkingCars[pos].top()].second = 1;
        parkingCars[pos].push(p.carNum);
        parKingInfo[p.carNum].first = pos;
        parKingInfo[p.carNum].second = 0;
        LeftTimes[p.carNum] = p.time + stopTimes[p.carNum];
        pq.push(Event(LeftTimes[p.carNum], 1, pos, p.carNum));
      } else {
        waitCars.push(p.carNum);
      }
    }
  }
}

int main() {
  while (cin >> M >> N && (N | M)) {
    for (int i = 0; i < 201; i++)
      parKingInfo[i].first = parKingInfo[i].second = -1;
    vector<int> ans;
    for (int i = 0; i < N; i++)
      cin >> stopTimes[i];
    priority_queue<Event> pq;
    for (int i = 0; i < N; i++)
      pq.push(Event(i * 10, 0, 0, i));
    queue<int> waitCars;
    while (pq.size()) {
      Event p = pq.top();
      pq.pop();
      // cout<<p.time<<" "<<p.kind<<endl;
      if (p.kind == 0)
        InCar(pq, p, ans, waitCars);
      // 出発
      else {
        int empSpc = 0;
        if (parkingCars[p.spaceNum].size() == 1) {
          ans.push_back(p.carNum);
          parkingCars[p.spaceNum].pop();
          empSpc++;
        } else {
          if (parKingInfo[p.carNum].second == 0) {
            ans.push_back(p.carNum);
            parkingCars[p.spaceNum].pop();
            empSpc++;
            // もし上段に時間を過ぎている車があれば、それも取り出す
            if (LeftTimes[parkingCars[p.spaceNum].top()] <= p.time) {
              empSpc++;
              ans.push_back(parkingCars[p.spaceNum].top());
              parkingCars[p.spaceNum].pop();
            }
          }
          // 時間切れになっても上段の車なら、何もしない
          else {
          }
        }
        // 今回空いたスペースの分だけ車を入れる
        for (int i = 0; i < min(empSpc, (int)waitCars.size()); i++)
          InCar(pq, p, ans, waitCars);
      }
    }
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i] + 1;
      if (i == ans.size() - 1)
        cout << endl;
      else
        cout << " ";
    }
  }
  return 0;
}