Problem p03816 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (long long i = 0; i < n; ++i)
#define REPR(i, n) for (long long i = n; i >= 0; --i)
#define REPI(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define REPIR(itr, v) for (auto itr = v.rbegin(); itr != v.rend(); ++itr)
#define FOR(i, a, b) for (long long i = a; i < b; ++i)
#define SORT(v, n) sort(v, v + n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(), v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair
#define SV(v)                                                                  \
  {                                                                            \
    for (long long sitr = 0; sitr < v.size(); ++sitr) {                        \
      cin >> v[sitr];                                                          \
    }                                                                          \
  }

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

typedef pair<int, int> pii;

int main() {

  int n;
  cin >> n;

  priority_queue<int> que;
  map<int, int> mp;
  REP(i, n) {
    int tmp;
    cin >> tmp;
    mp[tmp]++;
  }

  REPI(itr, mp) { que.push(itr->second); }

  while (true) {
    if (que.top() == 1) {
      break;
    }
    int a, b;
    a = que.top();
    que.pop();
    b = que.top();
    que.pop();
    a--;
    b--;
    que.push(a);
    if (b > 0) {
      que.push(b);
    }
  }

  cout << que.size() << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (long long i = 0; i < n; ++i)
#define REPR(i, n) for (long long i = n; i >= 0; --i)
#define REPI(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define REPIR(itr, v) for (auto itr = v.rbegin(); itr != v.rend(); ++itr)
#define FOR(i, a, b) for (long long i = a; i < b; ++i)
#define SORT(v, n) sort(v, v + n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(), v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair
#define SV(v)                                                                  \
  {                                                                            \
    for (long long sitr = 0; sitr < v.size(); ++sitr) {                        \
      cin >> v[sitr];                                                          \
    }                                                                          \
  }

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

typedef pair<int, int> pii;

int main() {

  int n;
  cin >> n;

  priority_queue<int> que;
  map<int, int> mp;
  REP(i, n) {
    int tmp;
    cin >> tmp;
    mp[tmp]++;
  }

  REPI(itr, mp) { que.push(itr->second); }

  while (true) {
    if (que.top() == 1) {
      break;
    }
    int a, b;
    a = que.top();
    que.pop();
    if (que.empty()) {
      cout << "1" << endl;
      return 0;
    }
    b = que.top();
    que.pop();
    a--;
    b--;
    que.push(a);
    if (b > 0) {
      que.push(b);
    }
  }

  cout << que.size() << endl;

  return 0;
}