Problem p03818 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

// #define LOCAL 0
// #ifdef LOCAL
// #define DEBUG(s) cout << (s) << endl
// #define dump(x)  cerr << #x << " = " << (x) << endl
// #define BR cout << endl;
// #else
// #define DEBUG(s) do{}while(0)
// #define dump(x) do{}while(0)
// #define BR
// #endif

// 改造
typedef long long int ll;
using namespace std;
#define INF (1 << 20)
#define INFl (ll)5e15
#define DEBUG 0 // デバッグする時1にしてね

// ここから編集する

int main() {
  vector<int> a(100001, 0);
  int N;
  cin >> N;
  REP(i, 0, N) {
    int tmp;
    cin >> tmp;
    a[tmp]++;
  }

  priority_queue<int> q;

  REP(i, 0, 100001) {
    if (a[i] > 0) {
      q.push(a[i]);
    }
  }
  bool cont = true;
  while (cont) {
    int a = q.top();
    q.pop();
    int b = q.top();
    q.pop();

    int diff = 0;
    switch (a) {
    case 0:
      cont = false;
      break;
    case 1:
      switch (b) {
      case 0:
        cont = false;
        q.push(a);
        break;
      case 1:
        cont = false;
        q.push(a);
        q.push(b);
        break;
      default:
        break;
      }
      break;
    default:
      switch (b) {
      case 0:
        if (a % 2 == 0) {
          diff = a;
        } else {
          diff = a - 1;
        }
        a -= diff;
        if (a > 0)
          q.push(a);
        cont = false;
        break;
      case 1:
        if (a % 2 == 0) {
          diff = a;
        } else {
          diff = a - 1;
        }
        a -= diff;
        if (a > 0)
          q.push(a);
        q.push(b);
        cont = false;
        break;
      default:
        diff = b - 1;
        a -= diff;
        b -= diff;
        q.push(a);
        q.push(b);
        break;
      }
      break;
    }
  }

  cout << q.size() << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

// #define LOCAL 0
// #ifdef LOCAL
// #define DEBUG(s) cout << (s) << endl
// #define dump(x)  cerr << #x << " = " << (x) << endl
// #define BR cout << endl;
// #else
// #define DEBUG(s) do{}while(0)
// #define dump(x) do{}while(0)
// #define BR
// #endif

// 改造
typedef long long int ll;
using namespace std;
#define INF (1 << 20)
#define INFl (ll)5e15
#define DEBUG 0 // デバッグする時1にしてね

// ここから編集する

int main() {
  vector<int> a(100001, 0);
  int N;
  cin >> N;
  REP(i, 0, N) {
    int tmp;
    cin >> tmp;
    a[tmp]++;
  }

  priority_queue<int> q;

  REP(i, 0, 100001) {
    if (a[i] > 0) {
      q.push(a[i]);
    }
  }
  bool cont = true;
  while (cont) {
    if (q.size() <= 1)
      break;
    int a = q.top();
    q.pop();
    int b = q.top();
    q.pop();

    int diff = 0;
    switch (a) {
    case 0:
      cont = false;
      break;
    case 1:
      switch (b) {
      case 0:
        cont = false;
        q.push(a);
        break;
      case 1:
        cont = false;
        q.push(a);
        q.push(b);
        break;
      default:
        break;
      }
      break;
    default:
      switch (b) {
      case 0:
        if (a % 2 == 0) {
          diff = a;
        } else {
          diff = a - 1;
        }
        a -= diff;
        if (a > 0)
          q.push(a);
        cont = false;
        break;
      case 1:
        if (a % 2 == 0) {
          diff = a;
        } else {
          diff = a - 1;
        }
        a -= diff;
        if (a > 0)
          q.push(a);
        q.push(b);
        cont = false;
        break;
      default:
        diff = b - 1;
        a -= diff;
        b -= diff;
        q.push(a);
        q.push(b);
        break;
      }
      break;
    }
  }

  cout << q.size() << endl;
  return 0;
}