Problem: A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:    * For each i = 1,2,..., n-2, a_i = a_{i+2}.   * Exactly two different numbers appear in the sequence.  You are given a sequence v_1,v_2,...,v_n whose length is even. We would like to make this sequence /\/\/\/ by replacing some of its elements. Find the minimum number of elements that needs to be replaced.

Submission attempts (in order):


#include <string>
#include <cmath>
#include <algorithm>
#include <tuple>
using namespace std;
typedef long long ll;




int main()
{

  int n;
  cin >> n;
  int v[n];

  rep(i, n) cin >> v[i];

  int seq0[100001] = {}, seq1[100001] = {};
  for (int i = 0; i < n; i += 2)
    ++seq0[v[i]];
  for (int i = 1; i < n; i += 2)
    ++seq1[v[i]];

  sort(seq0 + 1, seq0 + 100001, greater<int>());
  sort(seq1 + 1, seq1 + 100001, greater<int>());

  if (seq0[0] == seq1[0])
    cout << min(n - seq0[0] - seq1[1], n - seq0[1] - seq1[0]);
  else
    cout << n - seq0[0] - seq1[0];
}

from collections import defaultdict


def main():

    n = int(input())

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

    odd = defaultdict(int)
    even = defaultdict(int)

    for i, num in enumerate(v):
        # print(i % 2)
        if (i % 2) == 0:
            even[num] += 1
        elif (i % 2) == 1:
            # print(i, ": ", num)
            odd[num] += 1

    x = sorted(even.items(), key=lambda x: x[1], reverse=True)
    y = sorted(odd.items, key=lambda x: x[1], reverse=True)

    if x[0][0] != y[0][0]:
        ans = n - x[0][1] - y[0][1]
    else:
        ans = n - max(x[0][1] + y[1][1], x[1][1] + y[0][1])

    print(ans)


main()


from collections import defaultdict

n = input()
V = map(int, input().split())

odd = defaultdict(int)
even = defaultdict(int)

max_odd = 0
max_even = 0
f_odd = 0
f_even = 0
s_odd = 0
s_even = 0

for i, v in enumerate(V):
    i += 1
    if i % 2 == 0:
        even[v] += 1
        if even[v] > max_even:
            max_even = even[v]
            if f_even != 0 and v != f_even:
                s_even = f_even
                f_even = v
            elif f_even == 0:
                f_even = v

    elif i % 2 == 1:
        odd[v] += 1
        if odd[v] > max_odd:
            max_odd = odd[v]
            if f_odd != 0 and v != f_odd:
                s_odd = f_odd
                f_odd = v
            elif f_odd == 0:
                f_odd = v

print('test')
# print(odd, even)
# print(f'{max_odd}, {max_even}, {f_odd}, {s_odd}, {f_even}, {s_even}')


from collections import defaultdict

n = int(input())
V = map(int, input().split())

odd = defaultdict(int)
even = defaultdict(int)

max_odd = 0
max_even = 0
f_odd = 0
f_even = 0
s_odd = 0
s_even = 0

for i, v in enumerate(V):
    i += 1
    if i % 2 == 0:
        even[v] += 1
        if even[v] > max_even:
            max_even = even[v]
            if f_even != 0 and v != f_even:
                s_even = f_even
                f_even = v
            elif f_even == 0:
                f_even = v

    elif i % 2 == 1:
        odd[v] += 1
        if odd[v] > max_odd:
            max_odd = odd[v]
            if f_odd != 0 and v != f_odd:
                s_odd = f_odd
                f_odd = v
            elif f_odd == 0:
                f_odd = v

half = n / 2
cnt = 0


if f_odd != f_even:
    c1 = (half - max_odd) + (half - max_even)
    c2 = (half - max_even) + (half - max_odd)
    if c1 > c2:
        cnt = c2
    else:
        cnt = c1
    num1 = f_odd
    num2 = f_even
elif f_odd == f_even:
    c1 = (half - max_odd) + (half - even[s_even])
    c2 = (half - max_even) + (half - odd[s_odd])
    if c1 > c2:
        cnt = c2
        num1 = s_odd
        num2 = f_even
    else:
        cnt = c1
        num1 = f_odd
        num2 = s_even

last_odd = V[-2]
last_even = V[-1]
if last_odd != num1 and last_odd == num2:
    cnt -= 1
if last_even != num2 and last_even == num1:
    cnt -= 1

print(int(cnt))




#include <string>
#include <cmath>
#include <algorithm>
#include <tuple>
using namespace std;
typedef long long ll;




int main()
{

  int n;
  cin >> n;
  int v[n];

  rep(i, n) cin >> v[i];

  int seq0[100001] = {}, seq1[100001] = {};
  for (int i = 0; i < n; i += 2)
    ++seq0[v[i]];
  for (int i = 1; i < n; i += 2)
    ++seq1[v[i]];

  sort(seq0 + 1, seq0 + 100001, greater<int>());
  sort(seq1 + 1, seq1 + 100001, greater<int>());

  if (seq0[0] == seq1[0])
    cout << min(n - seq0[0][0] - seq1[0][1], n - seq[0][1] - seq1[0][0]);
  else
    cout << n - seq0[0][0] - seq1[0][0];
}

from collections import defaultdict


def main():

    n = int(input())

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

    odd = defaultdict(int)
    even = defaultdict(int)
    odd[0] = 0
    even[0] = 0

    for i, num in enumerate(v):
        # print(i % 2)
        if (i % 2) == 0:
            even[num] += 1
        elif (i % 2) == 1:
            # print(i, ": ", num)
            odd[num] += 1

    even_list = sorted(even.items(), key=lambda x: x[1], reverse=True)
    odd_list = sorted(odd.items(), key=lambda x: x[1], reverse=True)

    ans_list = []
    for ev in even_list[:2]:
        for od in odd_list[:2]:
            if ev[0] != od[0]:
                ans = 0
                ans += n - (ev[1] + od[1])
                ans_list.append(ans)

    print(min(ans_list))


main()


Verdicts: Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Accepted