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:
n = int(input())
A = list(map(int, input().split()))
B = {}
C = {}
for i, a in enumerate(A):
  if i % 2 == 0:
    B[a] = B.get(a, 0) + 1
  else:
    C[a] = C.get(a, 0) + 1
Bs = sorted(B.items(), key = lambda x: x[1], reverse = True)
Cs = sorted(C.items(), key = lambda x: x[1], reverse = True)
if Bs[0][0] != Cs[0][0]:
  print(n - Bs[0][1] - Cs[0][1])
elif len(Bs) == 1:
  if len(Cs) == 1:
    print(n // 2)
  else:
    print(n - Bs[0][1] - Cs[1][1])
else:
  if len(Cs) == 1:
    print(n - Bs[1][1] - Cs[0][1])
  else:
    print(min(n - Bs[0][1] - Cs[1][1], n - Bs[1][1] - Cs[0][1]))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
l1=[0]*100001
l2=[0]*100001
if a.count(a[0])==n:
    print(n//2)
    exit()

for i in range(0,n,2):
    l1[a[i]]+=1
for i in range(1,n,2):
    l2[a[i]]+=1

x,y=max(l1),max(l2)
#print(x,y)
i_x,i_y=l1.index(x),l2.index(y)
#print(i_x,i_y)

L1=sorted(l1,reverse=True)
L2=sorted(l2,reverse=True)

if i_x == i_y:
    #奇数番目を2番目に多い数でそろえる場合
    ans=min(((n//2-L1[1])+(n//2-L2[0])),((n//2-L1[0])+(n//2-L2[1])))
else:
    ans=((n//2-L1[0])+(n//2-L2[0]))
print(ans)

Submission status: Accepted

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

ol =al[::2]
el =al[1::2]

from collections import Counter
col = Counter(ol)
cel = Counter(el)
scol = sorted(col.items(), key=lambda x: x[1], reverse=True)  
scel = sorted(cel.items(), key=lambda x: x[1], reverse=True)  
scol.append(("x",0))
scel.append(("y",0))


temp = scol[0][1] + scel[0][1]

if scol[0][0]==scel[0][0]:
    temp2 = scol[0][1]+scel[1][1]
    temp3 = scol[1][1]+scel[0][1]
    print(n-max(temp2,temp3))
    
else:
    print(n-temp)

Submission status: Accepted

Solution code:
from collections import *
n=int(input())
l=list(map(int,input().split()))
x,y=[],[]
c=0
for i in range(0,n,2):
    x.append(l[i])
for i in range(1,n,2):
    y.append(l[i])
xx=-1
if(len(set(x))==1):
    xx=x[0]
else:
    d=defaultdict(int)
    for i in x:
        d[i]+=1
    m=0
    xx=-1
    for i in d:
        if(d[i]>m):
            xx=i
            m=d[i]
    c+=sum(d.values())-m
if(len(set(y))>1 or (len(set(y))==1 and y[0]==xx)):
    d=defaultdict(int)
    for i in y:
        d[i]+=1
    m=0
    yy=-1
    for i in d:
        if(d[i]>m and i!=xx):
            yy=i
            m=d[i]
    c+=sum(d.values())-m
cc=0
yy=-1
if(len(set(y))==1):
    yy=y[0]
else:
    d=defaultdict(int)
    for i in y:
        d[i]+=1
    m=0
    yy=-1
    for i in d:
        if(d[i]>m):
            yy=i
            m=d[i]
    cc+=sum(d.values())-m
if(len(set(x))>1 or (len(set(x))==1 and x[0]==yy)):
    d=defaultdict(int)
    for i in x:
        d[i]+=1
    m=0
    xx=-1
    for i in d:
        if(d[i]>m and i!=yy):
            xx=i
            m=d[i]
    cc+=sum(d.values())-m
print(min(cc,c))

Submission status: Accepted

Solution code:
import collections

def main():
    n = int(input())
    V = list(map(int, input().split()))
    if len(set(V)) == 1:
        print(n // 2)
        exit()
    evens = []
    odds = []
    for idx, v in enumerate(V, start=1):
        if idx % 2 == 0:
            evens.append(v)
        else:
            odds.append(v)
    c_even = collections.Counter(evens)
    c_odd = collections.Counter(odds)
    even_most_common = c_even.most_common()[0]
    odd_most_common = c_odd.most_common()[0]

    if even_most_common[0] != odd_most_common[0]:
        print(count_replace(c_even, even_most_common[0], c_odd, odd_most_common[0], n))
    else:
        even_second_most_common = c_even.most_common()[1]
        odd_second_most_common = c_odd.most_common()[1]
        vals = []
        vals.append(count_replace(c_even, even_most_common[0], c_odd, odd_second_most_common[0], n))
        vals.append(count_replace(c_even, even_second_most_common[0], c_odd, odd_most_common[0], n))
        print(min(vals))


def count_replace(c_even, even, c_odd, odd, n):
    return n - c_even[even] - c_odd[odd]


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
vs = list(map(int, input().split()))

eq_lis = []
odd_lis = []
for i in range(n):
    if i %2 ==0:
        eq_lis.append(vs[i])
    else:
        odd_lis.append(vs[i])
        
a = Counter(eq_lis)
b = Counter(odd_lis)
a_val = list(a.values())
b_val = list(b.values())
a_max = max(a_val)
b_max = max(b_val)

a_sorted = sorted(a.items(), key= lambda x:x[1])
b_sorted = sorted(b.items(), key= lambda x:x[1])

if a_sorted[-1][0] == b_sorted[-1][0]:
    if len(a_sorted) == 1 and len(b_sorted) == 1:
        print(n-a_max)
    elif len(a_sorted)==1 and len(b_sorted) >1:
        print(n-a_max-b_sorted[-2][1])
    elif len(a_sorted)>1 and len(b_sorted) ==1:
        print(n-b_max-a_sorted[-2][1])
    else:
        if b_sorted[-2][1] >= a_sorted[-2][1]:
            print(n-a_max-b_sorted[-2][1])
        else:
            print(n-b_max-a_sorted[-2][1])    
else:
    print(n-a_max-b_max)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())

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

lisa=[0 for _ in range(10**5)]
lisb=[0 for _ in range(10**5)]

for i in range(n):
  if i%2==0:
    lisa[v[i]-1]+=1
  else:
    lisb[v[i]-1]+=1

maxnuma=-1
maxnumb=-1

maxa=max(lisa)
maxb=max(lisb)


for i in range(10**5):
  if lisa[i]==maxa:
    maxnuma=i
for i in range(10**5):
  if lisb[i]==maxb:
    maxnumb=i


if maxnuma!=maxnumb:
  print(n-maxa-maxb)
else:
  lisa.sort(reverse=True)
  lisb.sort(reverse=True)
  print(n-max(lisa[0]+lisb[1],lisa[1]+lisb[0]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
l1 = []
l2 = []
for i in range(n):
    if i % 2 == 0:
        l1.append(v[i])
    else:
        l2.append(v[i])
if Counter(l1).most_common()[0][0] != Counter(l2).most_common()[0][0]:
    print(len(l1)-Counter(l1).most_common()[0][1]+len(l2)-Counter(l2).most_common()[0][1])
elif min(len(set(l1)), len(set(l2))) >= 2:
    print(min(len(l1)-Counter(l1).most_common()[0][1]+len(l2)-Counter(l2).most_common()[1][1], len(l1)-Counter(l1).most_common()[1][1]+len(l2)-Counter(l2).most_common()[0][1]))
elif len(set(l1)) > 1:
    print(len(l1)-Counter(l1).most_common()[1][1]+len(l2)-Counter(l2).most_common()[0][1])
elif len(set(l2)) > 1:
    print(len(l1)-Counter(l1).most_common()[0][1]+len(l2)-Counter(l2).most_common()[1][1])
else:
    print(n//2)

Submission status: Accepted

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

v_even = [0]*100000
v_odd = [0]*100000

for i in range(n):
  if i%2==0:
    v_even[v[i]-1] += 1 
  else:
    v_odd[v[i]-1] += 1
    
res1 = max(v_even)
res2 = max(v_odd)
if v_even.index(res1) == v_odd.index(res2):
  if res1 > res2:
    v_odd[v_odd.index(res2)] = 0
    res2 = max(v_odd)
  elif res2 > res1:
    v_even[v_even.index(res1)] = 0
    res1 = max(v_even)
  elif res1 == res2:
    v_odd[v_odd.index(res2)] = 0
    v_even[v_even.index(res1)] = 0
    res3 = max(v_even)
    res4 = max(v_odd)
    if res3 > res4:
      res1 = res3
    else:
      res2 = res4
      
print(n//2-res1 + n//2-res2)

Submission status: Accepted

Solution code:
from collections import Counter

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

v0=list(Counter(v[::2]).items())
v1=list(Counter(v[1::2]).items())

v0.sort(key=lambda x:x[1],reverse=True)
v1.sort(key=lambda x:x[1],reverse=True)

k0,ma0=v0[0]
k1,ma1=v1[0]

if k0==k1 and len(v0)==1 and len(v1)==1:
    print(n//2)
elif k0==k1 and len(v0)==1:
    print((n+1)//2-v1[1][1]+n//2-ma0)
elif k0==k1 and len(v1)==1:
    print(n//2-v0[1][1]+(n+1)//2-ma1)
elif k0==k1:
    print(min((n+1)//2-v1[1][1]+n//2-ma0,n//2-v0[1][1]+(n+1)//2-ma1))
else:
    print((n+1)//2-ma0+n//2-ma1)

Submission status: Accepted

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

first = {}
second = {}

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

first = sorted(first.items(), key=lambda x: x[1])
second = sorted(second.items(), key=lambda x: x[1])

f = first.pop()
s = second.pop()

if f[0] == s[0]:
    if len(first) >= 1:
        if len(second) >= 1:
            f2 = first.pop()
            s2 = second.pop()

            if f2[1] + s[1] > f[1] + s2[1]:
                f = f2
            else:
                s = s2
        else:
            s = second.pop()
    elif len(second) >= 1:
        f = first.pop()
    else:
        s = (0, 0)

print(n-f[1]-s[1])

Submission status: Accepted

Solution code:
from  collections import Counter

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

c = Counter(v[::2])
d = Counter(v[1::2])

x = c.most_common()
y = d.most_common()

s = 0

if x[0][0] == y[0][0]:
    if len(x) > 1 and len(y) > 1:
        s = min(n - x[0][1] - y[1][1], n - x[1][1] - y[0][1])
    elif len(x) > 1:
        s = n - x[1][1] - y[0][1]
    elif len(y) > 1:
        s = n - x[0][1] - y[1][1]
    else:
        s = n // 2
else:
    s = n - x[0][1] - y[0][1]

print(s)

Submission status: Accepted

Solution code:
def find_max( list ):
    x = 0
    x2 = 0
    x_i = 0
    x2_i = 0
    for i in range( len(list) ):
        if x < list[i]:
            x = list[i]
            x_i = i
    for i in range( len(list) ):
        if i == x_i:
            continue
        if x2 < list[i] <= x:
            x2 = list[i]
            x2_i = i
    return x,x2,x_i,x2_i

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

v_sort = sorted( v )

v_odd = [ 0 for i in range( v_sort[-1] + 1 ) ]
#奇数
v_eve = [ 0 for i in range( v_sort[-1] + 1 ) ]
#偶数
for i in range( n ):
    if i % 2 == 0:
        v_eve[ v[i] ] += 1
    if i % 2 == 1:
        v_odd[ v[i] ] += 1

vex,vex2,vex_i,vex2_i = find_max( v_eve )
vox,vox2,vox_i,vox2_i = find_max( v_odd )

ok = 0
# ok: figure that need not to fix
if vex_i != vox_i:
    ok = vex + vox
else:
    ok = max( vex + vox2 , vex2 + vox )

ans = n - ok
print( ans )

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
a = {}
b = {}
for i in range(n // 2):
    key_a = l[i * 2]
    key_b = l[i * 2 + 1]
    if key_a in a:
        a[key_a] = a[key_a] + 1
    else:
        a[l[i * 2]] = 1
    if key_b in b:
        b[key_b] = b[key_b] + 1
    else:
        b[key_b] = 1
a = sorted(a.items(), key=lambda x: x[1], reverse=True)
b = sorted(b.items(), key=lambda x: x[1], reverse=True)
ca_max = a[0][0]
cb_max = b[0][0]
ca_max_num = a[0][1]
cb_max_num = b[0][1]
same_num = ca_max_num + cb_max_num
if ca_max == cb_max:
    if len(a) > 1:
        next_ca_max = a[1][1]
    else:
        next_ca_max = 0
    if len(b) > 1:
        next_cb_max = b[1][1]
    else:
        next_cb_max = 0
    same_num = max(ca_max_num + next_cb_max, cb_max_num + next_ca_max)
print(n - same_num)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
a = {}
b = {}
for i in range(n // 2):
    key_a = l[i * 2]
    key_b = l[i * 2 + 1]
    if key_a in a:
        a[key_a] = a[key_a] + 1
    else:
        a[l[i * 2]] = 1
    if key_b in b:
        b[key_b] = b[key_b] + 1
    else:
        b[key_b] = 1
a = sorted(a.items(), key=lambda x: x[1], reverse=True)
b = sorted(b.items(), key=lambda x: x[1], reverse=True)
# a = list(a.items())
# b = list(b.items())
# print(a)
# print(b)
ca_max = a[0][0]
cb_max = b[0][0]
ca_max_num = a[0][1]
cb_max_num = b[0][1]
same_num = ca_max_num + cb_max_num
if ca_max == cb_max:
    if len(a) > 1:
        next_ca_max = a[1][1]
    else:
        next_ca_max = 0
    if len(b) > 1:
        next_cb_max = b[1][1]
    else:
        next_cb_max = 0
    same_num = max(ca_max_num + next_cb_max, cb_max_num + next_ca_max)
print(n - same_num)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import Counter
# from numba import njit

# input = stdin.readline

# @njit
def solve(n,v):
  if all(x == v[0] for x in v):
    return n//2
  v_even = v[::2]
  v_odd  = v[1::2]

  d_even = list(sorted(Counter(v_even).items(),key=lambda x: x[1],reverse=True))
  d_odd  = list(sorted(Counter(v_odd).items(),key=lambda x: x[1],reverse=True))
  e0 = d_even[0]
  o0 = d_odd[0]
  if e0[0] != o0[0]:
    return n - e0[1] - o0[1]
  e1 = d_even[1]
  o1 = d_odd[1]
  return min(n-e0[1]-o1[1],n-e1[1]-o0[1])



def main():
  N = int(input())
  # N,M = map(int,input().split())
  v = list(map(int,input().split()))
  print(solve(N,v))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

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

v1 = [v[i] for i in range(n) if i%2 == 1]
v2 = [v[i] for i in range(n) if i%2 == 0]
            
import collections

v1n = collections.Counter(v1)
v2n = collections.Counter(v2)

v1ns = v1n.most_common(2)
v2ns = v2n.most_common(2)

a = v2ns[0][1]
c = v1ns[0][1]
if v1ns[0][0]!=v2ns[0][0]:
    print(n-a-c)
else:
    if len(v1ns)==1:
        d = 0
    else:
        d = v1ns[1][1]
    if len(v2ns)==1:
        b = 0
    else:
        b = v2ns[1][1]
    print(min(n-a-d,n-c-b))

Submission status: Accepted

Solution code:
import collections
N = int(input())

O = []
E = []
A = list(map(lambda a: int(a), input().split(" ")))
for i in range(N):
  if i % 2 == 0:
    E.append(A[i])
  else:
    O.append(A[i])

CE = collections.Counter(E)
CO = collections.Counter(O)
CE2 = CE.most_common(2)
CO2 = CO.most_common(2)
if CE2[0][0] != CO2[0][0]:
  print(len(E) + len(O) - CE2[0][1] - CO2[0][1])
elif CE2[0][0] == CO2[0][0]:
  if len(CE2) == 1 and len(CO2) == 1:
    print(min([CE2[0][1], CO2[0][1]]))
  elif len(CE2) > 1 and len(CO2) == 1:
    print(len(E) + len(O) - CE2[1][1] - CO2[0][1])
  elif len(CE2) == 1 and len(CO2) > 1:
    print(len(E) + len(O) - CE2[0][1] - CO2[1][1])
  elif CE2[0][1] + CO2[1][1] > CO2[0][1] + CE2[1][1]:
    print(len(E) + len(O) - CE2[0][1] - CO2[1][1])
  elif CE2[0][1] + CO2[1][1] <= CO2[0][1] + CE2[1][1]:
    print(len(E) + len(O) - CE2[1][1] - CO2[0][1])

Submission status: Accepted

Solution code:
import sys
from collections import Counter
from operator import itemgetter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    n, *V = map(int, read().split())

    counter1 = Counter(V[::2])
    counter2 = Counter(V[1::2])

    vec1 = sorted(counter1.items(), reverse=True, key=itemgetter(1))
    vec2 = sorted(counter2.items(), reverse=True, key=itemgetter(1))

    if vec1[0][0] != vec2[0][0]:
        ans = n - vec1[0][1] - vec2[0][1]
    elif len(vec1) == len(vec2) == 1:
        ans = n // 2
    else:
        tmp = []
        if len(vec1) > 1:
            tmp.append(vec1[1][1] + vec2[0][1])
        if len(vec2) > 1:
            tmp.append(vec1[0][1] + vec2[1][1])
        ans = n - max(tmp)

    print(ans)

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
a=[0]*(10**5)
b=[0]*(10**5)
s,t=0,0
o,e,oi,ei=0,0,0,0
for i in range(1,n+1):
    if i%2==1:
        a[v[i-1]-1]+=1
        s+=1
    else:
        b[v[i-1]-1]+=1
        t+=1
o=max(a)
oi=[]
e=max(b)
ei=[]
for i in range(10**5):
    if a[i]==o:
        oi.append(i)
    if b[i]==e:
        ei.append(i)
if len(oi)==1 and len(ei)==1 and oi[0]==ei[0]:
    a.sort(reverse=True)
    b.sort(reverse=True)
    print(min(t-e+s-a[1],t-b[1]+s-o))
else:
    print(s+t-e-o)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
odd = [0] * (10 ** 5 + 5)
even = [0] * (10 ** 5 + 5)
for i in range(n):
    if i % 2 == 0:
        even[v[i]] += 1
    else:
        odd[v[i]] += 1
sum_odd = sum(odd)
sum_even = sum(even)
ans = sum_odd + sum_even
max_odd = max(odd)
max_even = max(even)
if odd.index(max_odd) != even.index(max_even):
    print(sum_odd + sum_even - max_even - max_odd)
else:
    odd.sort()
    even.sort()
    if odd[-1] - odd[-2] > even[-1] - even[-2]:
        ans -= odd[-1] + even[-2]
    else:
        ans -= odd[-2] + even[-1]
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))

o = Counter(v[::2]).most_common()
e = Counter(v[1::2]).most_common()
if o[0][0] == e[0][0]:
  if len(o) == 1: print(e[0][1])
  else:
    o1,o2 = e[0][1], e[1][1]
    e1,e2 = o[0][1], o[1][1]
  
    ans = min(n-o1-o2, n-e1-e2)
    print(ans)
else: print(n-o[0][1]-e[0][1])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
ls = list(map(int,input().split()))
lso = ls[::2]
lse = ls[1::2]
lso.sort()
lse.sort(reverse=True)
co = Counter(lso)
ce = Counter(lse)
co1,co2 = co.most_common()[0][0],co.most_common()[0][1]
ce1,ce2 = ce.most_common()[0][0],ce.most_common()[0][1]
coo1,coo2,cee1,cee2 = 0,0,0,0
if co2 != n//2:
    coo1,coo2 = co.most_common()[1][0],co.most_common()[1][1]
if ce2 != n//2:
    cee1,cee2 = ce.most_common()[1][0],ce.most_common()[1][1]
if co1 == ce1:
    print(n-max(co2+cee2,ce2+coo2))
else:
    print(n-co2-ce2)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))

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

if f[0][0]!=b[0][0]:print(n-f[0][1]-b[0][1]);exit()

if f[0][1]==n//2:print(n//2)
else:print(n-b[0][1]-f[1][1] if f[1][1]>b[1][1] else n-f[0][1]-b[1][1])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int ,input().split()))

o = Counter(v[::2]).most_common() # 偶数列の最頻値の順に並べる
e = Counter(v[1::2]).most_common() # 奇数列の最頻値の順に並べる

if o[0][0] == e[0][0]: # 最頻値が偶数列と奇数列で同じかどうか判定
  if len(o) == 1: print(n//2) # 値のバリエーションが最頻値のみの場合は片方の数列を書き換えて終わり
  else:
    # 最終的に数列上に残る値の種類は2種類のみの為、上位2種の値へ全て揃える
    # 下記の2パターンで書き換える数が少ない方が回答になる
    o1,o2 = e[0][1], e[1][1]
    e1,e2 = o[0][1], o[1][1]
    
    ans = min(n-e1-o2, n-e2-o1)
    print(ans)
else:
  print(n - o[0][1] - e[0][1]) #最頻値が異なる場合は、全体の数からそれぞれの最頻値の数を引く

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
m=n//2
x=[a[i] for i in range(n) if i%2==0] #kisuu
y=[a[i] for i in range(n) if i%2==1] #guusuu
x=sorted(x)
y=sorted(y)
inx=[]
iny=[]
p=1
q=1

for i in range(m-1):
    if x[i]==x[i+1]:
        p+=1
    else:
        inx.append([p,x[i]])
        p=1
inx.append([p,x[-1]])
for i in range(m-1):
    if y[i]==y[i+1]:
        q+=1
    else:
        iny.append([q,y[i]])
        q=1
iny.append([q,y[-1]])
inx=sorted(inx)
iny=sorted(iny)
inx.insert(0,[0,0])
iny.insert(0,[0,0])

if inx[-1][1]!=iny[-1][1]:
    print(2*m-inx[-1][0]-iny[-1][0])
else:
    print(min(2*m-inx[-1][0]-iny[-2][0],2*m-inx[-2][0]-iny[-1][0]))

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
odd = dict()
even = dict()
odd[-1] = 0
even[-1] = 0

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

odd_values = sorted(list(odd.values()), reverse=True)
for key, val in odd.items():
    if val == odd_values[0]:
        odd_1 = key
    elif val == odd_values[1]:
        odd_2 = key

even_values = sorted(list(even.values()), reverse=True)
for key, val in even.items():
    if val == even_values[0]:
        even_1 = key
    elif val == even_values[1]:
        even_2 = key

if odd_1 != even_1:
    res = n - odd_values[0] - even_values[0]
else:
    res = min(n - odd_values[0] - even_values[1], 
              n - odd_values[1] - even_values[0])
print(res)
# print(odd)
# print(even)

Submission status: Accepted

Solution code:
import sys
from collections import Counter


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    V = nl()
    V_e = V[0::2]
    V_o = V[1::2]
    V_e_c = Counter(V_e)
    V_o_c = Counter(V_o)

    if V_e_c.most_common()[0][0] != V_o_c.most_common()[0][0]:
        ans = len(V) - \
            V_e_c.most_common()[0][1] - V_o_c.most_common()[0][1]

    else:
        if len(V_e_c.most_common()) == 1 or len(V_o_c.most_common()) == 1:
            if len(V_e_c.most_common()) == 1 and len(V_o_c.most_common()) == 1:
                ans = len(V) // 2
            else:
                if len(V_e_c.most_common()) != 1:
                    ans = len(V) // 2 - V_e_c.most_common()[1][1]
                else:
                    ans = len(V) // 2 - V_o_c.most_common()[1][1]

        else:
            ans = len(V) - max(
                V_e_c.most_common()[0][1] + V_o_c.most_common()[1][1],
                V_e_c.most_common()[1][1] + V_o_c.most_common()[0][1],
            )

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def get_ans(v, ans):
    while len(v) != 1:
        ans += v.pop()
    return ans


def main():
    n = ni()
    V = nl()
    V_e = V[0::2]
    V_o = V[1::2]
    V_e_c = Counter(V_e)
    V_o_c = Counter(V_o)
    # num_V_e = sorted(list(V_e_c.values()))[::-1]
    # num_V_o = sorted(list(V_o_c.values()))[::-1]

    if V_e_c.most_common()[0][0] != V_o_c.most_common()[0][0]:
        ans = len(V) - \
            V_e_c.most_common()[0][1] - V_o_c.most_common()[0][1]

    else:
        if len(V_e_c.most_common()) == 1 or len(V_o_c.most_common()) == 1:
            if len(V_e_c.most_common()) == 1 and len(V_o_c.most_common()) == 1:
                ans = len(V) // 2
            else:
                if len(V_e_c.most_common()) != 1:
                    ans = len(V) // 2 - V_e_c.most_common()[1][1]
                else:
                    ans = len(V) // 2 - V_o_c.most_common()[1][1]

        else:
            ans = len(V) - max(
                V_e_c.most_common()[0][1] + V_o_c.most_common()[1][1],
                V_e_c.most_common()[1][1] + V_o_c.most_common()[0][1],
            )

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

n = int(input())
v = [int(i) for i in input().split()]
con_odd = []
con_even = []
for i in range(0,n,2):
    con_odd.append(v[i]) 
    con_even.append(v[i+1])

b = collections.Counter(con_odd)
c = sorted(b.items(), key=lambda x:x[1],reverse = True)
b_2 = collections.Counter(con_even)
c_2 = sorted(b_2.items(), key=lambda x:x[1],reverse = True)
if len(c) == 1 and len(c_2) == 1:
    if c[0][0] == c_2[0][0]:
        ans = n//2
    else:
        ans = 0
elif len(c) == 1 or len(c_2) == 1:
    if c[0][0] == c_2[0][0]:
        if len(c) == 1:
            ans_1 = len(con_odd) - c[0][1]
            ans_2 = len(con_even) - c_2[1][1]
            ans = ans_1 + ans_2
        else:
            ans_1 = len(con_odd) - c[1][1]
            ans_2 = len(con_even) - c_2[0][1]
            ans = ans_1 + ans_2
    else:
        ans_1 = len(con_odd) - c[0][1]
        ans_2 = len(con_even) - c_2[0][1]
        ans = ans_1 + ans_2
            
elif c[0][0] == c_2[0][0]:
    if c[0][1] + c_2[1][1] > c[1][1] + c_2[0][1]:
        ans_1 = len(con_odd) - c[0][1]
        ans_2 = len(con_even) - c_2[1][1]
        ans = ans_1 + ans_2
    else:
        ans_1 = len(con_odd) - c[1][1]
        ans_2 = len(con_even) - c_2[0][1]
        ans = ans_1 + ans_2
else:
    ans_1 = len(con_odd) - c[0][1]
    ans_2 = len(con_even) - c_2[0][1]
    ans = ans_1 + ans_2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ar=[int(i) for i in input().strip().split(" ")]
from collections import defaultdict
ev=defaultdict(int)
od=defaultdict(int)

def g(i):
    return (i[1],i[0])

for i in range(0,n,2):
    od[ar[i]]+=1

for i in range(1,n,2):
    ev[ar[i]]+=1

odd=list(od.items())
even=list(ev.items())
odd.append((0,0))
even.append((0,0))
odd.sort(key=g)
even.sort(key=g)

o1=odd[-1]
o2=odd[-2]
e1=even[-1]
e2=even[-2]
if o1[0]!=e1[0]:
    print(n-o1[1]-e1[1])
else:
    a1=(n-o1[1]-e2[1])
    a2=(n-o2[1]-e1[1])
    print(min(a1,a2))

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int, input().split()))
l1=l[0::2]
l2=l[1::2]
import collections
c1 = collections.Counter(l1)
#print(c1)
# Counter({'a': 4, 'c': 2, 'b': 1})
c2 =collections.Counter(l2)  
c0 =collections.Counter(l)
if len(c0)==1:
  print(int(n/2))
else:
  if c1.most_common()[0]==c2.most_common()[0]:
    print(min(n-c1.most_common()[0][1]-c2.most_common()[1][1], n-c1.most_common()[1][1]-c2.most_common()[0][1]))
  else:
    print(n-c1.most_common()[0][1]-c2.most_common()[0][1])

Submission status: Accepted

Solution code:
import collections

n=int(input())
m=n//2
a=list(map(int,input().split()))
g=[]
k=[]
for x in range(n):
  if (x+1)%2==1:
    k.append(a[x])
  else:
    g.append(a[x])
    
gg=collections.Counter(g)
kk=collections.Counter(k)
aa=collections.Counter(a)

if len(aa)==1:
  print(n//2)
  
else:
  if gg.most_common()[0]==kk.most_common()[0]:
    
      print(min(m-gg.most_common()[0][1]+m-kk.most_common()[1][1],m-kk.most_common()[0][1]+m-gg.most_common()[1][1]))
   
     
  else:
    print(n-gg.most_common()[0][1]-kk.most_common()[0][1])

Submission status: Accepted

Solution code:
import collections

n=int(input())
m=n//2
a=list(map(int,input().split()))
g=[]
k=[]
for x in range(n):
  if (x+1)%2==1:
    k.append(a[x])
  else:
    g.append(a[x])
    
gg=collections.Counter(g)
kk=collections.Counter(k)
aa=collections.Counter(a)

if len(aa)==1:
  print(n//2)
  
else:
  if gg.most_common()[0]==kk.most_common()[0]:
    if len(gg)>1 and len(kk)>1:
      print(min(m-gg.most_common()[0][1]+m-kk.most_common()[1][1],m-kk.most_common()[0][1]+m-gg.most_common()[1][1]))
    elif len(gg)==1:
      print(m-kk.most_common()[1][1])
    else:
      print(m-gg.most_common()[1][1])
  else:
    print(n-gg.most_common()[0][1]-kk.most_common()[0][1])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
a = v[1::2]
b = v[0::2]
a2 = Counter(a)
b2 = Counter(b)

if a2.most_common()[0][0] != b2.most_common()[0][0]:
    print(n-a2.most_common()[0][1]-b2.most_common()[0][1])
else:
    if len(a2.most_common()) == len(b2.most_common()) == 1:
        print(len(a)-a2.most_common()[0][1]+len(b))
    elif len(a2.most_common()) == 1:
        if len(b)-b2.most_common()[1][1] >= len(a)+len(b)-b2.most_common()[0][1]:
            print(len(a)+len(b)-b2.most_common()[0][1])
        else:
            print(len(b)-b2.most_common()[1][1])
    elif len(b2.most_common()) == 1:
        if len(a)-a2.most_common()[1][1] >= len(a)+len(b)-a2.most_common()[0][1]:
            print(len(a)+len(b)-a2.most_common()[0][1])
        else:
            print(len(a)-a2.most_common()[1][1])
    else:
        if len(a)-a2.most_common()[0][1]+len(b)-b2.most_common()[1][1] >= len(a)-a2.most_common()[1][1]+len(b)-b2.most_common()[0][1]:
            print(len(a)-a2.most_common()[1][1]+len(b)-b2.most_common()[0][1])
        else:
            print(len(a)-a2.most_common()[0][1]+len(b)-b2.most_common()[1][1])

Submission status: Accepted

Solution code:
from collections import Counter

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

kisu = A[0::2]
gusu = A[1::2]

CK = Counter(kisu)
CG = Counter(gusu)
CKM = CK.most_common(2)
CGM = CG.most_common(2)

if len(CKM) == 1 and len(CGM) == 1:
    if CKM[0][0] == CGM[0][0]:
        ans = n // 2
    else:
        ans = 0
elif len(CKM) == 1:
    if CKM[0][0] == CGM[0][0]:
        ans = n // 2 - CGM[1][1]
    else:
        ans = n // 2 - CGM[0][1]
elif len(CGM) == 1:
    if CKM[0][0] == CGM[0][0]:
        ans = n // 2 - CKM[1][1]
    else:
        ans = n // 2 - CKM[0][1]
else:
    if CKM[0][0] == CGM[0][0]:
        ans1 = n // 2 - CKM[1][1] + n // 2 - CGM[0][1]
        ans2 = n // 2 - CGM[1][1] + n // 2 - CKM[0][1]
        ans = min(ans1, ans2)
    else:
        ans = n - (CKM[0][1] + CGM[0][1])

print(ans)

Submission status: Accepted

Solution code:
import copy
import numpy as np

n = int(input())
v = list(map(int,input().split()))
a = v[0::2] 
b = v[1::2]

cnta = np.zeros(10**5+1, dtype=int)
cntb = np.zeros(10**5+1, dtype=int)
for i in range(n//2):
    ca, cb = a[i], b[i]
    cnta[ca] += 1
    cntb[cb] += 1

la = np.zeros((2,2), dtype=int)
lb = np.zeros((2,2), dtype=int)
for i in range(2):
    ia = cnta.argmax()
    ib = cntb.argmax()
    la[i][0], la[i][1] = ia, cnta[ia]
    lb[i][0], lb[i][1] = ib, cntb[ib]
    cnta[ia] = 0
    cntb[ib] = 0

if la[0][0] != lb[0][0]:
    ans = n//2 - la[0][1] + n//2 - lb[0][1]
else:
    ans = min(n//2 - la[0][1] + n//2 - lb[1][1], n//2 - la[1][1] + n//2 - lb[0][1])
print(ans)

Submission status: Accepted

Solution code:
import collections as co
if __name__ == '__main__':

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

	S = A[1::2]
	T = A[0::2]

	total_cnt = 0

	#全体をチェック
	U = set(A)
	if len(U) == 1:
		total_cnt = len(A[1::2])
		print(total_cnt)
		exit()

	c1 = []
	c2 = []
	#奇数列をチェック
	s = co.Counter(S)
	c1 = s.most_common()
	#これが秘策
	c1.append((0,0))
	#偶数列をチェック
	t = co.Counter(T)
	c2 = t.most_common()
	#これが秘策
	c2.append((0,0))


	#チェック
	#最頻値が異なるなら
	if c1[0][0] != c2[0][0]:
		#それ以外の数を総カウント-1
		total_cnt = len(S) - c1[0][1] 
		total_cnt += len(T) - c2[0][1]
	else:
		#最頻値が同じならその数が同じか？
		if c1[0][1] > c2[0][1]:
			total_cnt = len(S) - c1[0][1]
			total_cnt += len(T) - c2[1][1]
		elif c1[0][1] < c2[0][1]:
			total_cnt = len(S) - c1[1][1]
			total_cnt += len(T) - c2[0][1]
		else:
			#同じ場合、２番目の数値を比較して取り換え
			if c1[1][1] > c2[0][1]:
				total_cnt = len(S) - c1[1][1]
				total_cnt += len(T) - c2[0][1]
			elif c1[1][1] < c2[1][1]:
				total_cnt = len(T) - c2[1][1]
				total_cnt += len(S) - c1[0][1]
			else:
				#どちらでもよい
				total_cnt = len(S) - c1[1][1]
				total_cnt += len(T) - c2[0][1]
				
	print(total_cnt)