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

#include <iostream>
#include <vector>
using namespace std;
class queue {
public:
  queue(const int n, const int q);
  void enqueue(const pair<int, char *> &data);
  void dequeue();
  void operation();
  int calculate(int time);

private:
  vector<pair<int, char *>> mData;
  int mOperationTime;
  int mHead;
  int mTail;
  int mBufferSize;
  int mDone;
  int mTimeFromStart;
};

queue::queue(const int n, const int q) {
  mHead = 0;
  mTail = 0;
  mData.reserve(n);
  mBufferSize = n;
  mDone = 0;
  mTimeFromStart = 0;
  mOperationTime = q;
}

void queue::enqueue(const pair<int, char *> &data) {
  mData.push_back(data);
  mTail++;
  if (mTail == mBufferSize)
    mTail = 0;
};

void queue::dequeue() {
  // cout << mData[mHead].second << " " << mTimeFromStart << "\\n";
  printf("%s %d\\n", mData[mHead].second, mTimeFromStart);
  mData.erase(mData.begin() + mHead);
  mHead--;
  mDone++;
};

int queue::calculate(int time) {
  // if(mData[mHead].first != 0) {
  if (mData[mHead].first - time <= 0) {
    mTimeFromStart += mData[mHead].first;
    mData[mHead].first = 0;
    dequeue();
    return 0;
  } else {
    mData[mHead].first -= time;
    mTimeFromStart += time;
    return 0;
  }
  //}
  // else {
  //    return time;
  //}
};

void queue::operation() {
  int operation_time = mOperationTime;
  while (1) {
    operation_time = calculate(operation_time);
    if (operation_time == 0) {
      operation_time = mOperationTime;
    }
    mHead++;
    if (mHead == mData.size()) {
      mHead = 0;
    }
    if (mDone == mBufferSize) {
      break;
    }
  }
  return;
}

int main(int argc, char *argv[]) {
  int n, q;
  scanf("%d", &n);
  scanf("%d", &q);
  queue Q(n, q);
  char name[n][10];
  for (int i = 0; i < n; i++) {
    int time;
    scanf("%s", name[i]);
    scanf("%d", &time);
    pair<int, char *> p{time, name[i]};
    Q.enqueue(p);
  }

  Q.operation();

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class queue {
public:
  queue(const int n, const int q);
  void enqueue(const pair<int, string> &data);
  void dequeue();
  void operation();
  int calculate(int time);

private:
  vector<pair<int, string>> mData;
  int mOperationTime;
  int mHead;
  int mTail;
  int mBufferSize;
  int mDone;
  int mTimeFromStart;
};

queue::queue(const int n, const int q) {
  mHead = 0;
  mTail = 0;
  mData.reserve(n);
  mBufferSize = n;
  mDone = 0;
  mTimeFromStart = 0;
  mOperationTime = q;
}

void queue::enqueue(const pair<int, string> &data) {
  mData.push_back(data);
  mTail++;
  if (mTail == mBufferSize)
    mTail = 0;
  //????????????????????????????????????????????????????¶??????????????????????
};

void queue::dequeue() {
  cout << mData[mHead].second << " " << mTimeFromStart << "\\n";
  mData.erase(mData.begin() + mHead);
  mHead--;
  mDone++;
};

int queue::calculate(int time) {
  // if(mData[mHead].first != 0) {
  if (mData[mHead].first - time <= 0) {
    mTimeFromStart += mData[mHead].first;
    mData[mHead].first = 0;
    dequeue();
    return 0;
  } else {
    mData[mHead].first -= time;
    mTimeFromStart += time;
    return 0;
  }
  //}
  // else {
  //    return time;
  //}
};

void queue::operation() {
  int operation_time = mOperationTime;
  while (1) {
    operation_time = calculate(operation_time);
    if (operation_time == 0) {
      operation_time = mOperationTime;
    }
    mHead++;
    if (mHead == mData.size()) {
      mHead = 0;
    }
    if (mDone == mBufferSize) {
      break;
    }
  }
  return;
}

int main(int argc, char *argv[]) {
  int n, q, time;
  string name;
  cin >> n >> q;
  queue Q(n, q);
  for (int i = 0; i < n; i++) {
    cin >> name >> time;
    pair<int, string> p{time, name};
    Q.enqueue(p);
  }

  Q.operation();

  return 0;
}