Problem p03244 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t) for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t) for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#ifndef DBG
#define trace(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  return b > a && (a = b, true);
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  return b < a && (a = b, true);
}
constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T> inline vector<T> make_v(const T &initValue, size_t sz) {
  return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
  return vector<decltype(make_v<T>(initValue, args...))>(
      sz, make_v<T>(initValue, args...));
}

template <class T> inline istream &operator>>(istream &in, vector<T> &vec) {
  for (auto &e : vec)
    in >> e;
  return in;
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail &...tail) {
  cin >> head;
  read(tail...);
}

inline void print() { cout << "\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail))
    cout << ' ';
  print(forward<Tail>(tail)...);
}

template <class T>
inline ostream &operator<<(ostream &out, const vector<T> &vec) {
  static constexpr const char *delim[] = {" ", ""};
  for (const auto &e : vec)
    out << e << delim[&e == &vec.back()];
  return out;
}
// }}} End Header

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  read(N);
  map<int, int> even, odd;

  rep(i, N) {
    int a;
    read(a);
    if (i & 1) {
      odd[a] += 1;
    } else {
      even[a] += 1;
    }
  }

  vector<pii> x, y;
  int xcnt = 0;
  int ycnt = 0;
  for (const auto &e : even) {
    x.emplace_back(e.second, e.first);
    xcnt += e.second;
  }
  for (const auto &e : odd) {
    y.emplace_back(e.second, e.first);
    ycnt += e.second;
  }
  x.emplace_back(0, -1);
  y.emplace_back(0, -1);
  sort(all(x), greater<>());
  sort(all(y), greater<>());

  i64 ans = INF;
  rep(i, x.size()) rep(j, y.size()) {
    const int numx = x[i].second;
    const int numy = y[j].second;
    if (numx == numy)
      continue;
    trace(x[i], y[j]);
    chmin(ans, (xcnt - x[i].first) + (ycnt - y[j].first));
  }

  print(ans);
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t) for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t) for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#ifndef DBG
#define trace(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  return b > a && (a = b, true);
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  return b < a && (a = b, true);
}
constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T> inline vector<T> make_v(const T &initValue, size_t sz) {
  return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
  return vector<decltype(make_v<T>(initValue, args...))>(
      sz, make_v<T>(initValue, args...));
}

template <class T> inline istream &operator>>(istream &in, vector<T> &vec) {
  for (auto &e : vec)
    in >> e;
  return in;
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail &...tail) {
  cin >> head;
  read(tail...);
}

inline void print() { cout << "\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail))
    cout << ' ';
  print(forward<Tail>(tail)...);
}

template <class T>
inline ostream &operator<<(ostream &out, const vector<T> &vec) {
  static constexpr const char *delim[] = {" ", ""};
  for (const auto &e : vec)
    out << e << delim[&e == &vec.back()];
  return out;
}
// }}} End Header

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  read(N);
  map<int, int> even, odd;

  rep(i, N) {
    int a;
    read(a);
    if (i & 1) {
      odd[a] += 1;
    } else {
      even[a] += 1;
    }
  }

  vector<pii> x, y;
  int xcnt = 0;
  int ycnt = 0;
  for (const auto &e : even) {
    x.emplace_back(e.second, e.first);
    xcnt += e.second;
  }
  for (const auto &e : odd) {
    y.emplace_back(e.second, e.first);
    ycnt += e.second;
  }
  x.emplace_back(0, -1);
  y.emplace_back(0, -1);
  sort(all(x), greater<>());
  sort(all(y), greater<>());

  i64 ans = INF;
  rep(i, 2) rep(j, 2) {
    const int numx = x[i].second;
    const int numy = y[j].second;
    if (numx == numy)
      continue;
    trace(x[i], y[j]);
    chmin(ans, (xcnt - x[i].first) + (ycnt - y[j].first));
  }

  print(ans);
  return 0;
}