prithivMLmods/Coder-Stat huggingface dataset:
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

struct Process {
  char name[10];
  int time;
};

static const int MAX = 10000;
Process P[MAX];
int tail, head;

void enqueue(struct Process &p) {
  P[tail] = p;
  tail = (tail + 1) % MAX;
}

void dequeue(struct Process &p) {
  p = P[head];
  head = (head + 1) % MAX;
}

int main() {
  int n, q;

  cin >> n >> q;

  for (int i = 0; i < n; i++) {
    cin >> P[i].name >> P[i].time;
  }

  int count = 0;

  tail = n;

  struct Process p;

  while (head != tail) {
    dequeue(p);
    p.time -= q;
    if (p.time <= 0) {
      count += q + p.time;
      cout << p.name << " " << count << endl;
    } else {
      count += q;
      enqueue(p);
    }
  }
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

struct Process {
  char name[10];
  int time;
};

static const int MAX = 100000;
Process P[MAX];
int tail, head;

void enqueue(struct Process &p) {
  P[tail] = p;
  tail = (tail + 1) % MAX;
}

void dequeue(struct Process &p) {
  p = P[head];
  head = (head + 1) % MAX;
}

int main() {
  int n, q;

  cin >> n >> q;

  for (int i = 0; i < n; i++) {
    cin >> P[i].name >> P[i].time;
  }

  int count = 0;

  tail = n;

  struct Process p;

  while (head != tail) {
    dequeue(p);
    p.time -= q;
    if (p.time <= 0) {
      count += q + p.time;
      cout << p.name << " " << count << endl;
    } else {
      count += q;
      enqueue(p);
    }
  }
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <string>

using namespace std;

#define max_datasets 1000000

int main() {
  queue<string> data1;
  queue<int> data2;

  string kari_data1;
  int kari_data2;

  int all_time = 0;

  int n, q;
  string name[max_datasets];
  int time[max_datasets];
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> name[i] >> time[i];
    data1.push(name[i]);
    data2.push(time[i]);
  }

  while (data1.size() != 0) {
    if (data2.front() <= q) {
      all_time += data2.front();
      cout << data1.front() << " " << all_time << endl;
      data1.pop();
      data2.pop();
    } else {
      data2.front() -= q;
      all_time += q;
      kari_data1 = data1.front();
      kari_data2 = data2.front();
      data1.pop();
      data2.pop();
      data1.push(kari_data1);
      data2.push(kari_data2);
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <string>

using namespace std;

#define max_datasets 500000

int main() {
  queue<string> data1;
  queue<int> data2;

  string kari_data1;
  int kari_data2;

  int all_time = 0;

  int n, q;
  string name[max_datasets];
  int time[max_datasets];
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> name[i] >> time[i];
    data1.push(name[i]);
    data2.push(time[i]);
  }

  while (data1.size() != 0) {
    if (data2.front() <= q) {
      all_time += data2.front();
      cout << data1.front() << " " << all_time << endl;
      data1.pop();
      data2.pop();
    } else {
      data2.front() -= q;
      all_time += q;
      kari_data1 = data1.front();
      kari_data2 = data2.front();
      data1.pop();
      data2.pop();
      data1.push(kari_data1);
      data2.push(kari_data2);
    }
  }
  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int que[100000];
int head = 0;
int tail = 0;
void enq(int a) {
  que[tail] = a;
  tail = (tail + 1) % 100000;
}

int deq() { return que[head++]; }

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

int main() {
  int n, q;
  char name[100000][11];
  int time[100000];
  int clock = 0;

  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++) {
    scanf("%s%d", name[i], &time[i]);
    enq(i);
  }

  while (!empty()) {
    int i = deq();
    if (time[i] <= q) {
      clock += time[i];
      time[i] = 0;
      printf("%s %d\n", name[i], clock);
    } else {
      clock += q;
      time[i] -= q;
      enq(i);
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int que[100000];
int head = 0;
int tail = 0;
void enq(int a) {
  que[tail] = a;
  tail = (tail + 1) % 100000;
}

int deq() {
  int h = head;
  head = (head + 1) % 100000;
  return que[h];
}

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

int main() {
  int n, q;
  char name[100000][11];
  int time[100000];
  int clock = 0;

  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++) {
    scanf("%s%d", name[i], &time[i]);
    enq(i);
  }

  while (!empty()) {
    int i = deq();
    if (time[i] <= q) {
      clock += time[i];
      time[i] = 0;
      printf("%s %d\n", name[i], clock);
    } else {
      clock += q;
      time[i] -= q;
      enq(i);
    }
  }
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <string>

#define N 10000
using namespace std;

class Queue {
private:
  int num[N];
  int head;
  int tail;

public:
  Queue() {
    head = 0;
    tail = 0;
  }

  // add to head position
  void push(int x) {
    num[tail] = x;
    tail++;
  }
  // delete head position
  void pop(void) { head++; }
  // return now of stck
  int front(void) { return num[head]; }
};

int main() {

  Queue que;
  //
  bool finish[N];
  int n, q, tim = 0;
  string name[N];
  int time[N], i = 0;
  int passtime = 0;

  // load n and q
  cin >> n >> q;

  // load name and time
  for (i = 0; i < n; i++) {
    cin >> name[i] >> tim;
    que.push(tim);
    time[i] = 0;
  }

  // format finish[]
  for (int k = 0; k < n; k++)
    finish[k] = false;

  while (1) {
    int s;
    bool end = true;
    for (int k = 0; k < n; k++) {
      if (!finish[k]) {
        end = false;
        break;
      }
    }
    if (end)
      break;
    else
      end = true;

    if (i == n)
      i = 0;

    if (finish[i]) {
      i++;
      continue;
    }

    s = que.front();
    if (s - q > 0) {
      que.push(s - q);
      que.pop();
      passtime += q;
      time[i] = passtime;

    } else {
      que.pop();
      passtime += s;
      time[i] = passtime;
      finish[i] = true;
      cout << name[i] << " " << time[i] << endl;
    }
    i++;
  }

  return 0;
}

Fixed submission:
#include <cstdlib>
#include <iostream>
#include <string>

#define N 100000
using namespace std;

class Queue {
private:
  int num[N];
  int head;
  int tail;

public:
  Queue() {
    head = 0;
    tail = 0;
  }

  // add to head position
  void push(int x) {
    num[tail] = x;
    tail++;
  }
  // delete head position
  void pop(void) { head++; }
  // return now of stck
  int front(void) { return num[head]; }
};

int main() {

  Queue que;
  //
  bool finish[N];
  int n, q, tim = 0;
  string name[N];
  int time[N], i = 0;
  int passtime = 0;

  // load n and q
  cin >> n >> q;

  // load name and time
  for (i = 0; i < n; i++) {
    cin >> name[i] >> tim;
    que.push(tim);
    time[i] = 0;
  }

  // format finish[]
  for (int k = 0; k < n; k++)
    finish[k] = false;

  while (1) {
    int s;
    bool end = true;
    for (int k = 0; k < n; k++) {
      if (!finish[k]) {
        end = false;
        break;
      }
    }
    if (end)
      break;
    else
      end = true;

    if (i == n)
      i = 0;

    if (finish[i]) {
      i++;
      continue;
    }

    s = que.front();
    if (s - q > 0) {
      que.push(s - q);
      que.pop();
      passtime += q;
      time[i] = passtime;

    } else {
      que.pop();
      passtime += s;
      time[i] = passtime;
      finish[i] = true;
      cout << name[i] << " " << time[i] << endl;
    }
    i++;
  }

  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int n, q, head = -1, tail, time_queue[100001] = {}, now = 0;
  char name_queue[100001][15];
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> name_queue[i] >> time_queue[i];
  tail = (n - 1) % 100001;
  while (tail != head) {
    head++;
    if (time_queue[head] <= q) {
      now += time_queue[head];
      cout << name_queue[head] << " " << now << endl;
    } else {
      now += q;
      time_queue[(tail + 1) % 100001] = time_queue[head] - q;
      strcpy(name_queue[(tail + 1) % 100001], name_queue[head]);
      tail = (tail + 1) % 100001;
    }
  }

  return 0;
}


Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int n, q, head = -1, tail, time_queue[100001] = {}, now = 0;
  char name_queue[100001][15];
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> name_queue[i] >> time_queue[i];
  tail = (n - 1) % 100001;
  while (tail != head) {
    head = (head + 1) % 100001;
    if (time_queue[head] <= q) {
      now += time_queue[head];
      cout << name_queue[head] << " " << now << endl;
    } else {
      now += q;
      time_queue[(tail + 1) % 100001] = time_queue[head] - q;
      strcpy(name_queue[(tail + 1) % 100001], name_queue[head]);
      tail = (tail + 1) % 100001;
    }
  }

  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int Q[100000];
int f = 0, e = 0, f2 = 0, e2 = 0;
int Q2[100000];

void input_time(int a) {
  Q[e] = a;
  e = (e + 1) % 100000;
}

int output_time() {
  int a = Q[f];
  f = (f + 1) % 100000;
  return a;
}

void input_name(int a) {
  Q2[e2] = a;
  e2 = (e2 + 1) % 100000;
}

int output_name() {
  int a = Q2[f2];
  f2 = (f2 + 1) % 100000;
  return a;
}

char name[15][100000];

int main() {
  int n, q;
  int time, count;

  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> name[i] >> time;
    input_time(time);
    input_name(i);
  }
  count = 0;
  while (1) {
    if ((time = output_time()) > q) {
      input_time(time - q);
      input_name(output_name());
      count += q;
    } else {
      count += time;

      cout << name[output_name()] << ' ' << count << endl;
    }
    if (e == f) {
      break;
    }
  }
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int Q[100000];
int f = 0, e = 0, f2 = 0, e2 = 0;
int Q2[100000];

void input_time(int a) {
  Q[e] = a;
  e = (e + 1) % 100000;
}

int output_time() {
  int a = Q[f];
  f = (f + 1) % 100000;
  return a;
}

void input_name(int a) {
  Q2[e2] = a;
  e2 = (e2 + 1) % 100000;
}

int output_name() {
  int a = Q2[f2];
  f2 = (f2 + 1) % 100000;
  return a;
}

char name[100000][15];

int main() {
  int n, q;
  int time, count;

  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> name[i] >> time;
    input_time(time);
    input_name(i);
  }
  count = 0;
  while (1) {
    if ((time = output_time()) > q) {
      input_time(time - q);
      input_name(output_name());
      count += q;
    } else {
      count += time;

      cout << name[output_name()] << ' ' << count << endl;
    }
    if (e == f) {
      break;
    }
  }
  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

#define MAX 100005

using namespace std;

struct process {
  int time;
  string name;
};

class my_queue {
private:
  int head, tail;
  process queue[100000];

public:
  my_queue() {
    head = 0;
    tail = 0;
  }

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

  bool isFull() { return head == (tail + 1) % MAX; }

  void enqueue(process n) {
    if (isFull()) {
      cout << "Queue is full." << endl;
      return;
    }
    queue[tail++] = n;

    if (tail == MAX)
      tail = 0;

    return;
  }

  process dequeue() {
    if (isEmpty()) {
      cout << "Queue is empty." << endl;
      exit(-1);
    }

    process ret = queue[head];

    if (head + 1 == MAX)
      head = 0;
    else
      head++;

    return ret;
  }
};

int main(void) {
  my_queue queue;
  process proc;
  int n, q;
  int time = 0;

  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> proc.name >> proc.time;
    queue.enqueue(proc);
  }

  while (!queue.isEmpty()) {
    proc = queue.dequeue();
    if (proc.time <= q) {
      time += proc.time;
      cout << proc.name << " " << time << endl;
    } else {
      proc.time = proc.time - q;
      queue.enqueue(proc);
      time += q;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

#define MAX 100005

using namespace std;

struct process {
  int time;
  string name;
};

class my_queue {
private:
  int head, tail;
  process queue[MAX];

public:
  my_queue() {
    head = 0;
    tail = 0;
  }

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

  bool isFull() { return head == (tail + 1) % MAX; }

  void enqueue(process n) {
    if (isFull()) {
      cout << "Queue is full." << endl;
      return;
    }
    queue[tail++] = n;

    if (tail == MAX)
      tail = 0;

    return;
  }

  process dequeue() {
    if (isEmpty()) {
      cout << "Queue is empty." << endl;
      exit(-1);
    }

    process ret = queue[head];

    if (head + 1 == MAX)
      head = 0;
    else
      head++;

    return ret;
  }
};

int main(void) {
  my_queue queue;
  process proc;
  int n, q;
  int time = 0;

  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> proc.name >> proc.time;
    queue.enqueue(proc);
  }

  while (!queue.isEmpty()) {
    proc = queue.dequeue();
    if (proc.time <= q) {
      time += proc.time;
      cout << proc.name << " " << time << endl;
    } else {
      proc.time = proc.time - q;
      queue.enqueue(proc);
      time += q;
    }
  }

  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <vector>
#define SIZE 200
using namespace std;

struct Queue {
  int list[SIZE];
  int head, tail;
  void init() { head = tail = 0; }
  bool isEmpty() { return head == tail; }
  bool isFull() { return head == (tail + 1) % SIZE; }
  // int head(){ return list[head]; }
  // int tail(){ return list[tail]; }
  void enqueue(int x) {
    if (isFull()) {
      cout << "ERROR(Over flow)" << endl;
      exit(1);
    }
    list[tail] = x;
    tail++;
    tail %= SIZE;
  }
  int dequeue() {
    if (isEmpty()) {
      cout << "ERROR(Under flow)" << endl;
      exit(1);
    }
    int x = list[head];
    head++;
    head %= SIZE;
    return x;
  }
};
struct Process {
  string n;
  int t;
};
int main() {
  int n, q, idx, now = 0;
  vector<Process> p;
  Queue que;

  // input,initialize
  que.init();
  cin >> n >> q;
  p.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i].n >> p[i].t;
    que.enqueue(i);
  }

  // process
  while (!que.isEmpty()) {
    idx = que.dequeue();
    if (p[idx].t - q > 0) {
      p[idx].t -= q;
      now += q;
      que.enqueue(idx);
    } else {
      now += p[idx].t;
      p[idx].t = now;
      cout << p[idx].n << " " << p[idx].t << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <cstdlib>
#include <iostream>
#include <vector>
#define SIZE 100005
using namespace std;

struct Queue {
  int list[SIZE];
  int head, tail;
  void init() { head = tail = 0; }
  bool isEmpty() { return head == tail; }
  bool isFull() { return head == (tail + 1) % SIZE; }
  // int head(){ return list[head]; }
  // int tail(){ return list[tail]; }
  void enqueue(int x) {
    if (isFull()) {
      cout << "ERROR(Over flow)" << endl;
      exit(1);
    }
    list[tail] = x;
    tail++;
    tail %= SIZE;
  }
  int dequeue() {
    if (isEmpty()) {
      cout << "ERROR(Under flow)" << endl;
      exit(1);
    }
    int x = list[head];
    head++;
    head %= SIZE;
    return x;
  }
};
struct Process {
  string n;
  int t;
};
int main() {
  int n, q, idx, now = 0;
  vector<Process> p;
  Queue que;

  // input,initialize
  que.init();
  cin >> n >> q;
  p.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i].n >> p[i].t;
    que.enqueue(i);
  }

  // process
  while (!que.isEmpty()) {
    idx = que.dequeue();
    if (p[idx].t - q > 0) {
      p[idx].t -= q;
      now += q;
      que.enqueue(idx);
    } else {
      now += p[idx].t;
      p[idx].t = now;
      cout << p[idx].n << " " << p[idx].t << endl;
    }
  }
  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

struct Process {
  string name;
  int time;
};

int main() {
  int n, q;
  Process P[100000];

  cin >> n >> q;

  for (int i = 0; i < n; i++) {
    cin >> P[i].name >> P[i].time;
  }

  int count = 0;
  int origin = n;

  for (int i = 0; i < n; i++) {
    P[i].time -= q;
    if (P[i].time <= 0) {
      count += q + P[i].time;
      cout << P[i].name << " " << count << endl;
    } else {
      n++;
      P[n - 1] = P[i];
      count += q;
    }
  }
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

struct Process {
  string name;
  int time;
};

int main() {
  int n, q;
  Process P[1000000];

  cin >> n >> q;

  for (int i = 0; i < n; i++) {
    cin >> P[i].name >> P[i].time;
  }

  int count = 0;
  int origin = n;

  for (int i = 0; i < n; i++) {
    P[i].time -= q;
    if (P[i].time <= 0) {
      count += q + P[i].time;
      cout << P[i].name << " " << count << endl;
    } else {
      n++;
      P[n - 1] = P[i];
      count += q;
    }
  }
}
Problem p02264 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <string>

using namespace std;

int main() {
  int n, q;
  int elapsed_time = 0; //????????????
  queue<int> TIME;
  queue<string> NAME;
  cin >> n >> q;

  //??\???
  for (int i = 0; i < n; i++) {
    string name;
    int time;
    cin >> name >> time;
    NAME.push(name);
    TIME.push(time);
  }

  //??????
  while (!NAME.empty()) {
    if (TIME.front() > q) {
      elapsed_time += q;

      //?????????????????£?????????????????????
      string temp_name = NAME.front();
      NAME.pop();
      NAME.push(temp_name);

      int temp_time = TIME.front();
      TIME.pop();
      TIME.push(temp_time);
    } else {
      elapsed_time += TIME.front();
      TIME.pop();
      cout << NAME.front() << " " << elapsed_time << endl;
      NAME.pop();
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <string>

using namespace std;

int main() {
  int n, q;
  int elapsed_time = 0; //????????????
  queue<int> TIME;
  queue<string> NAME;
  cin >> n >> q;

  //??\???
  for (int i = 0; i < n; i++) {
    string name;
    int time;
    cin >> name >> time;
    NAME.push(name);
    TIME.push(time);
  }

  //??????
  while (!NAME.empty()) {
    if (TIME.front() > q) {
      elapsed_time += q;

      //?????????????????£?????????????????????
      string temp_name = NAME.front();
      NAME.pop();
      NAME.push(temp_name);

      int temp_time = TIME.front() - q;
      TIME.pop();
      TIME.push(temp_time);
    } else {
      elapsed_time += TIME.front();
      TIME.pop();
      cout << NAME.front() << " " << elapsed_time << endl;
      NAME.pop();
    }
  }

  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

pair<string, int> p[10000000];
pair<string, int> key;
int tail;
int head;

void push(string str, int n) {
  p[tail].first = str;
  p[tail].second = n;
  tail++;
}

void pop() {
  key = p[head];
  head++;
}

int main() {
  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
  }
  tail = n;

  int cnt = 0;
  int sum = 0;
  while (cnt != n) {
    pop();
    string name = key.first;
    int time = key.second;
    // cout << name << " " << time << endl;
    if (time - q > 0) {
      sum += q;
      push(name, time - q);
    } else {
      cnt++;
      sum += time;
      cout << name << " " << sum << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

pair<string, int> p[1000000];
pair<string, int> key;
int tail;
int head;

void push(string str, int n) {
  p[tail].first = str;
  p[tail].second = n;
  tail++;
}

void pop() {
  key = p[head];
  head++;
}

int main() {
  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
  }
  tail = n;

  int cnt = 0;
  int sum = 0;
  while (cnt != n) {
    pop();
    string name = key.first;
    int time = key.second;
    // cout << name << " " << time << endl;
    if (time - q > 0) {
      sum += q;
      push(name, time - q);
    } else {
      cnt++;
      sum += time;
      cout << name << " " << sum << endl;
    }
  }
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define INT_MAX 2147483647

struct Process {
  std::string name;
  int time;
};

class Queue {
private:
  Process Table[INT_MAX];
  int head;
  int top;

public:
  Queue() {
    head = 0;
    top = 0;
  };
  void Enqueue(Process P) { Table[top++] = P; };
  Process Dequeue() { return Table[head++]; };
  bool isEmpty() { return head == top; };
  void show() {
    std::cout << "head : " << head << '\n';
    std::cout << "top : " << top << '\n';
  };
};

int main(int argc, char const *argv[]) {
  int n;
  int q;
  int cumtime = 0;
  Process P;
  Queue queue = Queue();
  std::cin >> n >> q;

  for (size_t i = 0; i < n; i++) {
    std::cin >> P.name;
    std::cin >> P.time;
    queue.Enqueue(P);
  }

  while (!queue.isEmpty()) {
    P = queue.Dequeue();
    P.time -= q;
    if (P.time > 0) {
      queue.Enqueue(P);
      cumtime += q;
    } else {
      cumtime += (q + P.time);
      std::cout << P.name << ' ' << cumtime << '\n';
    }
  }

  return 0;
}


Fixed submission:
#include <iostream>
#define INT_MAX 2147483647

struct Process {
  std::string name;
  int time;
};

class Queue {
private:
  Process Table[1000000];
  int head;
  int top;

public:
  Queue() {
    head = 0;
    top = 0;
  };
  void Enqueue(Process P) { Table[top++] = P; };
  Process Dequeue() { return Table[head++]; };
  bool isEmpty() { return head == top; };
  void show() {
    std::cout << "head : " << head << '\n';
    std::cout << "top : " << top << '\n';
  };
};

int main(int argc, char const *argv[]) {
  int n;
  int q;
  int cumtime = 0;
  Process P;
  Queue queue = Queue();
  std::cin >> n >> q;

  for (size_t i = 0; i < n; i++) {
    std::cin >> P.name;
    std::cin >> P.time;
    queue.Enqueue(P);
  }

  while (!queue.isEmpty()) {
    P = queue.Dequeue();
    P.time -= q;
    if (P.time > 0) {
      queue.Enqueue(P);
      cumtime += q;
    } else {
      cumtime += (q + P.time);
      std::cout << P.name << ' ' << cumtime << '\n';
    }
  }

  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <utility>
using namespace std;

int main(void) {
  int Q[100001];
  int tail = 0, head = 0;

  pair<string, int> p[100001];
  int n, q, su = 0;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
    Q[tail] = i;
    tail++;
    su += p[i].second;
  }
  int sum = 0, k;
  while (su != sum) {
    int d = Q[head];
    head++;
    if (p[d].second > q) {
      p[d].second = p[d].second - q;
      Q[tail] = d;
      tail++;
      sum += q;
    } else {
      sum += p[d].second;
      cout << p[d].first << " " << sum << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
#include <utility>
using namespace std;

int main(void) {
  int Q[1000000];
  int tail = 0, head = 0;

  pair<string, int> p[100001];
  int n, q, su = 0;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
    Q[tail] = i;
    tail++;
    su += p[i].second;
  }
  int sum = 0, k;
  while (su != sum) {
    int d = Q[head];
    head++;
    if (p[d].second > q) {
      p[d].second = p[d].second - q;
      Q[tail] = d;
      tail++;
      sum += q;
    } else {
      sum += p[d].second;
      cout << p[d].first << " " << sum << endl;
    }
  }
  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
#define MAX 100000
using namespace std;

struct pp {
  int t;
  string s;
};

class queue {
public:
  int head;
  int tail;
  vector<pp> v;
  queue();
  void enqueue(pp x);
  pp dequeue();
  bool isEmpty();
};

queue::queue() {
  head = 0;
  tail = 0;
  v.resize(MAX);
}

void queue::enqueue(pp x) {
  v[tail].t = x.t;
  v[tail].s = x.s;
  tail++;
}

pp queue::dequeue() {
  head++;
  return v[head - 1];
}

bool queue::isEmpty() { return head == tail; }

int main() {
  int n;
  cin >> n;
  int q;
  cin >> q;
  queue q1;
  for (int i = 0; i < n; i++) {
    pp p;
    cin >> p.s >> p.t;
    // cout << p.s << endl;
    q1.enqueue(p);
  }

  int passtime = 0;
  vector<pp> v;
  while (!q1.isEmpty()) {
    pp ptmp = q1.dequeue();
    // cout << ptmp.s << endl;
    if (ptmp.t > q) {
      passtime += q;
      ptmp.t -= q;
      q1.enqueue(ptmp);
    } else {
      passtime += ptmp.t;
      ptmp.t = passtime;
      v.push_back(ptmp);
    }
  }
  for (int i = 0; i < v.size(); i++) {
    cout << v[i].s << " " << v[i].t << endl;
  }
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
#define MAX 1000000
using namespace std;

struct pp {
  int t;
  string s;
};

class queue {
public:
  int head;
  int tail;
  vector<pp> v;
  queue();
  void enqueue(pp x);
  pp dequeue();
  bool isEmpty();
};

queue::queue() {
  head = 0;
  tail = 0;
  v.resize(MAX);
}

void queue::enqueue(pp x) {
  v[tail].t = x.t;
  v[tail].s = x.s;
  tail++;
}

pp queue::dequeue() {
  head++;
  return v[head - 1];
}

bool queue::isEmpty() { return head == tail; }

int main() {
  int n;
  cin >> n;
  int q;
  cin >> q;
  queue q1;
  for (int i = 0; i < n; i++) {
    pp p;
    cin >> p.s >> p.t;
    // cout << p.s << endl;
    q1.enqueue(p);
  }

  int passtime = 0;
  vector<pp> v;
  while (!q1.isEmpty()) {
    pp ptmp = q1.dequeue();
    // cout << ptmp.s << endl;
    if (ptmp.t > q) {
      passtime += q;
      ptmp.t -= q;
      q1.enqueue(ptmp);
    } else {
      passtime += ptmp.t;
      ptmp.t = passtime;
      v.push_back(ptmp);
    }
  }
  for (int i = 0; i < v.size(); i++) {
    cout << v[i].s << " " << v[i].t << endl;
  }
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int head, tail, n;
struct task {
  char name[100];
  int time;
} t, Q[100005];
void enqueue(task x) {
  Q[tail] = x;
  tail++;
}
task dequeue() {
  task x = Q[head];
  head++;
  return x;
}
int main() {
  int q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> Q[i].name >> Q[i].time;
  head = 1;
  tail = n + 1;
  int sum = 0;
  while (head != tail) {
    t = dequeue();
    if (t.time > q) {
      sum += q;
      t.time -= q;
      enqueue(t);
    } else {
      sum += t.time;
      cout << t.name << ' ' << sum << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int head, tail, n;
struct task {
  char name[100];
  int time;
} t, Q[1000000];
void enqueue(task x) {
  Q[tail] = x;
  tail++;
}
task dequeue() {
  task x = Q[head];
  head++;
  return x;
}
int main() {
  int q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> Q[i].name >> Q[i].time;
  head = 1;
  tail = n + 1;
  int sum = 0;
  while (head != tail) {
    t = dequeue();
    if (t.time > q) {
      sum += q;
      t.time -= q;
      enqueue(t);
    } else {
      sum += t.time;
      cout << t.name << ' ' << sum << endl;
    }
  }
  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cassert>

#include <algorithm>
#include <deque>
#include <iostream>
#include <string>

using namespace std;

#define DEBUG(x) cerr << #x << " = " << x << endl

int main() {
  int n;
  cin >> n;
  deque<int> l;
  while (n--) {
    string s;
    cin >> s;
    if (s == "insert") {
      int x;
      cin >> x;
      l.push_front(x);
    } else if (s == "delete") {
      int x;
      cin >> x;
      auto it = find(l.begin(), l.end(), x);
      if (it != l.end()) {
        l.erase(it);
      }
    } else if (s == "deleteFirst") {
      l.pop_front();
    } else if (s == "deleteLast") {
      l.pop_back();
    } else {
      assert(false);
    }
  }

  int k = 0;
  for (int item : l) {
    cout << item << " \n"[k++ == l.size() - 1];
  }
}

Fixed submission:
#include <cassert>

#include <algorithm>
#include <deque>
#include <iostream>
#include <string>

using namespace std;

#define DEBUG(x) cerr << #x << " = " << x << endl

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  deque<int> l;
  while (n--) {
    string s;
    cin >> s;
    if (s == "insert") {
      int x;
      cin >> x;
      l.push_front(x);
    } else if (s == "delete") {
      int x;
      cin >> x;
      auto it = find(l.begin(), l.end(), x);
      if (it != l.end()) {
        l.erase(it);
      }
    } else if (s == "deleteFirst") {
      l.pop_front();
    } else if (s == "deleteLast") {
      l.pop_back();
    } else {
      assert(false);
    }
  }

  int k = 0;
  for (int item : l) {
    cout << item << " \n"[k++ == l.size() - 1];
  }
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;

int main(void) {
  ll N;
  cin >> N;
  deque<ll> q;
  REP(i, 0, N) {
    string C;
    ll X;
    cin >> C;
    if (C == "insert" || C == "delete")
      cin >> X;

    if (C == "insert") {
      q.push_front(X);
    }
    if (C == "delete") {
      deque<ll> _q;
      bool f = false;
      while (q.size()) {
        if (q.front() != X || f) {
          _q.push_back(q.front());
        } else if (!f) {
          f = true;
        }
        q.pop_front();
      }
      swap(q, _q);
    }
    if (C == "deleteFirst") {
      q.pop_front();
    }
    if (C == "deleteLast") {
      q.pop_back();
    }
  }

  while (q.size()) {
    cout << q.front() << (q.size() != 1 ? " " : "\n");
    q.pop_front();
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  deque<ll> q;
  REP(i, 0, N) {
    string C;
    ll X;
    cin >> C;
    if (C == "insert" || C == "delete")
      cin >> X;

    if (C == "insert") {
      q.push_front(X);
    }
    if (C == "delete") {
      deque<ll> _q;
      bool f = false;
      while (q.size()) {
        if (q.front() != X || f) {
          _q.push_back(q.front());
        } else if (!f) {
          f = true;
        }
        q.pop_front();
      }
      swap(q, _q);
    }
    if (C == "deleteFirst") {
      q.pop_front();
    }
    if (C == "deleteLast") {
      q.pop_back();
    }
  }

  while (q.size()) {
    cout << q.front() << (q.size() != 1 ? " " : "\n");
    q.pop_front();
  }
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, a;
  string str;
  list<int> l;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == "insert") {
      cin >> a;
      l.push_front(a);
    } else if (str == "delete") {
      cin >> a;
      auto itr = l.begin();
      for (; *itr != a; ++itr)
        ;
      l.erase(itr);
    } else if (str == "deleteFirst") {
      l.pop_front();
    } else if (str == "deleteLast") {
      l.pop_back();
    }
  }
  auto itr = l.begin();
  cout << *itr;
  ++itr;
  for (; itr != l.end(); ++itr) {
    cout << " " << *itr;
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, a;
  string str;
  list<int> l;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == "insert") {
      cin >> a;
      l.push_front(a);
    } else if (str == "delete") {
      cin >> a;
      auto itr = l.begin();
      for (; itr != l.end(); ++itr)
        if (*itr == a) {
          l.erase(itr);
          break;
        }
    } else if (str == "deleteFirst") {
      l.pop_front();
    } else if (str == "deleteLast") {
      l.pop_back();
    }
  }
  auto itr = l.begin();
  cout << *itr;
  ++itr;
  for (; itr != l.end(); ++itr) {
    cout << " " << *itr;
  }
  cout << endl;
  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <string>

using namespace std;

int main() {
  // const string
  //   str_insert = "insert",
  //   str_delete = "delete",
  //   str_delete_first = "deleteFirst",
  //   str_delete_last = "deleteLast";

  int N;
  cin >> N;

  list<int> l;
  // typedef enum{INS, DEL, DEL_F, DEL_L}
  // command;

  for (int i = 0; i < N; ++i) {
    string s;
    cin >> s;

    // command c;
    if (s[0] == 'i') {
      int n;
      cin >> n;
      l.push_front(n);
      // break;
    } else if (s.size() == 6) {
      int n;
      cin >> n;
      auto itr = find(l.begin(), l.end(), n);
      if (itr != l.end()) {
        l.erase(itr);
      }
      // break;
    } else if (s[6] == 'F') {
      l.pop_front();
      // break;
    } else if (s[6] == 'L') {
      l.pop_back();
      // break;
    }

    // cout << *(l.begin());
    // for(std::list<int>::iterator itr = next(l.begin()); itr!=l.end(); ++itr){
    //   cout << ' ' << *itr;
    // }
    // cout << endl;
  }

  cout << *(l.begin());
  for (std::list<int>::iterator itr = next(l.begin()); itr != l.end(); ++itr) {
    cout << ' ' << *itr;
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <string>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  // const string
  //   str_insert = "insert",
  //   str_delete = "delete",
  //   str_delete_first = "deleteFirst",
  //   str_delete_last = "deleteLast";

  int N;
  cin >> N;

  list<int> l;
  // typedef enum{INS, DEL, DEL_F, DEL_L}
  // command;

  for (int i = 0; i < N; ++i) {
    string s;
    cin >> s;

    // command c;
    if (s[0] == 'i') {
      int n;
      cin >> n;
      l.push_front(n);
      // break;
    } else if (s.size() == 6) {
      int n;
      cin >> n;
      auto itr = find(l.begin(), l.end(), n);
      if (itr != l.end()) {
        l.erase(itr);
      }
      // break;
    } else if (s[6] == 'F') {
      l.pop_front();
      // break;
    } else if (s[6] == 'L') {
      l.pop_back();
      // break;
    }

    // cout << *(l.begin());
    // for(std::list<int>::iterator itr = next(l.begin()); itr!=l.end(); ++itr){
    //   cout << ' ' << *itr;
    // }
    // cout << endl;
  }

  cout << *(l.begin());
  for (std::list<int>::iterator itr = next(l.begin()); itr != l.end(); ++itr) {
    cout << ' ' << *itr;
  }
  cout << endl;

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

struct Node {
  int data;
  Node *next, *prev;
};

class List {
  Node *nil;
  Node *searchNode(int x) {
    Node *p = nil->next;
    while (p != nil && p->data != x) {
      p = p->next;
    }
    return p;
  }
  void deleteNode(Node *node) {
    if (node == nil)
      return;
    node->prev->next = node->next;
    node->next->prev = node->prev;
    delete (node);
  }

public:
  List() {
    nil = new Node();
    nil->next = nil;
    nil->prev = nil;
  }
  void insertFirst(int x) {
    Node *node = new Node();
    node->data = x;
    node->next = nil->next;
    node->prev = nil;

    nil->next->prev = node;
    nil->next = node;
  }
  void deleteData(int x) { deleteNode(searchNode(x)); }
  void deleteFirst() { deleteNode(nil->next); }
  void deleteLast() { deleteNode(nil->prev); }
  void print() {
    Node *p = nil->next;
    while (p != nil) {
      if (p != nil->next)
        cout << " ";
      cout << p->data;
      p = p->next;
    }
    cout << endl;
  }
};

int main() {
  List l;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int num;
    string command;
    cin >> command;
    if (command == "insert") {
      cin >> num;
      l.insertFirst(num);
    } else if (command == "delete") {
      cin >> num;
      l.deleteData(num);
    } else if (command == "deleteFirst") {
      l.deleteFirst();
    } else if (command == "deleteLast") {
      l.deleteLast();
    }
  }
  l.print();
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

struct Node {
  int data;
  Node *next, *prev;
};

class List {
  Node *nil;
  Node *searchNode(int x) {
    Node *p = nil->next;
    while (p != nil && p->data != x) {
      p = p->next;
    }
    return p;
  }
  void deleteNode(Node *node) {
    if (node == nil)
      return;
    node->prev->next = node->next;
    node->next->prev = node->prev;
    delete (node);
  }

public:
  List() {
    nil = new Node();
    nil->next = nil;
    nil->prev = nil;
  }
  void insertFirst(int x) {
    Node *node = new Node();
    node->data = x;
    node->next = nil->next;
    node->prev = nil;

    nil->next->prev = node;
    nil->next = node;
  }
  void deleteData(int x) { deleteNode(searchNode(x)); }
  void deleteFirst() { deleteNode(nil->next); }
  void deleteLast() { deleteNode(nil->prev); }
  void print() {
    Node *p = nil->next;
    while (p != nil) {
      if (p != nil->next)
        cout << " ";
      cout << p->data;
      p = p->next;
    }
    cout << endl;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  List l;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int num;
    string command;
    cin >> command;
    if (command == "insert") {
      cin >> num;
      l.insertFirst(num);
    } else if (command == "delete") {
      cin >> num;
      l.deleteData(num);
    } else if (command == "deleteFirst") {
      l.deleteFirst();
    } else if (command == "deleteLast") {
      l.deleteLast();
    }
  }
  l.print();
  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <list>
#include <sstream>
#include <string>
using namespace std;

list<int>::iterator find_key(list<int> &li, int key) {
  list<int>::iterator li_itr = li.begin();
  while (li_itr != li.end()) {
    if (*li_itr == key) {
      return li_itr;
    }
    ++li_itr;
  }
  return li.end();
}

int main(void) {
  ios::sync_with_stdio(false);

  int n;
  list<int> li;

  cin >> n;
  for (int i = 0; i < n; i++) {
    string command;
    cin >> command;

    int key;
    if (command == "insert") {
      cin >> key;
      li.push_front(key);
    } else if (command == "deleteFirst") {
      li.pop_front();
    } else if (command == "deleteLast") {
      li.pop_back();
    } else {
      cin >> key;
      li.erase(find_key(li, key));
    }
  }

  list<int>::iterator itr = li.begin();
  while (itr != li.end()) {
    cout << *itr;
    ++itr;
    itr == li.end() ? cout << "" : cout << " ";
  }
  cout << endl;
}

Fixed submission:
#include <iostream>
#include <list>
#include <sstream>
#include <string>
using namespace std;

list<int>::iterator find_key(list<int> &li, int key) {
  list<int>::iterator li_itr = li.begin();
  while (li_itr != li.end()) {
    if (*li_itr == key) {
      return li_itr;
    }
    ++li_itr;
  }
  return li.end();
}

int main(void) {
  ios::sync_with_stdio(false);

  int n;
  list<int> li;

  cin >> n;
  for (int i = 0; i < n; i++) {
    string command;
    cin >> command;

    int key;
    if (command == "insert") {
      cin >> key;
      li.push_front(key);
    } else if (command == "deleteFirst") {
      li.pop_front();
    } else if (command == "deleteLast") {
      li.pop_back();
    } else {
      cin >> key;
      list<int>::iterator erace_itr = find_key(li, key);
      if (erace_itr != li.end()) {
        li.erase(find_key(li, key));
      }
    }
  }

  list<int>::iterator itr = li.begin();
  while (itr != li.end()) {
    cout << *itr;
    ++itr;
    itr == li.end() ? cout << "" : cout << " ";
  }
  cout << endl;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <list>

#define REP(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

int main() {
  list<int> data;
  list<int>::iterator it;
  int key, n, i;
  string command;

  cin >> n;
  rep(i, n) {
    cin >> command;
    if (command == "insert") {
      cin >> key;
      data.push_front(key);
    } else if (command == "delete") {
      cin >> key;
      it = data.begin();
      while (it != data.end()) {
        if (*it == key) {
          data.erase(it);
          break;
        }
        it++;
      }
    } else if (command == "deleteFirst") {
      data.pop_front();
    } else if (command == "deleteLast") {
      data.pop_back();
    }
  }

  it = data.begin();
  while (it != data.end()) {
    cout << *it;
    it++;
    if (it != data.end())
      cout << " ";
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <list>

#define REP(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

int main() {
  std::cin.tie(0);                  // tie
  std::ios::sync_with_stdio(false); // sync

  list<int> data;
  list<int>::iterator it;
  int key, n, i;
  string command;

  cin >> n;
  rep(i, n) {
    cin >> command;
    if (command == "insert") {
      cin >> key;
      data.push_front(key);
    } else if (command == "delete") {
      cin >> key;
      it = data.begin();
      while (it != data.end()) {
        if (*it == key) {
          data.erase(it);
          break;
        }
        it++;
      }
    } else if (command == "deleteFirst") {
      data.pop_front();
    } else if (command == "deleteLast") {
      data.pop_back();
    }
  }

  it = data.begin();
  while (it != data.end()) {
    cout << *it;
    it++;
    if (it != data.end())
      cout << " ";
  }
  cout << endl;

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

struct Node {
  int key;
  Node *prev, *next;
};

Node *nil;

Node *search(int k) {
  Node *x = nil->next;

  while (x != nil && x->key != k)
    x = x->next;

  return x;
}

void insert(int k) {
  Node *x = (Node *)malloc(sizeof(Node));

  x->key = k;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

void deleteNode(Node *x) {
  x->prev->next = x->next;
  x->next->prev = x->prev;

  free(x);
}

void deleteFirst() { deleteNode(nil->next); }

void deleteLast() { deleteNode(nil->prev); }

int main() {

  int n, k;
  char command[20];

  nil = (Node *)malloc(sizeof(Node));
  nil->prev = nil;
  nil->next = nil;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", command);
    if (strcmp(command, "insert") == 0) {
      scanf("%d", &k);
      insert(k);
    } else if (strcmp(command, "delete") == 0) {
      scanf("%d", &k);
      deleteNode(search(k));
    } else if (strcmp(command, "deleteFirst") == 0) {
      deleteFirst();
    } else if (strcmp(command, "deleteLast") == 0) {
      deleteLast();
    }
  }

  k = 0;
  Node *x = nil->next;
  while (x != nil) {
    printf((k ? " " : ""));
    printf("%d", x->key);
    k++;
    x = x->next;
  }
  printf("\n");

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

struct Node {
  int key;
  Node *prev, *next;
};

Node *nil;

Node *search(int k) {
  Node *x = nil->next;

  while (x != nil && x->key != k)
    x = x->next;

  return x;
}

void insert(int k) {
  Node *x = (Node *)malloc(sizeof(Node));

  x->key = k;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

void deleteNode(Node *x) {
  if (x == nil)
    return;
  x->prev->next = x->next;
  x->next->prev = x->prev;

  free(x);
}

void deleteFirst() { deleteNode(nil->next); }

void deleteLast() { deleteNode(nil->prev); }

int main() {

  int n, k;
  char command[20];

  nil = (Node *)malloc(sizeof(Node));
  nil->prev = nil;
  nil->next = nil;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", command);
    if (strcmp(command, "insert") == 0) {
      scanf("%d", &k);
      insert(k);
    } else if (strcmp(command, "delete") == 0) {
      scanf("%d", &k);
      deleteNode(search(k));
    } else if (strcmp(command, "deleteFirst") == 0) {
      deleteFirst();
    } else if (strcmp(command, "deleteLast") == 0) {
      deleteLast();
    }
  }

  k = 0;
  Node *x = nil->next;
  while (x != nil) {
    printf((k ? " " : ""));
    printf("%d", x->key);
    k++;
    x = x->next;
  }
  printf("\n");

  return 0;
}
Problem p02265 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <list>
using namespace std;

int main() {
  int n, k;
  char s[15];
  list<int> l;

  scanf("%d", &n);

  while (n--) {
    scanf("%s", s);
    if (*s == 'i') {
      scanf("%d", &k);
      l.push_front(k);
      continue;
    }
    switch (*(s + 6)) {
    case 'F':
      l.pop_front();
      break;
    case 'L':
      l.pop_back();
      break;
    default:
      scanf("%d", &k);
      l.erase(find(l.begin(), l.end(), k));
    }
  }

  int &f = l.front();
  for (int &x : l)
    printf("%s%d", (&f == &x ? "" : " "), x);
  puts("");
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <list>
using namespace std;

int main() {
  int n, k;
  char s[15];
  list<int> l;

  scanf("%d", &n);

  while (n--) {
    scanf("%s", s);
    if (*s == 'i') {
      scanf("%d", &k);
      l.push_front(k);
      continue;
    }
    switch (*(s + 6)) {
    case 'F':
      l.pop_front();
      break;
    case 'L':
      l.pop_back();
      break;
    default:
      scanf("%d", &k);
      auto r = find(l.begin(), l.end(), k);
      if (r != l.end())
        l.erase(r);
    }
  }

  int &f = l.front();
  for (int &x : l)
    printf("%s%d", (&f == &x ? "" : " "), x);
  puts("");
}
Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <string>

using namespace std;

deque<int> dll;

void printList() {
  deque<int>::iterator itr = dll.begin();
  deque<int>::iterator itrE = dll.end();
  for (; itr != itrE; itr++) {
    cout << *itr;
    if (itr != itrE - 1)
      cout << ' ';
  }
  cout << endl;
}

int main() {
  int n;
  cin >> n;

  string cmd;
  int key;
  for (int i = 0; i < n; i++) {
    cin >> cmd;
    if ("insert" == cmd) {
      cin >> key;
      dll.push_front(key);
    } else if ("deleteFirst" == cmd) {
      dll.pop_front();
    } else if ("deleteLast" == cmd) {
      dll.pop_back();
    } else if ("delete" == cmd) {
      cin >> key;
      deque<int>::iterator itr = dll.begin();
      deque<int>::iterator itrE = dll.end();
      for (; itr != itrE; itr++) {
        if (key == *itr) {
          dll.erase(itr);
          break;
        }
      }
    }
  }
  printList();

  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <string>

using namespace std;

deque<int> dll;

void printList() {
  deque<int>::iterator itr = dll.begin();
  deque<int>::iterator itrE = dll.end();
  for (; itr != itrE; itr++) {
    cout << *itr;
    if (itr != itrE - 1)
      cout << ' ';
  }
  cout << endl;
}

int main() {
  int n;
  ios::sync_with_stdio(false);
  cin >> n;

  string cmd;
  int key;
  for (int i = 0; i < n; i++) {
    cin >> cmd;
    if ("insert" == cmd) {
      cin >> key;
      dll.push_front(key);
    } else if ("deleteFirst" == cmd) {
      dll.pop_front();
    } else if ("deleteLast" == cmd) {
      dll.pop_back();
    } else if ("delete" == cmd) {
      cin >> key;
      deque<int>::iterator itr = dll.begin();
      deque<int>::iterator itrE = dll.end();
      for (; itr != itrE; itr++) {
        if (key == *itr) {
          dll.erase(itr);
          break;
        }
      }
    }
  }
  printList();

  return 0;
}