Problem p03246 (C++)

Buggy submission (status: Runtime Error):
/*
TASK:  C - /\/\/\/
LANG: C++
*/
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
/*
unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 g1.seed(seed1);
//*/
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pii;

typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef map<int, int> mii;

template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define MOD7 1000000007
#define MOD9 1000000009
#define EPS 1e-9

#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define X first
#define Y second

#define mset(a, x) memset(a, x, sizeof(a))
#define P(x) cout << #x << " = " << x << endl;
#define P2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl;
#define PV(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cout << #a << "[" << i << "] = " << a[i] << endl;
#define TM(a, b)                                                               \
  cout << #a << "->" << #b << ": " << 1. * (b - a) / CLOCKS_PER_SEC << "s\n";

#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define for_each(it, X)                                                        \
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)

#define scanf_v(n, v)                                                          \
  {                                                                            \
    int tt;                                                                    \
    F0R(i, n) cin >> tt, v.pb(tt);                                             \
  }

#define fastio                                                                 \
  {                                                                            \
    ios::sync_with_stdio(false);                                               \
    cin.tie(NULL);                                                             \
  }
#define unique_v(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))

bitset<10000010> chk_p;
vi primes;
ll _sieve_size;
void gen_prime(ll nn) {
  _sieve_size = nn + 1;
  chk_p.set();
  chk_p[0] = chk_p[1] = 0;
  primes.pb(2);
  FOR(i, 4, _sieve_size) chk_p[i] = 0;
  for (ll i = 3; i < _sieve_size; i += 2)
    if (chk_p[i]) {
      for (ll j = i * i; j <= _sieve_size; j += i)
        chk_p[j] = 0;
      primes.pb((int)i);
    }
}
ll exp(ll x, ll y, ll mod) {
  if (y < 0)
    return 0;
  ll ret = 1;
  for (; y; y >>= 1, x = (x * x) % mod) {
    if (y & 1)
      ret = (ret * x) % mod;
  }
  return ret;
}
ll inv_mod(ll a, ll p) { return exp(a, p - 2, p); } // only p-prime
bool isprime(ll x) {
  if (x <= 1)
    return false;
  for (ll i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
ll phi(ll x) {
  ll t = x, ret = x, i;
  for (i = 2; i * i <= x; i++) {
    if (t % i == 0) {
      ret -= ret / i;
      while (t % i == 0)
        t /= i;
    }
  }
  if (t != 1)
    ret -= ret / t;
  return ret;
}

template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
template <typename T> T extgcd(T a, T b, T &x, T &y) {
  T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
  while (b) {
    T q = a / b;
    a %= b;
    swap(a, b);
    x0 -= q * x1;
    swap(x0, x1);
    y0 -= q * y1;
    swap(y0, y1);
  }
  x = x0;
  y = y0;
  return a;
}
static inline int ctz(unsigned x) { return __builtin_ctz(x); }
static inline int ctzll(ull x) { return __builtin_ctzll(x); }
static inline int clz(unsigned x) { return __builtin_clz(x); }
static inline int clzll(ull x) { return __builtin_clzll(x); }
static inline int popcnt(unsigned x) { return __builtin_popcount(x); }
static inline int popcntll(ull x) { return __builtin_popcountll(x); }
static inline int bsr(unsigned x) { return 31 ^ clz(x); }
static inline int bsrll(ull x) { return 63 ^ clzll(x); }

int N, M, T;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("xxx.in","r",stdin);
  // freopen("xxx.out","w",stdout);
  int i, j, k;
  int tt = 0;
  vi v;
  cin >> N;
  scanf_v(N, v);
  mii mm, xx;
  for (i = 0; i < N; i += 2)
    mm[v[i]]++;
  for (i = 1; i < N; i += 2)
    xx[v[i]]++;
  vii v1, v2;
  for (auto it = mm.begin(); it != mm.end(); it++)
    v1.pb({(*it).Y, (*it).X});
  v1.pb({0, 1000000});
  for (auto it = xx.begin(); it != xx.end(); it++)
    v2.pb({(*it).Y, (*it).X});
  v2.pb({0, 1000000});
  sort(allr(v1));
  sort(allr(v2));
  int Mc = 0;
  for (i = 0; i < v1.size(); i++) {
    for (j = 0; j < v2.size(); j++) {
      if (v2[j].Y != v1[i].Y) {
        Mc = max(Mc, v2[j].X + v1[i].X);
        break;
      }
    }
  }

  for (j = 0; j < v1.size(); j++) {
    for (i = 0; i < v2.size(); i++) {
      if (v2[j].Y != v1[i].Y) {
        Mc = max(Mc, v2[j].X + v1[i].X);
        break;
      }
    }
  }
  cout << N - Mc << '\n';
  return 0;
}


Fixed submission:
/*
TASK:  C - /\/\/\/
LANG: C++
*/
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
/*
unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 g1.seed(seed1);
//*/
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pii;

typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef map<int, int> mii;

template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define MOD7 1000000007
#define MOD9 1000000009
#define EPS 1e-9

#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define X first
#define Y second

#define mset(a, x) memset(a, x, sizeof(a))
#define P(x) cout << #x << " = " << x << endl;
#define P2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl;
#define PV(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cout << #a << "[" << i << "] = " << a[i] << endl;
#define TM(a, b)                                                               \
  cout << #a << "->" << #b << ": " << 1. * (b - a) / CLOCKS_PER_SEC << "s\n";

#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define for_each(it, X)                                                        \
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)

#define scanf_v(n, v)                                                          \
  {                                                                            \
    int tt;                                                                    \
    F0R(i, n) cin >> tt, v.pb(tt);                                             \
  }

#define fastio                                                                 \
  {                                                                            \
    ios::sync_with_stdio(false);                                               \
    cin.tie(NULL);                                                             \
  }
#define unique_v(a) a.resize(distance(a.begin(), unique(a.begin(), a.end())))

bitset<10000010> chk_p;
vi primes;
ll _sieve_size;
void gen_prime(ll nn) {
  _sieve_size = nn + 1;
  chk_p.set();
  chk_p[0] = chk_p[1] = 0;
  primes.pb(2);
  FOR(i, 4, _sieve_size) chk_p[i] = 0;
  for (ll i = 3; i < _sieve_size; i += 2)
    if (chk_p[i]) {
      for (ll j = i * i; j <= _sieve_size; j += i)
        chk_p[j] = 0;
      primes.pb((int)i);
    }
}
ll exp(ll x, ll y, ll mod) {
  if (y < 0)
    return 0;
  ll ret = 1;
  for (; y; y >>= 1, x = (x * x) % mod) {
    if (y & 1)
      ret = (ret * x) % mod;
  }
  return ret;
}
ll inv_mod(ll a, ll p) { return exp(a, p - 2, p); } // only p-prime
bool isprime(ll x) {
  if (x <= 1)
    return false;
  for (ll i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
ll phi(ll x) {
  ll t = x, ret = x, i;
  for (i = 2; i * i <= x; i++) {
    if (t % i == 0) {
      ret -= ret / i;
      while (t % i == 0)
        t /= i;
    }
  }
  if (t != 1)
    ret -= ret / t;
  return ret;
}

template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
template <typename T> T extgcd(T a, T b, T &x, T &y) {
  T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
  while (b) {
    T q = a / b;
    a %= b;
    swap(a, b);
    x0 -= q * x1;
    swap(x0, x1);
    y0 -= q * y1;
    swap(y0, y1);
  }
  x = x0;
  y = y0;
  return a;
}
static inline int ctz(unsigned x) { return __builtin_ctz(x); }
static inline int ctzll(ull x) { return __builtin_ctzll(x); }
static inline int clz(unsigned x) { return __builtin_clz(x); }
static inline int clzll(ull x) { return __builtin_clzll(x); }
static inline int popcnt(unsigned x) { return __builtin_popcount(x); }
static inline int popcntll(ull x) { return __builtin_popcountll(x); }
static inline int bsr(unsigned x) { return 31 ^ clz(x); }
static inline int bsrll(ull x) { return 63 ^ clzll(x); }

int N, M, T;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("xxx.in","r",stdin);
  // freopen("xxx.out","w",stdout);
  int i, j, k;
  int tt = 0;
  vi v;
  cin >> N;
  scanf_v(N, v);
  mii mm, xx;
  for (i = 0; i < N; i += 2)
    mm[v[i]]++;
  for (i = 1; i < N; i += 2)
    xx[v[i]]++;
  vii v1, v2;
  for (auto it = mm.begin(); it != mm.end(); it++)
    v1.pb({(*it).Y, (*it).X});
  v1.pb({0, 1000000});
  for (auto it = xx.begin(); it != xx.end(); it++)
    v2.pb({(*it).Y, (*it).X});
  v2.pb({0, 1000000});
  sort(allr(v1));
  sort(allr(v2));
  int Mc = 0;
  for (i = 0; i < v1.size(); i++) {
    for (j = 0; j < v2.size(); j++) {
      if (v2[j].Y != v1[i].Y) {
        Mc = max(Mc, v2[j].X + v1[i].X);
        break;
      }
    }
  }

  for (i = 0; i < v2.size(); i++) {
    for (j = 0; j < v1.size(); j++) {
      if (v2[i].Y != v1[j].Y) {
        Mc = max(Mc, v2[i].X + v1[j].X);
        break;
      }
    }
  }
  cout << N - Mc << '\n';
  return 0;
}