Problem p03244 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define eps 1e-7
#define For(i, a, b) for (int i = a; i <= b; i++)
#define Fore(i, a, b) for (int i = a; i >= b; i--)
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define mkp make_pair
#define pb push_back
#define sz size()
#define met(a, b) memset(a, b, sizeof(a))
#define iossy ios::sync_with_stdio(false)
#define fr freopen
#define pi acos(-1.0)
#define Vector Point
typedef pair<int, int> pii;
const long long linf = 1LL << 62;
const int iinf = 1000000009;
const double dinf = 1e17;
const int Mod = 1e9 + 9;
typedef long long ll;
typedef long double ld;
const int maxn = 1000005;
int n, ans;
int a[maxn];
int b[maxn];
int r1[maxn], r2[maxn];
int vs1[100005], vs2[100005];
int rt1, rt2;
bool cmp1(int a, int b) { return vs1[a] > vs1[b]; }
bool cmp2(int a, int b) { return vs2[a] > vs2[b]; }
int chk() {
  int cnt = 0;
  For(i, 3, n) b[i] = b[i - 2];
  For(i, 1, n) if (a[i] != b[i]) cnt++;
  return cnt;
}
void solve() {
  iossy;
  cin >> n;
  ans = iinf;
  met(vs1, 0);
  met(vs2, 0);
  rt1 = 0;
  rt2 = 0;
  For(i, 1, n) {
    cin >> a[i];
    if (i % 2) {
      vs1[a[i]]++;
      if (vs1[a[i]] == 1)
        r1[rt1++] = a[i];
    } else {
      vs2[a[i]]++;
      if (vs2[a[i]] == 1)
        r2[rt2++] = a[i];
    }
  }
  sort(r1, r1 + rt1, cmp1);
  sort(r2, r2 + rt2, cmp2);
  For(i, 0, min(200, rt1 - 1)) {
    For(j, 0, min(200, rt2 - 1)) {
      b[1] = r1[i];
      b[2] = r2[j];
      if (b[1] == b[2]) {
        if (vs1[b[1]] >= vs2[b[2]])
          b[2]++;
        else
          b[1]++;
      }
      ans = min(ans, chk());
    }
  }
  cout << ans << endl;
}
int main() {
  int t = 1;
  while (t--)
    solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define eps 1e-7
#define For(i, a, b) for (int i = a; i <= b; i++)
#define Fore(i, a, b) for (int i = a; i >= b; i--)
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define mkp make_pair
#define pb push_back
#define sz size()
#define met(a, b) memset(a, b, sizeof(a))
#define iossy ios::sync_with_stdio(false)
#define fr freopen
#define pi acos(-1.0)
#define Vector Point
typedef pair<int, int> pii;
const long long linf = 1LL << 62;
const int iinf = 1000000009;
const double dinf = 1e17;
const int Mod = 1e9 + 9;
typedef long long ll;
typedef long double ld;
const int maxn = 1000005;
int n, ans;
int a[maxn];
int b[maxn];
int r1[maxn], r2[maxn];
int vs1[100005], vs2[100005];
int rt1, rt2;
bool cmp1(int a, int b) { return vs1[a] > vs1[b]; }
bool cmp2(int a, int b) { return vs2[a] > vs2[b]; }
int chk() {
  int cnt = 0;
  For(i, 3, n) b[i] = b[i - 2];
  For(i, 1, n) if (a[i] != b[i]) cnt++;
  return cnt;
}
void solve() {
  iossy;
  cin >> n;
  ans = iinf;
  met(vs1, 0);
  met(vs2, 0);
  rt1 = 0;
  rt2 = 0;
  For(i, 1, n) {
    cin >> a[i];
    if (i % 2) {
      vs1[a[i]]++;
      if (vs1[a[i]] == 1)
        r1[rt1++] = a[i];
    } else {
      vs2[a[i]]++;
      if (vs2[a[i]] == 1)
        r2[rt2++] = a[i];
    }
  }
  sort(r1, r1 + rt1, cmp1);
  sort(r2, r2 + rt2, cmp2);
  For(i, 0, min(100, rt1 - 1)) {
    For(j, 0, min(100, rt2 - 1)) {
      b[1] = r1[i];
      b[2] = r2[j];
      if (b[1] == b[2]) {
        if (vs1[b[1]] >= vs2[b[2]])
          b[2]++;
        else
          b[1]++;
      }
      ans = min(ans, chk());
    }
  }
  cout << ans << endl;
}
int main() {
  int t = 1;
  while (t--)
    solve();
  return 0;
}