Problem p03246 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define CEIL(a, b) ((a) / (b) + ((a) % (b) == 0 ? 0 : 1))
#define X first
#define Y second

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vl = vector<ll>;
using vs = vector<string>;

constexpr int MOD = int(1e9 + 7);
constexpr int INF = int(1e9 + 1);
constexpr ll LLINF = ll(4 * 1e18 + 1);
// constexpr int INF = 2147483647; // 2 * 1e9
// constexpr ll LLINF = 9223372036854775807; // 9 * 1e18

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

bool compy(pii a, pii b) { return a.second > b.second; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<pii> b(100001), c(100001);
  for (int i = 0; i < 100001; ++i) {
    b[i].second = i;
    c[i].second = i;
  }
  for (int i = 0; i < n; i += 2) {
    b[a[i]].first++;
  }
  for (int i = 1; i < n; i += 2) {
    c[a[i]].first++;
  }
  int m = 0, mm = 0;

  sort(begin(b), end(b), greater<pii>());
  sort(begin(c), end(c), greater<pii>());
  int ans = INF;
  if (b[0].second != c[0].second) {
    cout << n - b[0].first - c[0].first << "\n";
    return 0;
  }
  for (int i = 0; i < 100000; ++i) {

    if (b[i].second != c[i].second) {
      cout << n - b[i].first - c[i].first << "\n";
      return i;
    }
    if (b[i + 1].second != c[i].second) {
      ans = min(ans, n - b[i + 1].first - c[i].first);
    }
    if (b[i].second != c[i + 1].second) {
      ans = min(ans, n - b[i].first - c[i + 1].first);
    }
  }

  cout << ans << "\n";

  /*
          vector<int> q, r;
          for(auto && i : b){
                  if(m < i.second){
                          q = vector<int>();
                          m = max(m, i.second);
                          q.push_back(i.first);
                  }
                  else if(m == i.second){
                          q.push_back(i.first);
                  }
          }
          for(auto && i : c){
                  if(mm < i.second){
                          r = vector<int>();
                          mm = max(mm, i.second);
                          r.push_back(i.first);
                  }
                  else if(mm == i.second){
                          r.push_back(i.first);
                  }
          }

          for(auto && i : q){
                  for(auto && j : r){
                          if(q != r){
                                  cout << n - m - mm  << "\n";
                                  return 0;
                          }
                  }
          }

          int tm, tmm;

          for(auto && i : b){
                  if(m < i.second && ){
                          q = vector<int>();
                          m = max(m, i.second);
                          q.push_back(i.first);
                  }
                  else if(m == i.second){
                          q.push_back(i.first);
                  }
          }
          for(auto && i : c){
                  if(mm < i.second){
                          r = vector<int>();
                          mm = max(mm, i.second);
                          r.push_back(i.first);
                  }
                  else if(mm == i.second){
                          r.push_back(i.first);
                  }
          }

          cout << n - m - mm  << "\n";
          */
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define CEIL(a, b) ((a) / (b) + ((a) % (b) == 0 ? 0 : 1))
#define X first
#define Y second

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vl = vector<ll>;
using vs = vector<string>;

constexpr int MOD = int(1e9 + 7);
constexpr int INF = int(1e9 + 1);
constexpr ll LLINF = ll(4 * 1e18 + 1);
// constexpr int INF = 2147483647; // 2 * 1e9
// constexpr ll LLINF = 9223372036854775807; // 9 * 1e18

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

bool compy(pii a, pii b) { return a.second > b.second; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<pii> b(100001), c(100001);
  for (int i = 0; i < 100001; ++i) {
    b[i].second = i;
    c[i].second = i;
  }
  for (int i = 0; i < n; i += 2) {
    b[a[i]].first++;
  }
  for (int i = 1; i < n; i += 2) {
    c[a[i]].first++;
  }
  int m = 0, mm = 0;

  sort(begin(b), end(b), greater<pii>());
  sort(begin(c), end(c), greater<pii>());
  int ans = INF;
  if (b[0].second != c[0].second) {
    cout << n - b[0].first - c[0].first << "\n";
    return 0;
  }
  for (int i = 0; i < 100000; ++i) {

    if (b[i].second != c[i].second) {
      ans = min(ans, n - b[i].first - c[i].first);
    }
    if (b[i + 1].second != c[i].second) {
      ans = min(ans, n - b[i + 1].first - c[i].first);
    }
    if (b[i].second != c[i + 1].second) {
      ans = min(ans, n - b[i].first - c[i + 1].first);
    }
  }

  cout << ans << "\n";

  /*
          vector<int> q, r;
          for(auto && i : b){
                  if(m < i.second){
                          q = vector<int>();
                          m = max(m, i.second);
                          q.push_back(i.first);
                  }
                  else if(m == i.second){
                          q.push_back(i.first);
                  }
          }
          for(auto && i : c){
                  if(mm < i.second){
                          r = vector<int>();
                          mm = max(mm, i.second);
                          r.push_back(i.first);
                  }
                  else if(mm == i.second){
                          r.push_back(i.first);
                  }
          }

          for(auto && i : q){
                  for(auto && j : r){
                          if(q != r){
                                  cout << n - m - mm  << "\n";
                                  return 0;
                          }
                  }
          }

          int tm, tmm;

          for(auto && i : b){
                  if(m < i.second && ){
                          q = vector<int>();
                          m = max(m, i.second);
                          q.push_back(i.first);
                  }
                  else if(m == i.second){
                          q.push_back(i.first);
                  }
          }
          for(auto && i : c){
                  if(mm < i.second){
                          r = vector<int>();
                          mm = max(mm, i.second);
                          r.push_back(i.first);
                  }
                  else if(mm == i.second){
                          r.push_back(i.first);
                  }
          }

          cout << n - m - mm  << "\n";
          */
  return 0;
}