prithivMLmods/Coder-Stat huggingface dataset:
Problem p03243 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define long long long

int main() {
  long a, b, c, n, x, y;

  while (true) {
    for (int i = 1; i <= 9; i++) {
      if (n / i == 111 && n % 1 == 0) {
        cout << n << endl;
        return 0;
      }
    }
    n++;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define long long long

int main() {
  long a, b, c, n, x, y;
  cin >> n;
  if (n <= 111) {
    cout << 111 << endl;
    return 0;
  } else if (n <= 222) {
    cout << 222 << endl;
    return 0;
  } else if (n <= 333) {
    cout << 333 << endl;
    return 0;
  } else if (n <= 444) {
    cout << 444 << endl;
    return 0;
  } else if (n <= 555) {
    cout << 555 << endl;
    return 0;
  } else if (n <= 666) {
    cout << 666 << endl;
    return 0;
  } else if (n <= 777) {
    cout << 777 << endl;
    return 0;
  } else if (n <= 888) {
    cout << 888 << endl;
    return 0;
  } else if (n <= 999) {
    cout << 999 << endl;
    return 0;
  }
  return 0;
}
Problem p03243 (C++)

Buggy submission (status: Runtime Error):
/**
 *	  Name: Ajay Subhash Jadhav
 *	  Institute: International Institute of Technology Hyderabad
 */

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
#define LL long long
#define inf 0x7fffffff
#define mod 1000000007
#define pb push_back
#define ppi pair<int, int>
#define vi vector<int>
#define vii vector<ppi>
#define vll vector<LL>
#define mp make_pair
using namespace std;

void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void inputread() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

bool check(int n) {
  int dig = n % 10;
  n = n / 10;
  while (n > 0) {
    int temp = (n % 10);
    n = n / 10;
    if (temp == dig)
      dig = temp;
    else
      return 0;
  }
  return 1;
}

int main() {

  fastio();
  inputread();
  int n, m, i, j, t;
  cin >> n;

  while (true) {
    if (check(n))
      break;
    n++;
  }
  cout << n;
  return 0;
}

Fixed submission:
/**
 *	  Name: Ajay Subhash Jadhav
 *	  Institute: International Institute of Technology Hyderabad
 */

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>
#define LL long long
#define inf 0x7fffffff
#define mod 1000000007
#define pb push_back
#define ppi pair<int, int>
#define vi vector<int>
#define vii vector<ppi>
#define vll vector<LL>
#define mp make_pair
using namespace std;

void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void inputread() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

bool check(int n) {
  int dig = n % 10;
  n = n / 10;
  while (n > 0) {
    int temp = (n % 10);
    n = n / 10;
    if (temp == dig)
      dig = temp;
    else
      return 0;
  }
  return 1;
}

int main() {

  fastio();
  int n, m, i, j, t;
  cin >> n;

  while (true) {
    if (check(n))
      break;
    n++;
  }
  cout << n;
  return 0;
}
Problem p03243 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {

  int n = 1;
  cin >> n;

  for (int i = 111; i <= 999; i += 111) {
    if (n <= i) {
      cout << i << endl;
      return i;
    }
  }

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {

  int n = 1;
  cin >> n;

  for (int i = 111; i <= 999; i += 111) {
    if (n <= i) {
      cout << i << endl;
      return 0;
    }
  }

  return 0;
}
Problem p03243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using i64 = int64_t;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep2(i, x, n) for (int i = x; i < n; ++i)
#define INF 1 << 29;

int main() {
  int n;
  cin >> n;

  rep2(i, n, 1000) {
    int d[3];
    int tmp = i;
    rep(j, 3) {
      d[i] = tmp / pow(10, 2 - j);
      tmp = tmp - d[i] * pow(10, 2 - j);
    }
    if (d[0] == d[1] && d[1] == d[2]) {
      cout << i << endl;
      break;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using i64 = int64_t;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep2(i, x, n) for (int i = x; i < n; ++i)
#define INF 1 << 29;

int main() {
  int n;
  cin >> n;

  rep2(i, n, 1000) {
    int d[3];
    int tmp = i;
    rep(j, 3) {
      d[j] = tmp / pow(10, 2 - j);
      tmp = tmp - d[j] * pow(10, 2 - j);
    }
    if (d[0] == d[1] && d[1] == d[2]) {
      cout << i << endl;
      break;
    }
  }

  return 0;
}
Problem p03243 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int x;

  for (scanf("%d", x); x < 1000; x++) {
    char s[4];
    sprintf(s, "%d", x);
    if (s[0] == s[1] && s[1] == s[2])
      return puts(s), 0;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int x;

  for (scanf("%d", &x); x < 1000; x++) {
    char s[4];
    sprintf(s, "%d", x);
    if (s[0] == s[1] && s[1] == s[2])
      return puts(s), 0;
  }
  return 0;
}
Problem p03243 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
print(min([x for x in [int(str(i) * 3) - N for i in range(1, 10)] if x > 0]) + N)


Fixed submission:
N = int(input())
print(min([x for x in [int(str(i) * 3) - N for i in range(1, 10)] if x >= 0]) + N)
Problem p03243 (Python)

Buggy submission (status: Runtime Error):
n = input()
for ans in range(111, 1000, 111):
    if n <= ans:
        break
print(ans)


Fixed submission:
n = input()
for ans in range(111, 1000, 111):
    if int(n) <= ans:
        break
print(ans)
Problem p03243 (Python)

Buggy submission (status: Runtime Error):
n = input()
x = (n - 1) // 111 + 1
print(x * 111)


Fixed submission:
n = int(input())
x = (n - 1) // 111 + 1
print(x * 111)
Problem p03243 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int a, b;
  cin >> a;
  b = (a - 1) / 111;
  cout << 111 * (1 + b);
  return 111 * (1 + b);
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int a, b;
  cin >> a;
  b = (a - 1) / 111;
  cout << 111 * (b + 1);
  return 0;
}
Problem p03244 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
typedef long long ll;
const int maxn = 40;
pii _1[100010];
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif // LOCAL
  ios::sync_with_stdio(0);
  int n, tmp;
  scanf("%d", &n);
  map<int, int> cnt1, cnt2;
  for (int i = 0; i < n; i++) {
    scanf("%d", &tmp);
    if (i & 1)
      cnt1[tmp]++;
    else
      cnt2[tmp]++;
  }
  cnt1[-1000] = 0;
  cnt2[-1000] = 0;
  tmp = 0;
  for (auto a : cnt1)
    _1[tmp++] = make_pair(a.second, a.first);
  sort(_1, _1 + tmp);
  int maxn = 0;
  for (auto a : cnt2) {
    for (int i = tmp - 1; i >= 0; i--) {
      if (_1[i].second != a.first) {
        maxn = max(maxn, _1[i].first + a.second);
      }
    }
  }
  printf("%d", n - maxn);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
typedef long long ll;
const int maxn = 40;
pii _1[100010];
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif // LOCAL
  ios::sync_with_stdio(0);
  int n, tmp;
  scanf("%d", &n);
  map<int, int> cnt1, cnt2;
  for (int i = 0; i < n; i++) {
    scanf("%d", &tmp);
    if (i & 1)
      cnt1[tmp]++;
    else
      cnt2[tmp]++;
  }
  cnt1[-1000] = 0;
  cnt2[-1000] = 0;
  tmp = 0;
  for (auto a : cnt1)
    _1[tmp++] = make_pair(a.second, a.first);
  sort(_1, _1 + tmp);
  int maxn = 0;
  for (auto a : cnt2) {
    for (int i = tmp - 1; i >= 0; i--) {
      if (_1[i].second != a.first) {
        maxn = max(maxn, _1[i].first + a.second);
        break;
      }
    }
  }
  printf("%d", n - maxn);
  return 0;
}
Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mx 10005
using namespace std;

int main() {
  int n, s, m[2][2], v[2][mx];
  scanf("%d", &n);
  for (int i = 0; i < mx; i++)
    v[0][i] = v[1][i] = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &s);
    v[i & 1][s]++;
  }
  for (int i = 0; i < 4; i++)
    m[i & 1][i >> 1] = !(i >> 1);
  for (int i = 2; i < mx; i++) {
    for (int j = 0; j < 2; j++) {
      if (v[j][m[j][0]] < v[j][i]) {
        m[j][1] = m[j][0];
        m[j][0] = i;
      } else if (v[j][m[j][1]] < v[j][i])
        m[j][1] = i;
    }
  }
  int t = (m[0][0] == m[1][0] ? max(v[0][m[0][1]] + v[1][m[1][0]],
                                    v[1][m[1][1]] + v[0][m[0][0]])
                              : v[0][m[0][0]] + v[1][m[1][0]]);
  printf("%d\n", n - t);
}


Fixed submission:
#include <bits/stdc++.h>
#define mx 100005
using namespace std;

int main() {
  int n, s, m[2][2], v[2][mx];
  scanf("%d", &n);
  for (int i = 0; i < mx; i++)
    v[0][i] = v[1][i] = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &s);
    v[i & 1][s]++;
  }
  for (int i = 0; i < 4; i++)
    m[i & 1][i >> 1] = !(i >> 1);
  for (int i = 2; i < mx; i++) {
    for (int j = 0; j < 2; j++) {
      if (v[j][m[j][0]] < v[j][i]) {
        m[j][1] = m[j][0];
        m[j][0] = i;
      } else if (v[j][m[j][1]] < v[j][i])
        m[j][1] = i;
    }
  }
  int t = (m[0][0] == m[1][0] ? max(v[0][m[0][1]] + v[1][m[1][0]],
                                    v[1][m[1][1]] + v[0][m[0][0]])
                              : v[0][m[0][0]] + v[1][m[1][0]]);
  printf("%d\n", n - t);
}
Problem p03244 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> ve(100000), vo(100000);
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    if (i % 2) {
      vo.at(k).first++;
      vo.at(k).second = k;
    } else {
      ve.at(k).first++;
      ve.at(k).second = k;
    }
  }

  sort(vo.begin(), vo.end());
  reverse(vo.begin(), vo.end());
  sort(ve.begin(), ve.end());
  reverse(ve.begin(), ve.end());

  int sum = vo.at(0).first + ve.at(0).first;
  if (vo.at(0).second == ve.at(0).second) {
    int s = vo.at(0).first + ve.at(1).first;
    int t = vo.at(1).first + ve.at(0).first;
    if (s > t)
      sum = s;
    else
      sum = t;
  }

  cout << n - sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> ve(1000000), vo(1000000);
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    if (i % 2) {
      vo.at(k).first++;
      vo.at(k).second = k;
    } else {
      ve.at(k).first++;
      ve.at(k).second = k;
    }
  }

  sort(vo.begin(), vo.end());
  reverse(vo.begin(), vo.end());
  sort(ve.begin(), ve.end());
  reverse(ve.begin(), ve.end());

  int sum = vo.at(0).first + ve.at(0).first;
  if (vo.at(0).second == ve.at(0).second) {
    int s = vo.at(0).first + ve.at(1).first;
    int t = vo.at(1).first + ve.at(0).first;
    if (s > t)
      sum = s;
    else
      sum = t;
  }

  cout << n - sum << endl;
}
Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lint = long long;

signed main() {
  lint N;
  cin >> N;
  vector<lint> a(N), num(10010), num1(100010);
  for (lint i = 0; i < N; i++) {
    cin >> a[i];
    if (i % 2 == 0)
      num[a[i]]++;
    else
      num1[a[i]]++;
  }
  lint p = max_element(num.begin(), num.end()) - num.begin();
  lint q = max_element(num1.begin(), num1.end()) - num1.begin();
  lint ans;
  if (p != q) {
    ans = N - num[p] - num1[q];
  }
  if (p == q) {
    sort(num.begin(), num.end(), greater<lint>());
    sort(num1.begin(), num1.end(), greater<lint>());
    ans = min(N - num[0] - num1[1], N - num[1] - num1[0]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long;

signed main() {
  lint N;
  cin >> N;
  vector<lint> a(N), num(100010), num1(100010);
  for (lint i = 0; i < N; i++) {
    cin >> a[i];
    if (i % 2 == 0)
      num[a[i]]++;
    else
      num1[a[i]]++;
  }
  lint p = max_element(num.begin(), num.end()) - num.begin();
  lint q = max_element(num1.begin(), num1.end()) - num1.begin();
  lint ans;
  if (p != q) {
    ans = N - num[p] - num1[q];
  }
  if (p == q) {
    sort(num.begin(), num.end(), greater<lint>());
    sort(num1.begin(), num1.end(), greater<lint>());
    ans = min(N - num[0] - num1[1], N - num[1] - num1[0]);
  }
  cout << ans << endl;
}
Problem p03244 (C++)

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

int main() {
  int n;
  cin >> n;

  vector<int> v(n);
  for (int i = 1; i <= n; i++)
    cin >> v[i];

  const int N = 100010;

  vector<int> o(N);
  vector<int> e(N);
  int o_maxNum_first = 0;
  int o_maxNum_second = 0;
  int o_maxValue;
  int e_maxNum_first = 0;
  int e_maxNum_second = 0;
  int e_maxValue;

  for (int i = 1; i <= n; i++) {
    if (i % 2 == 0) {
      e[v[i]]++;
    } else {
      o[v[i]]++;
    }
  }

  for (int j = 0; j < N; j++) {

    if (o_maxNum_first <= o[j]) {

      o_maxNum_second = o_maxNum_first;
      o_maxNum_first = o[j];
      o_maxValue = j;

    } else if (o_maxNum_second <= o[j]) {

      o_maxNum_second = o[j];
    }

    if (e_maxNum_first <= e[j]) {

      e_maxNum_second = e_maxNum_first;
      e_maxNum_first = e[j];
      e_maxValue = j;

    } else if (e_maxNum_second <= e[j]) {

      e_maxNum_second = e[j];
    }
  }

  int ans;

  if (o_maxValue != e_maxValue) {

    ans = n - o_maxNum_first - e_maxNum_first;

  } else { //  o_maxValue == e_maxValue

    ans = min(n - o_maxNum_first - e_maxNum_second,
              n - e_maxNum_first - o_maxNum_second);
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;

  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i];

  const int N = 100010;

  vector<int> o(N);
  vector<int> e(N);
  int o_maxNum_first = 0;
  int o_maxNum_second = 0;
  int o_maxValue;
  int e_maxNum_first = 0;
  int e_maxNum_second = 0;
  int e_maxValue;

  for (int i = 1; i <= n; i++) {
    if (i % 2 == 0) {
      e[v[i]]++;
    } else {
      o[v[i]]++;
    }
  }

  for (int j = 0; j < N; j++) {

    if (o_maxNum_first <= o[j]) {

      o_maxNum_second = o_maxNum_first;
      o_maxNum_first = o[j];
      o_maxValue = j;

    } else if (o_maxNum_second <= o[j]) {

      o_maxNum_second = o[j];
    }

    if (e_maxNum_first <= e[j]) {

      e_maxNum_second = e_maxNum_first;
      e_maxNum_first = e[j];
      e_maxValue = j;

    } else if (e_maxNum_second <= e[j]) {

      e_maxNum_second = e[j];
    }
  }

  int ans;

  if (o_maxValue != e_maxValue) {

    ans = n - o_maxNum_first - e_maxNum_first;

  } else { //  o_maxValue == e_maxValue

    ans = min(n - o_maxNum_first - e_maxNum_second,
              n - e_maxNum_first - o_maxNum_second);
  }

  cout << ans << endl;
  return 0;
}
Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int v_num = 10;

int main() {
  int n, v;
  cin >> n;
  vector<int> even(v_num, 0), odd(v_num + 1, 0);
  for (int i = 0; i < n; ++i) {
    cin >> v;
    if (i % 2 == 0)
      even[v]++;
    else
      odd[v]++;
  }

  bool same = false;
  vector<int>::iterator evenMaxIt = max_element(even.begin(), even.end());
  vector<int>::iterator oddMaxIt = max_element(odd.begin(), odd.end());
  if (distance(even.begin(), evenMaxIt) == distance(odd.begin(), oddMaxIt))
    same = true;

  sort(even.begin(), even.end(), greater<int>());
  sort(odd.begin(), odd.end(), greater<int>());
  if (same) {
    if (even[0] == odd[0]) {
      if (even[1] < odd[1]) {
        cout << n - even[0] - odd[1] << endl;
      } else {
        cout << n - even[1] - odd[0] << endl;
      }
    } else if (even[0] > odd[0]) {
      cout << n - even[0] - odd[1] << endl;
    } else {
      cout << n - even[1] - odd[0] << endl;
    }
  } else {
    cout << n - even[0] - odd[0] << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int v_num = pow(10, 5) + 1;

int main() {
  int n, v;
  cin >> n;
  vector<int> even(v_num, 0), odd(v_num + 1, 0);
  for (int i = 0; i < n; ++i) {
    cin >> v;
    if (i % 2 == 0)
      even[v]++;
    else
      odd[v]++;
  }

  bool same = false;
  vector<int>::iterator evenMaxIt = max_element(even.begin(), even.end());
  vector<int>::iterator oddMaxIt = max_element(odd.begin(), odd.end());
  if (distance(even.begin(), evenMaxIt) == distance(odd.begin(), oddMaxIt))
    same = true;

  sort(even.begin(), even.end(), greater<int>());
  sort(odd.begin(), odd.end(), greater<int>());
  if (same) {
    if (even[0] == odd[0]) {
      if (even[1] < odd[1]) {
        cout << n - even[0] - odd[1] << endl;
      } else {
        cout << n - even[1] - odd[0] << endl;
      }
    } else if (even[0] > odd[0]) {
      cout << n - even[0] - odd[1] << endl;
    } else {
      cout << n - even[1] - odd[0] << endl;
    }
  } else {
    cout << n - even[0] - odd[0] << endl;
  }

  return 0;
}
Problem p03244 (Python)

Buggy submission (status: Runtime Error):
# coding:utf-8
import collections


def main():
    n = int(input())
    v = list(map(int, input().split()))

    va = []
    vb = []
    for i in range(0, n, 2):
        va.append(v[i])
        vb.append(v[i + 1])

    c1mc = collections.Counter(va).most_common()
    c2mc = collections.Counter(vb).most_common()

    if c1mc[0][0] != c2mc[0][0]:
        print(n - c1mc[0][1] - c2mc[0][1])
        return

    else:
        a = n - c1mc[1][1] - c2mc[0][1]
        b = n - c1mc[0][1] - c2mc[1][1]

        if a < b:
            print(a)
        else:
            print(b)


if __name__ == "__main__":
    main()


Fixed submission:
# coding:utf-8
import collections


def main():
    n = int(input())
    v = list(map(int, input().split()))

    va = []
    vb = []
    for i in range(0, n, 2):
        va.append(v[i])
        vb.append(v[i + 1])

    if va == vb and len(set(va)) == 1:
        print(len(va))
        return

    c1mc = collections.Counter(va).most_common()
    c2mc = collections.Counter(vb).most_common()

    if c1mc[0][0] != c2mc[0][0]:
        print(n - c1mc[0][1] - c2mc[0][1])
        return

    else:
        a = n - c1mc[1][1] - c2mc[0][1]
        b = n - c1mc[0][1] - c2mc[1][1]

        if a < b:
            print(a)
        else:
            print(b)


if __name__ == "__main__":
    main()
Problem p03244 (Python)

Buggy submission (status: Runtime Error):
import numpy as np

even = np.zeros(100000, dtype=int)
odd = np.zeros(100000, dtype=int)

n = int(input())
v = list(map(int, input().split()))

for i in range(n):
    if i % 2 == 0:
        even[v[i]] += 1
    else:
        odd[v[i]] += 1

e1 = even.argmax()
e1n = even.max()
o1 = odd.argmax()
o1n = odd.max()

# print(e1, e1n, o1, o1n)
if e1 == o1:
    e2n = np.sort(even)[-2]
    o2n = np.sort(odd)[-2]
    # print(e2n, o2n)
    print(min(n - e1n - o2n, n - e2n - o1n))
else:
    print(n - e1n - o1n)


Fixed submission:
import numpy as np

even = np.zeros(100001, dtype=int)
odd = np.zeros(100001, dtype=int)

n = int(input())
v = list(map(int, input().split()))

for i in range(n):
    if i % 2 == 0:
        even[v[i]] += 1
    else:
        odd[v[i]] += 1

e1 = even.argmax()
e1n = even.max()
o1 = odd.argmax()
o1n = odd.max()

# print(e1, e1n, o1, o1n)
if e1 == o1:
    e2n = np.sort(even)[-2]
    o2n = np.sort(odd)[-2]
    # print(e2n, o2n)
    print(min(n - e1n - o2n, n - e2n - o1n))
else:
    print(n - e1n - o1n)
Problem p03244 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

n = int(input())
v = list(map(int, input().split()))

if len(set(v)) != 1:
    a = Counter(v[0::2]).most_common()
    b = Counter(v[1::2]).most_common()

    if a[0] != b[0]:
        q = (len(v[0::2]) - a[0][1]) + (len(v[1::2]) - b[0][1])
        w = (len(v[0::2]) - a[0][1]) + (len(v[1::2]) - b[1][1])
        e = (len(v[0::2]) - a[1][1]) + (len(v[1::2]) - b[0][1])
        print(min(q, w, e))
else:
    print(len(v[1::2]))


Fixed submission:
from collections import Counter

n = int(input())
v = list(map(int, input().split()))

if len(set(v)) != 1:
    a = Counter(v[0::2]).most_common()
    b = Counter(v[1::2]).most_common()
    if a[0][0] != b[0][0]:
        print((len(v[0::2]) - a[0][1]) + (len(v[1::2]) - b[0][1]))
    else:
        if a[0][1] < b[0][1]:
            print((len(v[0::2]) - a[0][1]) + (len(v[1::2]) - b[1][1]))
        elif a[0][1] > b[0][1]:
            print((len(v[0::2]) - a[1][1]) + (len(v[1::2]) - b[0][1]))
        elif a[0][1] == b[0][1]:
            if len(a) != 1 and len(b) != 1:
                if a[1][1] > b[1][1]:
                    print((len(v[0::2]) - a[1][1]) + (len(v[1::2]) - b[0][1]))
                elif a[1][1] < b[1][1]:
                    print((len(v[0::2]) - a[0][1]) + (len(v[1::2]) - b[1][1]))
                elif a[1][1] == b[1][1]:
                    print((len(v[0::2]) - a[0][1]) + (len(v[1::2]) - b[1][1]))
            elif len(a) != 1:
                print((len(v[0::2]) - a[1][1]) + (len(v[1::2]) - b[0][1]))
            elif len(b) != 1:
                print((len(v[0::2]) - a[0][1]) + (len(v[1::2]) - b[1][1]))
else:
    print(len(v[1::2]))
Problem p03244 (Python)

Buggy submission (status: Runtime Error):
import collections


def solve(string):
    n, *v = map(int, string.split())
    if len(set(v)) == 1:
        return str(n // 2)
    else:
        odds = v[1::2]
        _o = collections.Counter(odds)
        r_o = {str(v): k for k, v in _o.items()}
        evens = v[::2]
        _e = collections.Counter(evens)
        r_e = {str(v): k for k, v in _e.items()}
        ans1 = 0
        tmp = max(_o.values())
        ans1 += len(odds) - tmp
        key = r_o[str(tmp)]
        if key in _e.keys():
            _e[key] = 0
        ans1 += len(evens) - max(_e.values())

        ans2 = 0
        _o = collections.Counter(odds)
        r_o = {str(v): k for k, v in _o.items()}
        _e = collections.Counter(evens)
        r_e = {str(v): k for k, v in _e.items()}
        tmp = max(_e.values())
        ans2 += len(evens) - tmp
        key = r_e[str(tmp)]
        if key in _o.keys():
            _o[key] = 0
        ans2 += len(evens) - max(_o.values())

        return str(min(ans1, ans2))


if __name__ == "__main__":
    print(solve(input()))


Fixed submission:
import collections


def solve(string):
    n, *v = map(int, string.split())
    if len(set(v)) == 1:
        return str(n // 2)
    else:
        odds = v[1::2]
        _o = collections.Counter(odds)
        r_o = {str(v): k for k, v in _o.items()}
        evens = v[::2]
        _e = collections.Counter(evens)
        r_e = {str(v): k for k, v in _e.items()}
        ans1 = 0
        tmp = max(_o.values())
        ans1 += len(odds) - tmp
        key = r_o[str(tmp)]
        if key in _e.keys():
            _e[key] = 0
        ans1 += len(evens) - max(_e.values())

        ans2 = 0
        _o = collections.Counter(odds)
        r_o = {str(v): k for k, v in _o.items()}
        _e = collections.Counter(evens)
        r_e = {str(v): k for k, v in _e.items()}
        tmp = max(_e.values())
        ans2 += len(evens) - tmp
        key = r_e[str(tmp)]
        if key in _o.keys():
            _o[key] = 0
        ans2 += len(evens) - max(_o.values())

        return str(min(ans1, ans2))


if __name__ == "__main__":
    print(solve("\n".join([input(), input()])))
Problem p03244 (Python)

Buggy submission (status: Time Limit Exceeded):
import collections

N = int(input())

even = collections.defaultdict(lambda: 0)
odd = collections.defaultdict(lambda: 0)

for i, a in enumerate(map(int, input().split())):
    if i % 2 == 0:
        even[a] += 1
    else:
        odd[a] += 1

if (
    len(even.keys()) == 1
    and len(odd.keys()) == 1
    and list(even.keys())[0] == list(odd.keys())[0]
):
    print(N // 2)
else:
    m = 0
    for e_k, e_v in even.items():
        for o_k, o_v in odd.items():
            if e_k == o_k:
                continue
            m = max(m, e_v + o_v)

    print(N - m)


Fixed submission:
import collections

N = int(input())

even = collections.defaultdict(lambda: 0)
odd = collections.defaultdict(lambda: 0)

for i, a in enumerate(map(int, input().split())):
    if i % 2 == 0:
        even[a] += 1
    else:
        odd[a] += 1

if (
    len(even.keys()) == 1
    and len(odd.keys()) == 1
    and list(even.keys())[0] == list(odd.keys())[0]
):
    print(N // 2)
else:
    m = 0
    for e_k, e_v in sorted(even.items(), key=lambda x: x[1], reverse=True)[:2]:
        for o_k, o_v in sorted(odd.items(), key=lambda x: x[1], reverse=True)[:2]:
            if e_k == o_k:
                continue
            m = max(m, e_v + o_v)

    print(N - m)
Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAX 10

int main(void) {
  int n, i, buf;
  cin >> n;
  vector<int> vec1(MAX), vec2(MAX);
  for (i = 0; i < MAX; i++) {
    vec1.at(i) = 0;
    vec2.at(i) = 0;
  }
  for (i = 0; i < n; i++) {
    cin >> buf;
    if (i % 2 == 0) {
      vec1.at(buf - 1)++;
    } else {
      vec2.at(buf - 1)++;
    }
  }
  int m1 = 0, m2 = 0;
  for (i = 0; i < MAX; i++) {
    if (vec1.at(i) > vec1.at(m1)) {
      m1 = i;
    }
    if (vec2.at(i) > vec2.at(m2)) {
      m2 = i;
    }
  }

  sort(vec1.begin(), vec1.end());
  sort(vec2.begin(), vec2.end());
  reverse(vec1.begin(), vec1.end());
  reverse(vec2.begin(), vec2.end());

  int ans;
  if (m1 != m2) {
    ans = n - vec1.at(0) - vec2.at(0);
  } else {
    ans = min(n - vec1.at(1) - vec2.at(0), n - vec1.at(0) - vec2.at(1));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAX 100000

int main(void) {
  int n, i, buf;
  cin >> n;
  vector<int> vec1(MAX), vec2(MAX);
  for (i = 0; i < MAX; i++) {
    vec1.at(i) = 0;
    vec2.at(i) = 0;
  }
  for (i = 0; i < n; i++) {
    cin >> buf;
    if (i % 2 == 0) {
      vec1.at(buf - 1)++;
    } else {
      vec2.at(buf - 1)++;
    }
  }
  int m1 = 0, m2 = 0;
  for (i = 0; i < MAX; i++) {
    if (vec1.at(i) > vec1.at(m1)) {
      m1 = i;
    }
    if (vec2.at(i) > vec2.at(m2)) {
      m2 = i;
    }
  }

  sort(vec1.begin(), vec1.end());
  sort(vec2.begin(), vec2.end());
  reverse(vec1.begin(), vec1.end());
  reverse(vec2.begin(), vec2.end());

  int ans;
  if (m1 != m2) {
    ans = n - vec1.at(0) - vec2.at(0);
  } else {
    ans = min(n - vec1.at(1) - vec2.at(0), n - vec1.at(0) - vec2.at(1));
  }
  cout << ans << endl;
  return 0;
}
Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

const int N = 5;

signed main() {
  int n;
  cin >> n;

  vector<int> a(n);
  vector<int> odd(N), even(N);
  int odd_max = 0, oddv = 0;
  int even_max = 0, evenv = 0;
  rep(i, n) {
    cin >> a[i];
    if (i % 2 == 0) {
      even[a[i]]++;
      if (even[a[i]] > even_max) {
        even_max = even[a[i]];
        evenv = a[i];
      }
    } else {
      odd[a[i]]++;
      if (odd[a[i]] > odd_max) {
        odd_max = odd[a[i]];
        oddv = a[i];
      }
    }
  }

  int answer = n + 1;
  // SET EVEN
  for (int i = 0; i < N; i++) {
    if (i == evenv)
      continue;

    int tmp = n - even_max - odd[i];
    answer = min(answer, tmp);
  }
  // SET ODD
  for (int i = 0; i < N; i++) {
    if (i == oddv)
      continue;

    int tmp = n - odd_max - even[i];
    answer = min(answer, tmp);
  }

  cout << answer << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

const int N = 100010;

signed main() {
  int n;
  cin >> n;

  vector<int> a(n);
  vector<int> odd(N), even(N);
  int odd_max = 0, oddv = 0;
  int even_max = 0, evenv = 0;
  rep(i, n) {
    cin >> a[i];
    if (i % 2 == 0) {
      even[a[i]]++;
      if (even[a[i]] > even_max) {
        even_max = even[a[i]];
        evenv = a[i];
      }
    } else {
      odd[a[i]]++;
      if (odd[a[i]] > odd_max) {
        odd_max = odd[a[i]];
        oddv = a[i];
      }
    }
  }

  int answer = n + 1;
  // SET EVEN
  for (int i = 0; i < N; i++) {
    if (i == evenv)
      continue;

    int tmp = n - even_max - odd[i];
    answer = min(answer, tmp);
  }
  // SET ODD
  for (int i = 0; i < N; i++) {
    if (i == oddv)
      continue;

    int tmp = n - odd_max - even[i];
    answer = min(answer, tmp);
  }

  cout << answer << endl;

  return 0;
}
Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  const int LARGE = 10010;
  int N;
  cin >> N;
  vector<int> v(N);
  for (int i = 0; i < N; i++)
    cin >> v[i];

  int ans = 0;

  vector<int> even(LARGE, 0);
  vector<int> odd(LARGE, 0);

  for (int i = 0; i < N; i++) {
    if (i % 2 == 0)
      even[v[i]]++;
    else
      odd[v[i]]++;
  }

  int even_max = 0, odd_max = 0, even_max2 = 0, odd_max2 = 0;
  for (int i = 0; i < LARGE; i++) {
    if (even[even_max] < even[i]) {
      even_max2 = even_max;
      even_max = i;
    } else if (even[even_max2] < even[i]) {
      even_max2 = i;
    }
    if (odd[odd_max] < odd[i]) {
      odd_max2 = odd_max;
      odd_max = i;
    } else if (odd[odd_max2] < odd[i]) {
      odd_max2 = i;
    }
  }

  if (even_max == odd_max) {
    cout << N - max(even[even_max] + odd[odd_max2],
                    even[even_max2] + odd[odd_max])
         << endl;
  } else {
    cout << N - even[even_max] - odd[odd_max] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  const int LARGE = 100010;
  int N;
  cin >> N;
  vector<int> v(N);
  for (int i = 0; i < N; i++)
    cin >> v[i];

  int ans = 0;

  vector<int> even(LARGE, 0);
  vector<int> odd(LARGE, 0);

  for (int i = 0; i < N; i++) {
    if (i % 2 == 0)
      even[v[i]]++;
    else
      odd[v[i]]++;
  }

  int even_max = 0, odd_max = 0, even_max2 = 0, odd_max2 = 0;
  for (int i = 0; i < LARGE; i++) {
    if (even[even_max] < even[i]) {
      even_max2 = even_max;
      even_max = i;
    } else if (even[even_max2] < even[i]) {
      even_max2 = i;
    }
    if (odd[odd_max] < odd[i]) {
      odd_max2 = odd_max;
      odd_max = i;
    } else if (odd[odd_max2] < odd[i]) {
      odd_max2 = i;
    }
  }

  if (even_max == odd_max) {
    cout << N - max(even[even_max] + odd[odd_max2],
                    even[even_max2] + odd[odd_max])
         << endl;
  } else {
    cout << N - even[even_max] - odd[odd_max] << endl;
  }
}
Problem p03244 (C++)

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

using namespace std;

int main() {
  int N;
  int odd[100001];
  int even[100001];
  int v[100001];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> v[i];
  }

  for (int i = 1; i < N; i += 2) {
    odd[v[i]]++;
    even[v[i + 1]]++;
  }

  int omax = 0, emax = 0, omax2 = 0, emax2 = 0;
  for (int i = 0; i < 100001; i++) {
    if (omax < odd[i]) {
      omax = odd[i];
      omax2 = i;
    }
    if (emax < even[i]) {
      emax = even[i];
      emax2 = i;
    }
  }

  if (omax2 != emax2) {
    cout << (N - omax - emax) << endl;
  } else {
    int osec = 0, esec = 0;
    for (int i = 0; i < 100001; i++) {
      // なるほど、ほんとの最大値は引っかからないようにif文を設定して最大値をもう一度求めることで二番目の最大値を求めれるというわけか
      if (osec <= odd[i] && i != omax2) {
        osec = odd[i];
      }
      if (esec < even[i] && i != emax2) {
        esec = even[i];
      }
    }
    cout << min((N - omax - esec), (N - emax - osec));
  }
  return 0;
}

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

using namespace std;

int main() {
  int N;
  int odd[100001];
  int even[100001];
  int v[100001];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> v[i];
  }

  for (int i = 1; i < N; i += 2) {
    odd[v[i]]++;
    even[v[i ^ 1]]++;
  }

  int omax = 0, emax = 0, omax2 = 0, emax2 = 0;
  for (int i = 0; i < 100001; i++) {
    if (omax < odd[i]) {
      omax = odd[i];
      omax2 = i;
    }
    if (emax < even[i]) {
      emax = even[i];
      emax2 = i;
    }
  }

  if (omax2 != emax2) {
    cout << (N - omax - emax) << endl;
  } else {
    int osec = 0, esec = 0;
    for (int i = 0; i < 100001; i++) {
      // なるほど、ほんとの最大値は引っかからないようにif文を設定して最大値をもう一度求めることで二番目の最大値を求めれるというわけか
      if (osec <= odd[i] && i != omax2) {
        osec = odd[i];
      }
      if (esec < even[i] && i != emax2) {
        esec = even[i];
      }
    }
    cout << min((N - omax - esec), (N - emax - osec));
  }
  return 0;
}
Problem p03244 (C++)

Buggy submission (status: Runtime Error):


#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

template <class Container> auto me(const Container &c) {
  return std::max_element(std::begin(c), std::end(c));
}

auto exec2() {
  int N{};

  int tab1[10005]{}, tab2[10005]{};
  std::cin >> N;

  for (int i{0}; i < N / 2; i++) {
    int tmp{};
    std::cin >> tmp;
    tab1[tmp] += 1;
    std::cin >> tmp;
    tab2[tmp] += 1;
  }

  auto it1 = me(tab1);
  auto d1 = std::distance(std::cbegin(tab1), it1);

  auto it2 = me(tab2);
  auto d2 = std::distance(std::cbegin(tab2), it2);

  auto valid = 0;
  if (d1 == d2) {
    valid = std::max(*it1, *it2);
    tab1[d1] = tab2[d1] = 0;
    auto it12 = me(tab1);
    auto it22 = me(tab2);
    valid += std::max(*it12, *it22);
  } else {
    valid = (*it1) + (*it2);
  }
  std::cout << N - valid << "\n";
}

int main() {

  exec2();
  return 0;
}


Fixed submission:


#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

template <class Container> auto me(const Container &c) {
  return std::max_element(std::begin(c), std::end(c));
}

auto exec2() {
  int N{};

  int tab1[100005]{}, tab2[100005]{};
  std::cin >> N;

  for (int i{0}; i < N / 2; i++) {
    int tmp{};
    std::cin >> tmp;
    tab1[tmp] += 1;
    std::cin >> tmp;
    tab2[tmp] += 1;
  }

  auto it1 = me(tab1);
  auto d1 = std::distance(std::cbegin(tab1), it1);

  auto it2 = me(tab2);
  auto d2 = std::distance(std::cbegin(tab2), it2);

  auto valid = 0;
  if (d1 == d2) {
    valid = std::max(*it1, *it2);
    tab1[d1] = tab2[d1] = 0;
    auto it12 = me(tab1);
    auto it22 = me(tab2);
    valid += std::max(*it12, *it22);
  } else {
    valid = (*it1) + (*it2);
  }
  std::cout << N - valid << "\n";
}

int main() {

  exec2();
  return 0;
}
Problem p03244 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;

bool comp(P x, P y) { return x.second < y.first; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vi v(n);
  bool same = true;
  rep(i, n) {
    cin >> v[i];
    if (v[0] != v[i])
      same = false;
  }

  if (same) {
    cout << n / 2 << endl;
    return 0;
  }

  map<int, int> od, ev;
  rep(i, n) {
    if (i & 1) {
      od[v[i]]++;
    } else {
      ev[v[i]]++;
    }
  }

  vector<P> odp, evp;
  for (P x : od) {
    odp.emplace_back(x);
  }
  for (P x : ev) {
    evp.emplace_back(x);
  }

  sort(rall(odp), comp);
  sort(rall(evp), comp);

  int ans = INT_MAX;
  rep(i, min((int)odp.size(), 2)) {
    rep(j, min((int)evp.size(), 2)) {
      if (odp[i].first == evp[j].first)
        continue;
      ans = min(ans, n - odp[i].second - evp[j].second);
    }
  }

  cout << ans << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;

bool comp(P x, P y) { return x.second < y.second; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vi v(n);
  bool same = true;
  rep(i, n) {
    cin >> v[i];
    if (v[0] != v[i])
      same = false;
  }

  if (same) {
    cout << n / 2 << endl;
    return 0;
  }

  map<int, int> od, ev;
  rep(i, n) {
    if (i & 1) {
      od[v[i]]++;
    } else {
      ev[v[i]]++;
    }
  }

  vector<P> odp, evp;
  for (P x : od) {
    odp.emplace_back(x);
  }
  for (P x : ev) {
    evp.emplace_back(x);
  }

  sort(rall(odp), comp);
  sort(rall(evp), comp);

  int ans = INT_MAX;
  rep(i, min((int)odp.size(), 2)) {
    rep(j, min((int)evp.size(), 2)) {
      if (odp[i].first == evp[j].first)
        continue;
      ans = min(ans, n - odp[i].second - evp[j].second);
    }
  }

  cout << ans << endl;
}
Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define P pair<ll, ll>
#define INF 1000000000000000000

using namespace std;

int main(void) {
  ll n;
  cin >> n;

  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  map<ll, ll> b, c;
  for (int i = 0; i < n; i++) {
    if (i % 2) {
      c[a[i]]++;
    } else
      b[a[i]]++;
  }

  vector<P> bs(0), cs(0);
  for (auto p : b) {
    bs.emplace_back(P(p.second, p.first));
  }
  for (auto p : c) {
    cs.emplace_back(P(p.second, p.first));
  }
  sort(bs.begin(), bs.end());
  sort(cs.begin(), cs.end());

  if (bs[bs.size() - 1].second == cs[bs.size() - 1].second) {
    if (bs.size() == 1) {
      if (cs.size() == 1) {
        cout << n / 2 << endl;
      } else {
        cout << n / 2 - cs[cs.size() - 2].first << endl;
      }
    } else {
      if (cs.size() == 1)
        cout << n / 2 - bs[bs.size() - 2].first << endl;
      else {
        ll s = bs[bs.size() - 1].first + cs[cs.size() - 2].first;
        ll t = bs[bs.size() - 2].first + cs[cs.size() - 1].first;
        cout << n - max(s, t) << endl;
      }
    }
  } else
    cout << n - bs[bs.size() - 1].first - cs[cs.size() - 1].first << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define P pair<ll, ll>
#define INF 1000000000000000000

using namespace std;

int main(void) {
  ll n;
  cin >> n;

  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  map<ll, ll> b, c;
  for (int i = 0; i < n; i++) {
    if (i % 2) {
      c[a[i]]++;
    } else
      b[a[i]]++;
  }

  vector<P> bs(0), cs(0);
  for (auto p : b) {
    bs.emplace_back(P(p.second, p.first));
  }
  for (auto p : c) {
    cs.emplace_back(P(p.second, p.first));
  }
  sort(bs.begin(), bs.end());
  sort(cs.begin(), cs.end());

  if (bs[bs.size() - 1].second == cs[cs.size() - 1].second) {
    if (bs.size() == 1) {
      if (cs.size() == 1) {
        cout << n / 2 << endl;
      } else {
        cout << n / 2 - cs[cs.size() - 2].first << endl;
      }
    } else {
      if (cs.size() == 1)
        cout << n / 2 - bs[bs.size() - 2].first << endl;
      else {
        ll s = bs[bs.size() - 1].first + cs[cs.size() - 2].first;
        ll t = bs[bs.size() - 2].first + cs[cs.size() - 1].first;
        cout << n - max(s, t) << endl;
      }
    }
  } else
    cout << n - bs[bs.size() - 1].first - cs[cs.size() - 1].first << endl;

  return 0;
}
Problem p03244 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

typedef int64_t ll;
typedef vector<int> VI;
typedef pair<ll, ll> pii;
const int MD = 1e9 + 7;
typedef vector<ll> VL;

void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  VI v(n);
  rep(i, n) cin >> v[i];

  map<int, int> me, mo;
  rep(i, n) {
    if (i % 2)
      mo[v[i]]++;
    else
      me[v[i]]++;
  }
  vector<pii> ve, vo;
  for (auto q : me)
    ve.emplace_back(q.second, q.first);
  for (auto q : mo)
    vo.emplace_back(q.second, q.first);
  sort(rbegin(ve), rend(ve));
  sort(rbegin(vo), rend(vo));
  int ans = min(n - ve[0].first, n - vo[0].first);
  rep(i, min(sz(ve), 4)) rep(j, min(sz(vo), 4)) {
    if (ve[i].second == vo[j].second)
      continue;
    int ct = 0;
    rep(k, n) {
      dbg(k, v[k], ve[i].second, vo[j].second);
      if (k % 2 && v[k] != vo[i].second)
        ct++;
      else if (k % 2 == 0 && v[k] != ve[j].second)
        ct++;
    }
    // dbg(i,j,ct);
    ans = min(ans, ct);
  }
  cout << ans << "\n";
  return 0;
}


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

using namespace std;

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

typedef int64_t ll;
typedef vector<int> VI;
typedef pair<ll, ll> pii;
const int MD = 1e9 + 7;
typedef vector<ll> VL;

void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  VI v(n);
  rep(i, n) cin >> v[i];

  map<int, int> me, mo;
  rep(i, n) {
    if (i % 2)
      mo[v[i]]++;
    else
      me[v[i]]++;
  }
  vector<pii> ve, vo;
  for (auto q : me)
    ve.emplace_back(q.second, q.first);
  for (auto q : mo)
    vo.emplace_back(q.second, q.first);
  sort(rbegin(ve), rend(ve));
  sort(rbegin(vo), rend(vo));
  int ans = min(n - ve[0].first, n - vo[0].first);
  rep(i, min(sz(ve), 4)) rep(j, min(sz(vo), 4)) {
    if (ve[i].second == vo[j].second)
      continue;
    int ct = n - ve[i].first - vo[j].first;

    // dbg(i,j,ct);
    ans = min(ans, ct);
  }
  cout << ans << "\n";
  return 0;
}
Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n / 2), b(n / 2);
  int init_a = 0;
  int init_b = 0;
  int mx_a, mx_b;
  rep(i, n) {
    if (i % 2 == 0) {
      int x;
      cin >> x;
      a[x]++;
      if (a[x] > init_a) {
        init_a = a[x];
        mx_a = x;
      }
    } else {
      int x;
      cin >> x;
      b[x]++;
      if (b[x] > init_b) {
        init_b = b[x];
        mx_b = x;
      }
    }
  }
  if (mx_a != mx_b)
    cout << n - a[mx_a] - b[mx_b] << endl;
  else {
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int ans = 1e9;
    ans = min(ans, n - a[1] - b[0]);
    ans = min(ans, n - a[0] - b[1]);
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(100005), b(100005);
  int init_a = 0;
  int init_b = 0;
  int mx_a, mx_b;
  rep(i, n) {
    if (i % 2 == 0) {
      int x;
      cin >> x;
      a[x]++;
      if (a[x] > init_a) {
        init_a = a[x];
        mx_a = x;
      }
    } else {
      int x;
      cin >> x;
      b[x]++;
      if (b[x] > init_b) {
        init_b = b[x];
        mx_b = x;
      }
    }
  }
  if (mx_a != mx_b)
    cout << n - a[mx_a] - b[mx_b] << endl;
  else {
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    int ans = 1e9;
    ans = min(ans, n - a[1] - b[0]);
    ans = min(ans, n - a[0] - b[1]);
    cout << ans << endl;
  }
}
Problem p03244 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

// O(N log N)
int solve2(vector<int> v) {

  int N = (int)v.size();
  map<int, int> cnt;
  cnt.clear();
  rep(i, N) { ++cnt[v[i]]; } // end rep

  vector<P> rank;
  rank.clear();
  map<int, int>::iterator it = cnt.begin();
  for (; it != cnt.end(); ++it) {
    int n = (*it).first;
    int c = (*it).second;
    rank.push_back(P(-c, n));
  } // end for

  sort(ALL(rank));

  int res = N;
  if (rank.size() == 1) {
    res = min(res, N / 2);
  } else if (rank.size() >= 2) {
    vector<int> n((int)min(10, N), 0);
    rep(i, (int)min(10, N)) n[i] = rank[i].second;
    sort(ALL(n));
    do {
      int curr = 0;
      rep(i, N) {
        if (i % 2 == 0) {
          curr += (int)(v[i] != n[0]);
        } else { // if (i % 2 == 1)
          curr += (int)(v[i] != n[1]);
        } // end if
      }   // end rep
      res = min(res, curr);
    } while (next_permutation(ALL(n)));
  } // end if

  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int N;
  cin >> N;
  vector<int> v(N, 0);
  rep(i, N) { cin >> v[i]; } // end rep

  int res = solve2(v);

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

// O(N log N)
int solve2(vector<int> v) {

  int N = (int)v.size();
  map<int, int> cnt;
  cnt.clear();
  rep(i, N) { ++cnt[v[i]]; } // end rep

  vector<P> rank;
  rank.clear();
  map<int, int>::iterator it = cnt.begin();
  for (; it != cnt.end(); ++it) {
    int n = (*it).first;
    int c = (*it).second;
    rank.push_back(P(-c, n));
  } // end for

  sort(ALL(rank));

  int res = N;
  if (rank.size() == 1) {
    res = min(res, N / 2);
  } else if (rank.size() >= 2) {
    vector<int> n((int)min(7, N), 0);
    rep(i, (int)min(7, N)) n[i] = rank[i].second;
    sort(ALL(n));
    do {
      int curr = 0;
      rep(i, N) {
        if (i % 2 == 0) {
          curr += (int)(v[i] != n[0]);
        } else { // if (i % 2 == 1)
          curr += (int)(v[i] != n[1]);
        } // end if
      }   // end rep
      res = min(res, curr);
    } while (next_permutation(ALL(n)));
  } // end if

  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int N;
  cin >> N;
  vector<int> v(N, 0);
  rep(i, N) { cin >> v[i]; } // end rep

  int res = solve2(v);

  cout << res << endl;

  return 0;
}