Problem p03246 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define int long long
#define double long double
#define endl "\n"
#define pb push_back
#define PI 3.1415926535897932384626433832795l
#define F first
#define S second
#define mp make_pair
#define f(i, n) for (int i = 0; i < n; i++)
#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
// #define sort(v)     sort((v).begin(),(v).end())
// #define rsort(v)    sort((v).rbegin(),(v).rend())
#define gcd(a, b) __gcd((a), (b))
#define minn(v) *min_element(v.begin(), v.end());
#define maxx(v) *max_element(v.begin(), v.end());
#define print(x) cout << (x) << endl;
#define sum(v) +x accumulate(v.begin(), v.end(), x);
typedef pair<int, int> pii;
typedef vector<int> vi;
// x.substr(start, length);
/*
// Create a vector containing n
//vectors of size m.
    vector<vector<int> > vec( n , vector<int> (m, 0));
*/
struct type {
  int k, i, j;
};
// to check if a no is prime or not?
bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}

// minimum subaaray sum of size k-sliding window approach
int findSubarray(int arr[], int n, int k) {
  int window_sum = 0;
  int min_window = INT_MAX;
  int last = 0;
  for (int i = 0; i < n; i++) {
    window_sum += arr[i];
    if (i + 1 >= k) {
      if (min_window > window_sum) {
        min_window = window_sum;
        last = i;
      }
      window_sum -= arr[i + 1 - k];
    }
  }
  int sum = 0;
  for (int i = last - k + 1; i <= last; i++) {
    sum += arr[i];
  }
  return sum;
}
// finds the next permutation to given sequence of nos
int nextpermutation(vi v) {
  vi t = v;
  // sort(t);
  int x = 0;
  while (true) {
    x++;
    if (t == v) {
      return x;
    }
    next_permutation(all(t));
  }
}
signed main() {
  fastio;
  // cout << fixed << setprecision(0);
  int n;
  cin >> n;
  vi a(n);
  f(i, n) cin >> a[i];
  map<int, int> m1, m2;
  f(i, n) {
    if (i % 2 == 0) {
      m1[a[i]] += 1;
    } else {
      m2[a[i]] += 1;
    }
  }
  vector<pii> v[2];
  for (auto x : m1)
    v[0].pb({x.S, x.F});
  for (auto x : m2)
    v[1].pb({x.S, x.F});
  for (int i = 0; i < 2; i++)
    sort(v[i].rbegin(), v[i].rend());
  if (v[0][0].S != v[1][0].S) {
    cout << n / 2 - v[0][0].F + n / 2 - v[1][0].F << endl;
  } else {
    int ans = n;
    for (int i = 0; i < (int)v[0].size(); i++) {
      for (int j = 0; j < (int)v[1].size(); j++) {
        int cur = n;
        if (v[0][i].S == v[1][j].S)
          cur -= max(v[0][i].F, v[1][j].F);
        else
          cur -= (v[0][i].F + v[1][j].F);
        ans = min(ans, cur);
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define int long long
#define double long double
#define endl "\n"
#define pb push_back
#define PI 3.1415926535897932384626433832795l
#define F first
#define S second
#define mp make_pair
#define f(i, n) for (int i = 0; i < n; i++)
#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
// #define sort(v)     sort((v).begin(),(v).end())
// #define rsort(v)    sort((v).rbegin(),(v).rend())
#define gcd(a, b) __gcd((a), (b))
#define minn(v) *min_element(v.begin(), v.end());
#define maxx(v) *max_element(v.begin(), v.end());
#define print(x) cout << (x) << endl;
#define sum(v) +x accumulate(v.begin(), v.end(), x);
typedef pair<int, int> pii;
typedef vector<int> vi;
// x.substr(start, length);
/*
// Create a vector containing n
//vectors of size m.
    vector<vector<int> > vec( n , vector<int> (m, 0));
*/
struct type {
  int k, i, j;
};
// to check if a no is prime or not?
bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}

// minimum subaaray sum of size k-sliding window approach
int findSubarray(int arr[], int n, int k) {
  int window_sum = 0;
  int min_window = INT_MAX;
  int last = 0;
  for (int i = 0; i < n; i++) {
    window_sum += arr[i];
    if (i + 1 >= k) {
      if (min_window > window_sum) {
        min_window = window_sum;
        last = i;
      }
      window_sum -= arr[i + 1 - k];
    }
  }
  int sum = 0;
  for (int i = last - k + 1; i <= last; i++) {
    sum += arr[i];
  }
  return sum;
}
// finds the next permutation to given sequence of nos
int nextpermutation(vi v) {
  vi t = v;
  // sort(t);
  int x = 0;
  while (true) {
    x++;
    if (t == v) {
      return x;
    }
    next_permutation(all(t));
  }
}
signed main() {
  fastio;
  // cout << fixed << setprecision(0);
  int n;
  cin >> n;
  vi a(n);
  f(i, n) cin >> a[i];
  map<int, int> m1, m2;
  f(i, n) {
    if (i % 2 == 0) {
      m1[a[i]] += 1;
    } else {
      m2[a[i]] += 1;
    }
  }
  vector<pii> v[2];
  for (auto x : m1)
    v[0].pb({x.S, x.F});
  for (auto x : m2)
    v[1].pb({x.S, x.F});
  for (int i = 0; i < 2; i++)
    sort(v[i].rbegin(), v[i].rend());
  if (v[0][0].S != v[1][0].S) {
    cout << n / 2 - v[0][0].F + n / 2 - v[1][0].F << endl;
  } else {
    int ans = n;
    for (int i = 0; i < min((int)v[0].size(), 5LL); i++) {
      for (int j = 0; j < min((int)v[1].size(), 5LL); j++) {
        int cur = n;
        if (v[0][i].S == v[1][j].S)
          cur -= max(v[0][i].F, v[1][j].F);
        else
          cur -= (v[0][i].F + v[1][j].F);
        ans = min(ans, cur);
      }
    }
    cout << ans << endl;
  }
}