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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
using namespace std;

int N = 1001001;
int a[1001001];
int p0;
int p1;

void insertx(int x) {
  if (p0 == 0) {
    a[N - 1] = x;
    p0 = N - 1;
  } else {
    a[p0 - 1] = x;
    p0--;
  }
}
void deletex(int x) {
  if (p0 <= p1) {
    for (int i = p0; i < p1; i++) {
      if (x == a[i]) {
        for (int j = i; j < p1 - 1; j++) {
          a[j] = a[j + 1];
        }
        p1--;
        break;
      }
    }
  } else {
    int f = 0;
    for (int i = p0; i < N; i++) {
      if (x == a[i]) {
        for (int j = i; j >= p0 + 1; j--) {
          a[j] = a[j - 1];
        }
        p0++;
        if (p0 == N)
          p0 = 0;
        f = 1;
        break;
      }
    }
    if (f == 0) {
      for (int i = 0; i < p1; i++) {
        if (x == a[i]) {
          for (int j = i; j < p1 - 1; j++) {
            a[j] = a[j + 1];
          }
          p1--;
          break;
        }
      }
    }
  }
}

void deleteFirst() {
  p0++;
  if (p0 == N)
    p0 = 0;
}

void deleteLast() {
  p1--;
  if (p1 < 0)
    p1 = N - 1;
}

int main() {
  int n;
  cin >> n;
  p0 = 0;
  p1 = 0;
  for (int i = 0; i < n; i++) {
    char s_[100];
    string s;
    int m;
    scanf("%s", s_);
    s = s_;
    if (s == "deleteFirst")
      deleteFirst();
    else if (s == "deleteLast")
      deleteLast();
    else {
      scanf("%d", &m);
      if (s == "insert")
        insertx(m);
      if (s == "delete")
        deletex(m);
    }
  }

  if (p0 <= p1) {
    for (int i = p0; i < p1; i++) {
      cout << a[i];
      if (i == p1 - 1)
        cout << endl;
      else
        cout << " ";
    }
  } else {
    for (int i = p0; i < N; i++) {
      cout << a[i];
      if (i != N - 1)
        cout << " ";
    }
    for (int i = 0; i < p1; i++) {
      cout << " " << a[i];
    }
    cout << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
using namespace std;

int N = 1001001;
int a[1001001];
int p0;
int p1;

void insertx(int x) {
  if (p0 == 0) {
    a[N - 1] = x;
    p0 = N - 1;
  } else {
    a[p0 - 1] = x;
    p0--;
  }
}
void deletex(int x) {
  if (p0 <= p1) {
    for (int i = p0; i < p1; i++) {
      if (x == a[i]) {
        for (int j = i; j < p1 - 1; j++) {
          a[j] = a[j + 1];
        }
        p1--;
        break;
      }
    }
  } else {
    int f = 0;
    for (int i = p0; i < N; i++) {
      if (x == a[i]) {
        for (int j = i; j >= p0 + 1; j--) {
          a[j] = a[j - 1];
        }
        p0++;
        if (p0 == N)
          p0 = 0;
        f = 1;
        break;
      }
    }
    if (f == 0) {
      for (int i = 0; i < p1; i++) {
        if (x == a[i]) {
          for (int j = i; j < p1 - 1; j++) {
            a[j] = a[j + 1];
          }
          p1--;
          break;
        }
      }
    }
  }
}

void deleteFirst() {
  p0++;
  if (p0 == N)
    p0 = 0;
}

void deleteLast() {
  p1--;
  if (p1 < 0)
    p1 = N - 1;
}

int main() {
  int n;
  cin >> n;
  p0 = 0;
  p1 = 0;
  for (int i = 0; i < n; i++) {
    string s;
    int m;
    cin >> s;
    if (s == "deleteFirst")
      deleteFirst();
    else if (s == "deleteLast")
      deleteLast();
    else {
      cin >> m;
      if (s == "insert")
        insertx(m);
      if (s == "delete")
        deletex(m);
    }
  }

  if (p0 <= p1) {
    for (int i = p0; i < p1; i++) {
      cout << a[i];
      if (i == p1 - 1)
        cout << endl;
      else
        cout << " ";
    }
  } else {
    for (int i = p0; i < N; i++) {
      cout << a[i];
      if (i != N - 1)
        cout << " ";
    }
    for (int i = 0; i < p1; i++) {
      cout << " " << a[i];
    }
    cout << endl;
  }

  return 0;
}