Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define INF 1e16
typedef pair<int, int> P;
typedef long long ll;

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first > b.first)
    return a.first > b.first;
  else {
    return a.second > b.second;
  }
}

int main(void) {
  int n;
  cin >> n;
  vector<int> odd, even, v(n);
  vector<pair<int, int>> oddnum, evennum;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    if (i % 2 == 0)
      even.push_back(v[i]);
    else
      odd.push_back(v[i]);
  }
  sort(even.begin(), even.end());
  sort(odd.begin(), odd.end());
  int i = 0;
  for (i = 0; i < (int)even.size();) {
    int j = i;
    while (true) {
      if (i == (int)even.size()) {
        pair<int, int> p = make_pair(i - j, even[j]);
        evennum.push_back(p);
        break;
      }
      if (even[j] == even[i])
        i++;
      else {
        pair<int, int> p = make_pair(i - j, even[j]);
        evennum.push_back(p);
        break;
      }
    }
  }
  for (i = 0; i < (int)odd.size();) {
    int j = i;
    while (true) {
      if (i == (int)odd.size()) {
        pair<int, int> p = make_pair(i - j, odd[j]);
        oddnum.push_back(p);
        break;
      }
      if (odd[j] == odd[i])
        i++;
      else {
        pair<int, int> p = make_pair(i - j, odd[j]);
        oddnum.push_back(p);
        break;
      }
    }
  }
  int ans = 10000000;
  sort(oddnum.begin(), oddnum.end(), cmp);
  sort(evennum.begin(), evennum.end(), cmp);
  int v1 = ((int)oddnum.size() == 1) ? 0 : oddnum[1].second;
  int v2 = ((int)oddnum.size() == 1) ? 0 : oddnum[1].first;
  int u1 = ((int)evennum.size() == 1) ? 0 : evennum[1].second;
  int u2 = ((int)evennum.size() == 1) ? 0 : evennum[1].first;
  if (oddnum[0].second != evennum[0].second)
    ans = n - oddnum[0].first - evennum[0].first;
  else {
    if (v1 == v2) {
      ans = min(ans, min(n / 2 - oddnum[0].first + n / 2 - u2,
                         n / 2 - v2 + n / 2 - evennum[0].first));
    } else {
      ans = min(ans, min(n / 2 - oddnum[0].first + n / 2 - u2,
                         n / 2 - v2 + n / 2 - evennum[0].first));
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define INF 1e16
typedef pair<int, int> P;
typedef long long ll;

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first != b.first)
    return a.first > b.first;
  else {
    return a.second > b.second;
  }
}

int main(void) {
  int n;
  cin >> n;
  vector<int> odd, even, v(n);
  vector<pair<int, int>> oddnum, evennum;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    if (i % 2 == 0)
      even.push_back(v[i]);
    else
      odd.push_back(v[i]);
  }
  sort(even.begin(), even.end());
  sort(odd.begin(), odd.end());
  int i = 0;
  for (i = 0; i < (int)even.size();) {
    int j = i;
    while (true) {
      if (i == (int)even.size()) {
        pair<int, int> p = make_pair(i - j, even[j]);
        evennum.push_back(p);
        break;
      }
      if (even[j] == even[i])
        i++;
      else {
        pair<int, int> p = make_pair(i - j, even[j]);
        evennum.push_back(p);
        break;
      }
    }
  }
  for (i = 0; i < (int)odd.size();) {
    int j = i;
    while (true) {
      if (i == (int)odd.size()) {
        pair<int, int> p = make_pair(i - j, odd[j]);
        oddnum.push_back(p);
        break;
      }
      if (odd[j] == odd[i])
        i++;
      else {
        pair<int, int> p = make_pair(i - j, odd[j]);
        oddnum.push_back(p);
        break;
      }
    }
  }
  int ans = 10000000;
  sort(oddnum.begin(), oddnum.end(), cmp);
  sort(evennum.begin(), evennum.end(), cmp);
  int v1 = ((int)oddnum.size() == 1) ? 0 : oddnum[1].second;
  int v2 = ((int)oddnum.size() == 1) ? 0 : oddnum[1].first;
  int u1 = ((int)evennum.size() == 1) ? 0 : evennum[1].second;
  int u2 = ((int)evennum.size() == 1) ? 0 : evennum[1].first;
  if (oddnum[0].second != evennum[0].second)
    ans = n - oddnum[0].first - evennum[0].first;
  else {
    if (v1 == v2) {
      ans = min(ans, min(n / 2 - oddnum[0].first + n / 2 - u2,
                         n / 2 - v2 + n / 2 - evennum[0].first));
    } else {
      ans = min(ans, min(n / 2 - oddnum[0].first + n / 2 - u2,
                         n / 2 - v2 + n / 2 - evennum[0].first));
    }
  }
  cout << ans << endl;
  return 0;
}