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

#include <iostream>
using namespace std;

class Process {
private:
  int time;

public:
  string name;

  Process() : time(0), name("") {}

  Process(const string &str, const int &num) : time(num), name(str) {}

  int excute(const int &quantum) {
    if (quantum < time) {
      time -= quantum;
      return quantum;
    } else {
      int temp = time;
      time = 0;
      return temp;
    }
  }

  bool is_complete() { return time == 0; }
};

class PsQueue {
private:
  Process data[100000];
  int head;
  int tail;

public:
  PsQueue() : head(0), tail(0) {}

  void enqueue(const Process &process) {
    data[tail] = process;
    if (tail == (100000 - 1)) {
      tail = 0;
    } else {
      ++tail;
    }
  }

  Process dequeue() {
    Process ps_head = data[head];
    if (head == (100000 - 1)) {
      head = 0;
    } else {
      ++head;
    }

    return ps_head;
  }

  bool is_empty() { return head == tail; }
};

int main() {
  int n = 0;
  int quantum = 0;
  cin >> n >> quantum;

  string str = "";
  int num = 0;
  Process a_process;
  PsQueue ps_queue;
  for (int i = 0; i < n; ++i) {
    cin >> str >> num;
    a_process = Process(str, num);
    ps_queue.enqueue(a_process);
  }

  int total = 0;
  Process cur_process;
  while (true) {
    // All processes complete
    if (ps_queue.is_empty())
      break;

    // Excute the head process and add excution time
    cur_process = ps_queue.dequeue();
    total += cur_process.excute(quantum);

    // If the process is complete, then output a line
    if (cur_process.is_complete()) {
      cout << cur_process.name << ' ' << total << endl;
    }
    // If the process is NOT complete, then add to queue again
    else {
      ps_queue.enqueue(cur_process);
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

class Process {
private:
  int time;

public:
  string name;

  Process() : time(0), name("") {}

  Process(const string &str, const int &num) : time(num), name(str) {}

  int excute(const int &quantum) {
    if (quantum < time) {
      time -= quantum;
      return quantum;
    } else {
      int temp = time;
      time = 0;
      return temp;
    }
  }

  bool is_complete() { return time == 0; }
};

class PsProcess {
private:
  Process data[100000];
  int count;

public:
  PsProcess() : count(0) {}

  void enqueue(const Process &process) {
    data[count] = process;
    ++count;
  }

  Process dequeue() {
    Process head = data[0];
    --count;

    // Shift elements one by one
    if (count > 0) {
      for (int i = 1; i <= count; ++i) {
        data[i - 1] = data[i];
      }
    }

    return head;
  }

  bool is_empty() { return count == 0; }
};

int main() {
  int n = 0;
  int quantum = 0;
  cin >> n >> quantum;

  string str = "";
  int num = 0;
  Process a_process;
  PsProcess ps_process;
  for (int i = 0; i < n; ++i) {
    cin >> str >> num;
    a_process = Process(str, num);
    ps_process.enqueue(a_process);
  }

  int total = 0;
  Process cur_process;
  while (true) {
    // All processes complete
    if (ps_process.is_empty())
      break;

    // Excute the head process and add excution time
    cur_process = ps_process.dequeue();
    total += cur_process.excute(quantum);

    // If the process is complete, then output a line
    if (cur_process.is_complete()) {
      cout << cur_process.name << ' ' << total << endl;
    }
    // If the process is NOT complete, then add to queue again
    else {
      ps_process.enqueue(cur_process);
    }
  }

  return 0;
}