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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define loop(a, t) for (int a = 0; a < t; a++)
#define forever while (true)
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define pop_front(a) a.erase(a.begin())
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  forever {

    vector<int> taro, hanako;
    cin >> n;
    if (n == 0) {
      break;
    }
    loop(i, n) {
      int u;
      cin >> u;
      taro.push_back(u);
    }
    Sort(taro);
    for (int i = 1; i <= 2 * n; i++) {
      bool flag = false;
      for (int j = 0; j < taro.size(); j++) {
        if (taro[j] == i) {
          flag = true;
        }
      }
      if (!flag) {
        hanako.push_back(i);
      }
    }
    int now_card = 0; // 今のカード
    bool turn = true; // true taro, false hanako
    forever {
      if (taro.size() == 0 || hanako.size() == 0) {
        cout << hanako.size() << endl << taro.size() << endl;
        break;
      }
      if (turn) {
        for (int i = 0; i < taro.size(); i++) {
          if (taro[i] > now_card) {
            now_card = taro[i];
            taro.erase(taro.begin() + i);
            break;
          }
          if (i == taro.size() - 1) {
            now_card = 0;
          }
        }
      } else {
        for (int i = 0; i < hanako.size(); i++) {
          if (hanako[i] > now_card) {
            now_card = hanako[i];
            hanako.erase(hanako.begin() + i);
            break;
          }
          if (i == hanako.size() - 1) {
            now_card = 0;
          }
        }
      }

      turn = !turn;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define loop(a, t) for (int a = 0; a < t; a++)
#define forever while (true)
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define pop_front(a) a.erase(a.begin())
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  vector<int> taro, hanako;
  forever {
    cin >> n;
    if (n == 0) {
      break;
    }
    loop(i, n) {
      int u;
      cin >> u;
      taro.push_back(u);
    }
    Sort(taro);
    for (int i = 1; i <= 2 * n; i++) {
      bool flag = false;
      for (int j = 0; j < taro.size(); j++) {
        if (taro[j] == i) {
          flag = true;
        }
      }
      if (!flag) {
        hanako.push_back(i);
      }
    }
    int now_card = 0; // 今のカード
    bool turn;        // true taro, false hanako
    forever {
      if (taro.size() == 0 || hanako.size() == 0) {
        cout << taro.size() << endl << hanako.size() << endl;
      }
      if (turn) {
        for (int i = 0; i < taro.size(); i++) {
          if (taro[i] > now_card) {
            now_card = taro[i];
            taro.erase(taro.begin() + i);
            break;
          }
          if (i == taro.size() - 1) {
            now_card = 0;
          }
        }
      } else {
        for (int i = 0; i < hanako.size(); i++) {
          if (hanako[i] > now_card) {
            now_card = taro[i];
            hanako.erase(taro.begin() + i);
            break;
          }
          if (i == hanako.size() - 1) {
            now_card = 0;
          }
        }
      }

      turn = !turn;
    }
  }
  return 0;
}