Problem:


Score : 300 points

Problem Statement

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.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.



Sample input:
4
3 1 3 2


Sample output:
1

Submission status: Accepted

Solution code:
from collections import Counter

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

x = Counter(v[::2]).most_common(2)
y = Counter(v[1::2]).most_common(2)
ans = n
if x[0][0] == y[0][0]:
  if len(x) == len(y) == 1:
    ans = n//2
  else:
    s = max([a[1] for a in x]) + min([b[1] for b in y])
    t = min([a[1] for a in x]) + max([b[1] for b in y])
    ans -= max(s, t)
else:
  ans -= x[0][1] + y[0][1]
print(ans)

Submission status: Accepted

Solution code:
from collections import*
n,*v=map(int,open(0).read().split())
a,b=[Counter(v[i::2]).most_common()+[(0,0)]for i in(0,1)]
s,x=a[0]
t,y=b[0]
print(n-[x+y,max(x+b[1][1],y+a[1][1])][s==t])

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
a=n//2
dp=[[0,0]for i in range(10**5+1)]
bp=[[0,0]for i in range(10**5+1)]
for i in range(n):
  if i%2==0:
    dp[v[i]][0]+=1
    dp[v[i]][1]=v[i]
  else:
    bp[v[i]][0]+=1
    bp[v[i]][1]=v[i]
dp.sort(reverse=True)
bp.sort(reverse=True)
if bp[0][1]==dp[0][1]:
  if dp[1][0]>=bp[1][0]:
    dp[0][0]=dp[1][0]
  else:
    bp[0][0]=bp[1][0]
print(a-dp[0][0]+a-bp[0][0])

Submission status: Accepted

Solution code:
from collections import Counter

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def main():
    n = ii()
    v = list(mi())
    ve = [v[i] for i in range(n) if i%2==0]
    vo = [v[i] for i in range(n) if i%2==1]

    ce = Counter(ve)
    co = Counter(vo)

    if ce.most_common()[0][0] == co.most_common()[0][0]:
        if len(ce.most_common()) > 1 and len(co.most_common()) > 1:
            if ce.most_common()[1][1] > co.most_common()[1][1]:
                ne = ce.most_common()[1][1]
                no = ce.most_common()[0][1]
            else:
                ne = ce.most_common()[0][1]
                no = co.most_common()[1][1]
        else:
            ne = ce.most_common()[0][1]
            no = 0
    else:
        ne = ce.most_common()[0][1]
        no = co.most_common()[0][1]

    print(n-ne-no)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
a=Counter(v[0::2]).most_common()
b=Counter(v[1::2]).most_common()
a.append([0,0])
b.append([0,0])
if a[0][0]!=b[0][0]:print(n-(a[0][1]+b[0][1]))
else:print(n-max(a[0][1]+b[1][1],a[1][1]+b[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
a=Counter(v[0::2]).most_common()
b=Counter(v[1::2]).most_common()
a.append([0,0])
b.append([0,0])
if a[0][0]!=b[0][0]:
    print(n-(a[0][1]+b[0][1]))
else:
    print(min(n-(a[1][1]+b[0][1]),n-(a[0][1]+b[1][1])))

Submission status: Accepted

Solution code:
from collections import Counter
d = [Counter(), Counter()]
n = int(input())
v = list(map(int, input().split()))
for i, vi in enumerate(v):
    d[i&1][vi] += 1
a = []
for i in range(2):
    a.append(sorted(d[i].keys(), key=lambda k: d[i][k], reverse=True))
c = [ai[:2] for ai in a]
if len(c[0]) == len(c[1]) == 1 and c[0][0] == c[1][0]:
    print(n // 2)
else:
    ans = 1001001001
    for ci in c[0]:
        for cj in c[1]:
            if ci == cj:
                continue
            cnt = sum(v for k, v in d[0].items() if k != ci) + sum(v for k, v in d[1].items() if k != cj)
            ans = min(ans, cnt)
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int,input().split()))
v1 = v[::2]
v2 = v[1::2]
C1 = Counter(v1)
C2 = Counter(v2)
if C1.most_common(1)[0][0]!=C2.most_common(1)[0][0]: 
  print((n//2-C1.most_common(1)[0][1])+(n//2-C2.most_common(1)[0][1]))
elif C1.most_common(1)[0][1]==C2.most_common(1)[0][1]==n//2:
  print(n//2)
elif C1.most_common(1)==C2.most_common(1) and C1.most_common(2)[1][1] >= C2.most_common(2)[1][1]:
  print((n//2-C1.most_common(2)[1][1])+(n//2-C2.most_common(1)[0][1]))
else:
  print((n//2-C1.most_common(1)[0][1])+(n//2-C2.most_common(2)[1][1]))

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
v = list(map(int, input().split()))
even = [[0,i] for i in range(10**5+5)]
odd = [[0,i] for i in range(10**5+5)]
numnum = 0
for i in range(n):
    if even[v[i]][0] == 0 and odd[v[i]][0] == 0:
        numnum += 1
    if i % 2 == 0:
        even[v[i]][0] += 1
    else:
        odd[v[i]][0] += 1
if numnum == 1:
    print(n // 2)
    exit()
even.sort(key=lambda x:x[0], reverse=True)
odd.sort(key=lambda x:x[0], reverse=True)
if n == 2:
    print(0)
    exit()
e0, e1, o0, o1 = even[0][1], even[1][1], odd[0][1], odd[1][1]
if e0 != o0:
    print(n - even[0][0] - odd[0][0])
elif e0 != o1 and e1 != o0:
    print(min(n - even[0][0] - odd[1][0], n - even[1][0] - odd[0][0]))
elif e0 != o1:
    print(n - even[0][0] - odd[1][0])
else:
    print(n - even[1][0] - odd[0][0])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int,input().split()))
v1 = v[::2]
v2 = v[1::2]

C1 = Counter(v1)
C2 = Counter(v2)
#print(C1.most_common(1))
#print(C2.most_common(1))
#print(C1.most_common(1)[0][0]!=C2.most_common(1)[0][0])
if C1.most_common(1)[0][0]!=C2.most_common(1)[0][0]: 
  print((n//2-C1.most_common(1)[0][1])+(n//2-C2.most_common(1)[0][1]))
  exit()
  
elif C1.most_common(1)[0][1]==C2.most_common(1)[0][1]==n//2:
  print(n//2)
  exit()
  
elif C1.most_common(1)==C2.most_common(1) and C1.most_common(2)[1][1] >= C2.most_common(2)[1][1]:
  print((n//2-C1.most_common(2)[1][1])+(n//2-C2.most_common(1)[0][1]))
  exit()

else:
  print((n//2-C1.most_common(1)[0][1])+(n//2-C2.most_common(2)[1][1]))
  exit()

Submission status: Accepted

Solution code:
from collections import Counter

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

e=Counter(v[::2]).most_common() + [(0,0)]
o=Counter(v[1::2]).most_common() + [(0,0)]


if e[0][0]!=o[0][0]:
    print(n-e[0][1]-o[0][1])
else:
    print(min(n-e[0][1]-o[1][1],n-e[1][1]-o[0][1]))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))

Aone = A[0]
counter = 0

for i in range (0, n):
	if A[i] == Aone:
		counter+=1
	else:
		break

if counter == n:
	print(int(n/2))
	exit()

k = int(len(A)/2)
maxA = max(A)

Odd = []
Even = []

for i in range (0, k):
	Odd.append(A[2*i])
	Even.append(A[2*i+1])

Oddcount = [0]*maxA
Evencount = [0]*maxA

for i in range (0, k):
	Oddcount[Odd[i]-1]+=1
	Evencount[Even[i]-1]+=1

Oddcount2 = sorted(Oddcount.copy())
Evencount2 = sorted(Evencount.copy())

if Oddcount.index(Oddcount2[-1]) != Evencount.index(Evencount2[-1]):
	print(2*k-Oddcount2[-1]-Evencount2[-1])
elif Oddcount2[-2] >= Evencount2[-2]:
	print(2*k-Oddcount2[-2]-Evencount2[-1])
else:
	print(2*k-Oddcount2[-1]-Evencount2[-2])

Submission status: Accepted

Solution code:
import itertools
from collections import Counter


n = int(input())
v = list(map(int, input().split()))
ev_cnt = Counter(v[::2])
od_cnt = Counter(v[1::2])
ev_cnt[-1] = 0
od_cnt[-2] = 0
ev = ev_cnt.most_common(2)
od = od_cnt.most_common(2)
ans = n
for (k1, v1), (k2, v2) in itertools.product(ev, od):
    if k1 == k2:
        continue
    x = (n - v1 - v2)
    ans = min(ans, x)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int, input().split()))
even=[0]*(10**6 + 1)
odd=[0]*(10**6 + 1)



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

a=max(even)
M=even.index(a)
even[M]=0
a_second=max(even)
even[M]=a

b=max(odd)
N=odd.index(b)
odd[N]=0
b_second=max(odd)
odd[N]=b


if M!=N:
    ans=sum(even)-a + sum(odd)-b
    print(ans)
else:
    ans=min(sum(even)-a + sum(odd)-b_second, sum(even)-a_second + sum(odd)-b)
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
V=list(map(int,input().split()))

A=[V[i] for i in range(0,n,2)]
B=[V[i] for i in range(1,n,2)]

import collections
  
C = collections.Counter(A)
D = collections.Counter(B)
  
if C.most_common()[0][0] != D.most_common()[0][0]:
  print(len(A)+len(B)-C.most_common()[0][1]-D.most_common()[0][1])
else:
  if len(C)!=1 and len(D)!=1:
    print( min(len(A)+len(B)-C.most_common()[0][1]-D.most_common()[1][1],
            len(A)+len(B)-C.most_common()[1][1]-D.most_common()[0][1]) )
  elif len(C)!=1 and len(D)==1:
    print( min(len(A)+len(B)-C.most_common()[0][1]-D.most_common()[0][1],
            len(A)+len(B)-C.most_common()[1][1]-D.most_common()[0][1]) )
  elif len(C)==1 and len(D)!=1:
    print( min(len(A)+len(B)-C.most_common()[0][1]-D.most_common()[1][1],
            len(A)+len(B)-C.most_common()[0][1]-D.most_common()[0][1]) )
  else:
    print(min(len(A),len(B)))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())

lis = list(map(int, input().split()))

t = len(lis)

a = Counter(lis[0::2]).most_common()
b = Counter(lis[1::2]).most_common()


ans2 = 1001001001
ans3 = 1001001001
ans4 = 1001001001

if a[0][0] != b[0][0]:
    ans = t - a[0][1] - b[0][1]
    print(ans)
    
else:
    if len(a) != 1:
        ans2 = t - a[1][1] - b[0][1]
    if len(b) != 1:
        ans3 = t - a[0][1]-b[1][1]
    else:
        ans4 = t/2
        
    print(int(min(ans2, ans3, ans4)))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
V = list(map(int,input().split()))
A = Counter(V[0::2]).most_common(2)
B = Counter(V[1::2]).most_common(2)
A.append((0,0))
B.append((0,0))
if A[0][0]!=B[0][0]:
    print(n-(A[0][1]+B[0][1]))
else:
    print(min(n-(A[1][1]+B[0][1]),n-(A[0][1]+B[1][1])))

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
V=list(map(int,input().split()))
a=Counter(V[0::2]).most_common(2)
b=Counter(V[1::2]).most_common(2)
a.append((0,0))
b.append((0,0))
if a[0][0]!=b[0][0]:
    print(n-(a[0][1]+b[0][1]))
else:
    print(min(n-(a[1][1]+b[0][1]),n-(a[0][1]+b[1][1])))

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    n = int(input())
    A = list(map(int, input().split()))
    A1 = A[::2]
    A2 = A[1::2]
    C1 = Counter(A1).most_common()
    C2 = Counter(A2).most_common()
    if len(set(A)) == 1:
        print(n // 2)
    elif C1[0][0] == C2[0][0]:
        print(n - max(C1[0][1] + C2[1][1], C1[1][1] + C2[0][1]))
    else:
        print(n - C1[0][1] - C2[0][1])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
v1, v2 = v[::2], v[1::2]
vv1 = Counter(v1).most_common()
vv2 = Counter(v2).most_common()

if len(set(v)) == 1:
    print(n//2)
elif vv1[0] != vv2[0]:
    print(n - vv1[0][1] - vv2[0][1])
else:
    print(n - max(vv1[1][1] + vv2[0][1], vv1[0][1] + vv2[1][1]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
v1, v2 = [], []
for i in range(n):
    if i%2:
        v1.append(v[i])
    else:
        v2.append(v[i])
vv1 = Counter(v1).most_common()
vv2 = Counter(v2).most_common()
if vv1[0] != vv2[0]:
    print(n - vv1[0][1] - vv2[0][1])
else:
    if len(vv1) == len(vv2) == 1:
        print(n - max(vv1[0][1], vv2[0][1]))
    elif len(vv1) == 1 and len(vv2) == 2:
        print(n - vv1[0][1] - vv2[1][1])
    elif len(vv1) == 2 and len(vv2) == 1:
        print(n - vv1[1][1] - vv2[0][1])
    else:
        print(n - max(vv1[1][1] + vv2[0][1], vv1[0][1] + vv2[1][1]))

Submission status: Accepted

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial#, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_list(N): return [s_list() for _ in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    n = i_input()
    num_list = i_list()

    zero_list = []
    one_list = []
    for i in range(n):
        if i % 2 == 0:
            zero_list.append(num_list[i])
        else:
            one_list.append(num_list[i])

    zero_c = Counter(zero_list)
    one_c = Counter(one_list)

    zero_saihin_key = zero_c.most_common()[0][0]
    zero_saihin_val = zero_c.most_common()[0][1]
    one_saihin_key = one_c.most_common()[0][0]
    one_saihin_val = one_c.most_common()[0][1]

    if zero_saihin_key != one_saihin_key:
        print(n - zero_saihin_val - one_saihin_val)
        exit()

    if num_list.count(zero_saihin_key) == n:
        print(n//2)
        exit()

    if len(zero_c) == 1:
        zero_saihin_val2 = zero_saihin_val
    else:
        zero_saihin_val2 = zero_c.most_common()[1][1]

    if len(one_c) == 1:
        one_saihin_val2 = one_saihin_val
    else:
        one_saihin_val2 = one_c.most_common()[1][1]

    count1 = n - zero_saihin_val - one_saihin_val2
    count2 = n - zero_saihin_val2 - one_saihin_val

    print(min(count1, count2))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial#, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_list(N): return [s_list() for _ in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    n = i_input()
    num_list = i_list()

    zero_list = []
    one_list = []
    for i in range(n):
        if i % 2 == 0:
            zero_list.append(num_list[i])
        else:
            one_list.append(num_list[i])

    zero_c = Counter(zero_list)
    one_c = Counter(one_list)

    zero_saihin_key = zero_c.most_common()[0][0]
    zero_saihin_val = zero_c.most_common()[0][1]
    one_saihin_key = one_c.most_common()[0][0]
    one_saihin_val = one_c.most_common()[0][1]

    if zero_saihin_key != one_saihin_key:
        print(n - zero_saihin_val - one_saihin_val)
        exit()

    if num_list.count(zero_saihin_key) == n:
        print(n//2)
        exit()

    if len(zero_c) == 1:
        print(n//2 - one_c.most_common()[1][1])
    elif len(one_c) == 1:
        print(n//2 - zero_c.most_common()[1][1])
    else:
        count1 = n - zero_saihin_val - one_c.most_common()[1][1]
        count2 = n - zero_c.most_common()[1][1] - one_saihin_val
        print(min(count1, count2))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def resolve():
    import math
    from collections import Counter
    n = int(input())
    v = [int(i) for i in input().split()]
    c0 = Counter(v[0::2]).most_common(2)
    c1 = Counter(v[1::2]).most_common(2)
    l0 = math.ceil(n / 2)
    l1 = n // 2
    if c0[0][0] != c1[0][0]:
        print(l0 - c0[0][1] + l1 - c1[0][1])
    else:
        if len(c0) == 1 and len(c1) == 1:
            print(n // 2)
        else:
            if len(c0) == 2 and len(c1) == 2:
                print(min(l0 - c0[0][1] + l1 - c1[1][1],
                          l0 - c0[1][1] + l1 - c1[0][1]))
            elif len(c0) == 1:
                print(l1 - c1[1][1])
            else:
                print(l0 - c0[1][1])


resolve()

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
vs=list(map(int,input().split()))
xs=vs[0::2]
ys=vs[1::2]
cx=Counter(xs).most_common()
cy=Counter(ys).most_common()
if len(cx)==1:
    cx.append((0,0))
if len(cy)==1:
    cy.append((-1,0))
if not cx[0][0]==cy[0][0]:
    print(len(xs)-cx[0][1]+len(ys)-cy[0][1])
else:
    mx=len(xs)-cx[1][1]+len(ys)-cy[0][1]
    my=len(xs)-cx[0][1]+len(ys)-cy[1][1]
    print(min(mx,my))

Submission status: Accepted

Solution code:
N = int(input())
N_2 = N//2

v = list(map(int, input().split()))
v_odd = [v[2*i+1] for i in range(N_2)]
v_evn = [v[2*i] for i in range(N_2)]

lst_odd = [0] * 100001
lst_evn = [0] * 100001

for vi in v_odd:
  lst_odd[vi] += 1
  
for vi in v_evn:
  lst_evn[vi] += 1
  
tpl_odd = [(lst_odd[i+1], i+1) for i in range(100000)]
tpl_evn = [(lst_evn[i+1], i+1) for i in range(100000)]

tpl_odd.sort(reverse=True)
tpl_evn.sort(reverse=True)
if tpl_odd[0][1] != tpl_evn[0][1]:
  print(N - tpl_odd[0][0] - tpl_evn[0][0])
else:
  print(N - max(tpl_odd[1][0]+tpl_evn[0][0], tpl_odd[0][0]+tpl_evn[1][0]))

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
V = list(map(int, input().split()))

d1 = defaultdict(int)
d2 = defaultdict(int)
for i, v in enumerate(V):
    if i % 2 == 0:
        d1[v] += 1
    else:
        d2[v] += 1
d1 = sorted(d1.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
d2 = sorted(d2.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)

best = 10 ** 10
for d1, d2 in ((d1, d2), (d2, d1)):
    ans = N // 2 - d1[0][1]
    val = d1[0][0]
    if d2[0][0] != val:
        ans += N // 2 - d2[0][1]
    else:
        ans += d2[0][1]
        if 2 < len(d2):
            ans += N // 2 - d2[0][1] - d2[1][1]
    best = min(best, ans)
print(best)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

if len(set(V)) == 1:
    print(n // 2)
    exit()

d0, d1 = defaultdict(int), defaultdict(int)

for i, v in enumerate(V):
    if i % 2 == 0:
        d0[v] += 1
    else:
        d1[v] += 1

d0_ = sorted(d0.items(), key=lambda x: -x[1])
d1_ = sorted(d1.items(), key=lambda x: -x[1])

a0, n0 = d0_[0]
a1, n1 = d1_[0]

if a0 != a1:
    ans = n - n0 - n1
else:
    if n0 > n1:
        m1 = d1_[1][1]
        ans = n - n0 - m1
    elif n0 < n1:
        m0 = d0_[1][1]
        ans = n - m0 - n1
    else:
        m0 = d0_[1][1]
        m1 = d1_[1][1]
        ans = n - n0 - max(m0, m1)

print(ans)

Submission status: Accepted

Solution code:
import collections

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

    c1 = collections.Counter(v[0::2])
    c2 = collections.Counter(v[1::2])
    
    def most(c, x, y):
        return c.most_common()[x][y]
    
    if most(c1, 0, 1) == most(c2, 0, 1) == n//2 and most(c1, 0, 0) == most(c2, 0, 0):
        print(n // 2)
    elif most(c1, 0, 0) != most(c2, 0, 0):
        print(n - most(c1, 0, 1)- most(c2, 0, 1))
    else:
            a  = n - most(c1, 1, 1)- most(c2, 0, 1)
            b  = n - most(c1, 0, 1)- most(c2, 1, 1)
            print(min(a, b))
         
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

c1 = Counter(v[::2]).most_common() + [(0, 0)]
c2 = Counter(v[1::2]).most_common() + [(0, 0)]

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


n = int(input())
v = list(map(int, input().split()))
v_former = Counter(v[::2]).most_common() + [(0, 0)]
v_latter = Counter(v[1::2]).most_common() + [(0, 0)]
if v_former[0][0] != v_latter[0][0]:
    print(n - v_former[0][1] - v_latter[0][1])
else:
    print(n - max(v_former[0][1] + v_latter[1][1], v_former[1][1] + v_latter[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter


n = int(input())
v = list(map(int, input().split()))
v_former = sorted([(v, k) for k, v in Counter(v[::2]).items()], reverse=True) + [(0, 0)]
v_latter = sorted([(v, k) for k, v in Counter(v[1::2]).items()], reverse=True) + [(0, 0)]
if v_former[0][1] != v_latter[0][1]:
    print(n - v_former[0][0] - v_latter[0][0])
else:
    print(n - max(v_former[0][0] + v_latter[1][0], v_former[1][0] + v_latter[0][0]))

Submission status: Accepted

Solution code:
from collections import Counter


n = int(input())
v = list(map(int, input().split()))
v_former = sorted([(v, k) for k, v in Counter(v[::2]).items()], reverse=True)
v_latter = sorted([(v, k) for k, v in Counter(v[1::2]).items()], reverse=True)
if len(v_former) == 1 and len(v_latter) == 1:
    if v_former[0][1] != v_latter[0][1]:
        print(0)
    else:
        print(n // 2)
elif v_former[0][1] != v_latter[0][1]:
    print(n - v_former[0][0] - v_latter[0][0])
elif len(v_former) == 1:
    print(n - v_former[0][0] - v_latter[1][0])
elif len(v_latter) == 1:
    print(n - v_former[1][0] - v_latter[0][0])
else:
    print(n - max(v_former[0][0] + v_latter[1][0], v_former[1][0] + v_latter[0][0]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
v1 = Counter(v[::2])
v2 = Counter(v[1::2])
E = v1.most_common()
O = v2.most_common()
if E[0][0] == O[0][0]:
    tmp1 = E[1][1] if len(E) > 1 else 0
    tmp2 = O[1][1] if len(O) > 1 else 0
    ans = min(n-E[0][1]-tmp2, n-tmp1-O[0][1])
else:
    ans = n-E[0][1]-O[0][1]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
o, e = v[0::2], v[1::2]
co, ce = Counter(o).most_common(), Counter(e).most_common()
if co[0][0] == ce[0][0]:
    if len(co) == len(ce) == 1:
        print(n // 2)
    elif len(co) == 1:
        print(n // 2 - ce[0][1])
    elif len(ce) == 1:
        print(n // 2 - co[0][1])
    else:
        print(min(n - (co[1][1] + ce[0][1]),n - (co[0][1] + ce[1][1])))
else:
    if len(co) == len(ce) == 1:
        print(0)
    elif len(co) == 1:
        print(n // 2 - ce[0][1])
    elif len(ce) == 1:
        print(n // 2 - co[0][1])
    else:
        print(n - (co[0][1] + ce[0][1]))

Submission status: Accepted

Solution code:
N = int(input())
v = list(map(int, input().split()))

first = {}
second = {}

for x in range(N):
    if x % 2 == 0:
        first.setdefault(v[x], 0)
        first[v[x]] += 1
    else:
        second.setdefault(v[x], 0)
        second[v[x]] += 1

first_sorted = sorted(first.items(), key=lambda x: x[1], reverse=True)
second_sorted = sorted(second.items(), key=lambda x: x[1], reverse=True)
sum_change_1 = 0

first_value = 0
for y in range(len(first_sorted)):
    if y == 0:
        first_value = first_sorted[y][0]
    if y != 0:
        sum_change_1 += first_sorted[y][1]

for z in range(len(second_sorted)):
    if first_value != second_sorted[0][0]:
        if z != 0:
            sum_change_1 += second_sorted[z][1]
    else:
        if z != 1:
            sum_change_1 += second_sorted[z][1]

sum_change_2 = 0

first_value = 0
for v in range(len(second_sorted)):
    if v == 0:
        first_value = second_sorted[v][0]
    if v != 0:
        sum_change_2 += second_sorted[v][1]

for w in range(len(first_sorted)):
    if first_value != first_sorted[0][0]:
        if w != 0:
            sum_change_2 += first_sorted[w][1]
    else:
        if w != 1:
            sum_change_2 += first_sorted[w][1]

print(min(sum_change_1, sum_change_2))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
e = v[::2]
o = v[1::2]
ce = sorted([(v, k) for k, v in Counter(e).items()], reverse=True)
co = sorted([(v, k) for k, v in Counter(o).items()], reverse=True)
if ce[0][1] == co[0][1]:
    if len(ce) == 1 and len(co) == 1:
        print(len(o))
    elif len(ce) == 1:
        print(len(o) + len(e) - ce[0][0] - co[1][0])
    elif len(co) == 1:
        print(len(o) + len(e) - ce[1][0] - co[0][0])
    else:
        print(len(e) + len(o) - max(ce[1][0] + co[0][0], ce[0][0] + co[1][0]))
else:
    print(len(e) + len(o) - ce[0][0] - co[0][0])

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
 
n = INT()
v = LIST()

v1 = v[::2]
v2 = v[1::2]

def cnt(v1, v2):
	ans1 = len(v1) - Counter(v1).most_common()[0][1]

	if Counter(v1).most_common()[0][0] != Counter(v2).most_common()[0][0]:
		ans2 = len(v2) - Counter(v2).most_common()[0][1]
	else:
		if len(Counter(v2).most_common()) == 1:
			ans2 = Counter(v2).most_common()[0][1]
		else:
			ans2 = len(v2) - Counter(v2).most_common()[1][1]

	return ans1 + ans2

print(min(cnt(v1, v2), cnt(v2, v1)))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = [int(x) for x in input().split()]

v1 = Counter(v[0::2]).most_common() + [(0, 0)]
v2 = Counter(v[1::2]).most_common() + [(0, 0)]

if v1[0][0] != v2[0][0]:
    res = n - v1[0][1] - v2[0][1]
else:
    res = min(n - v1[0][1] - v2[1][1], n - v1[1][1] - v2[0][1])

print(res)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = [int(x) for x in input().split()]

v1 = Counter(v[0::2]).most_common() + [(0, 0)]
v2 = Counter(v[1::2]).most_common() + [(0, 0)]

if v1[0] != v2[0]:
    res = n - v1[0][1] - v2[0][1]
else:
    res = min(n - v1[0][1] - v2[1][1], n - v1[1][1] - v2[0][1])

print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun May 31 18:36:13 2020

@author: NEC-PCuser
"""
n = int(input())
l = list(map(int, input().split()))
dic_gusu = {}
dic_kisu = {}

for i in range(0, n):
    if (i % 2 == 0):
        if (l[i] in dic_gusu):
            dic_gusu[l[i]] += 1
        else:
            dic_gusu[l[i]] = 1
    if (i % 2 == 1):
        if (l[i] in dic_kisu):
            dic_kisu[l[i]] += 1
        else:
            dic_kisu[l[i]] = 1
dic_kisu = sorted(dic_kisu.items(), key = lambda x : x[1])
dic_gusu = sorted(dic_gusu.items(), key = lambda x : x[1])

count = 0
if (len(dic_kisu) == 1 and len(dic_gusu) == 1 and dic_kisu[len(dic_kisu) - 1][0] == dic_gusu[len(dic_gusu) - 1][0]):
    count = n // 2
elif (dic_kisu[len(dic_kisu) - 1][0] != dic_gusu[len(dic_gusu) - 1][0]):
    for i in range(0, len(dic_kisu) - 1):
        count += dic_kisu[i][1]
    for i in range(0, len(dic_gusu) - 1):
        count += dic_gusu[i][1]
else:
    for i in range(0, len(dic_kisu)):
        count += dic_kisu[i][1]
    for i in range(0, len(dic_gusu)):
        count += dic_gusu[i][1]
    count_tmp = count
    count_ans = count_tmp - dic_kisu[len(dic_kisu) - 1][1] - dic_gusu[len(dic_gusu) - 2][1] 
    count_ans2 = count_tmp  - dic_kisu[len(dic_kisu) - 2][1] - dic_gusu[len(dic_gusu) - 1][1]
    count = min([count_ans, count_ans2])    
        
print(count)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

N = int(input())
V = list(map(int, sys.stdin.readline().rsplit()))

Ceven = Counter(V[::2])
Codd = Counter(V[1::2])

res = 0
even, odd = Ceven.most_common(2), Codd.most_common(2)
if even[0][0] != odd[0][0]:
    res = N - even[0][1] - odd[0][1]
else:
    if len(Ceven) == len(Codd) == 1:
        res = N // 2
    else:
        res = min(
            N - even[1][1] - odd[0][1],
            N - even[0][1] - odd[1][1]
        )
print(res)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
l = list(map(int, input().split()))
E = [i for i in l[::2]]
O = [i for i in l[1::2]]
e=Counter(E).most_common() #偶数番目の位置にある数の出現回数を最頻度順に並べる
o=Counter(O).most_common() #奇数番目の位置にある数の出現回数を最頻度順に並べる

if e[0][0] != o[0][0]: #偶数列の最頻値と奇数列の最頻値が異なる場合、それぞれを最頻値で置き換えるだけ
    print(n-e[0][1]-o[0][1])
else:
    if len(e)==1 or len(o)==1: #偶奇数列で最頻地が同じかつ片方が一種類の時、もう一方を全部置き換えるしかない
        print(n//2)
    else: #そうでない場合最頻値とsecond最頻値で置き換える
        a=n-e[1][1]-o[0][1]
        b=n-e[0][1]-o[1][1]
        print(min(a,b))

Submission status: Accepted

Solution code:
n = int(input())
V = map(int, input().split())

odd = []
even = []
for i, v in enumerate(V):
    if i % 2 == 0:
        odd.append(v)
    else:
        even.append(v)
num_odd = {}
for v in odd:
    if v in num_odd:
        num_odd[v] += 1
    else:
        num_odd[v] = 1
num_even = {}
for v in even:
    if v in num_even:
        num_even[v] += 1
    else:
        num_even[v] = 1
val_odd = sorted(num_odd.items(), key=lambda x:x[1])
val_even = sorted(num_even.items(), key=lambda x:x[1])
if len(val_odd) == 1 and len(val_even) == 1:
    if val_odd[0][0] == val_even[0][0]:
        # 全部同じ値 -> 半分変化させる
        print(n // 2)
    else:
        # すでに整ってる
        print(0)
else:
    # 奇数・偶数ともに必要なだけ変化させる
    if val_odd[-1][0] != val_even[-1][0]:
        # 最も多い値が違う場合はそれぞれその値を増やせばいい
        count_odd = n // 2 - val_even[-1][1]
        count_even = n // 2 - val_odd[-1][1]
        print(count_odd + count_even)
    else:
        # 同じ場合は、少ない方の第二頻出値を増やす
        c1 = n - val_odd[-1][1] - val_even[-2][1]
        c2 = n - val_odd[-2][1] - val_even[-1][1]
        print(min(c1, c2))