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 defaultdict

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

def count(vs):
    d1 = defaultdict(int)
    d2 = defaultdict(int)
    for i, v in enumerate(vs):
        if i % 2 == 0:
            d1[v] += 1
        else:
            d2[v] += 1
    return d1, d2

d1, d2 = count(vs)
d1 = sorted([(v, k) for k, v in d1.items()], reverse=True)
d2 = sorted([(v, k) for k, v in d2.items()], reverse=True)
n1, v1 = d1[0]
n2, v2 = d2[0]
if v1 == v2:
    n1_2 = 0 if len(d1) == 1 else d1[1][0]
    n2_2 = 0 if len(d2) == 1 else d2[1][0]
    total = max(n1 + n2_2, n2 + n1_2)
else:
    total = n1 + n2

print(n - total)

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys
n=int(input().strip())
v=list(map(int, input().strip().split()))
counterO=defaultdict(int)
counterE=defaultdict(int)
for i in v[::2]:
    counterO[i]+=1
for i in v[1::2]:
    counterE[i]+=1
cOO=sorted(counterO.items(),key=lambda x:x[1],reverse=True)
cEE=sorted(counterE.items(),key=lambda x:x[1],reverse=True)
iO,iE=0,0
if cOO[iO][0]!=cEE[iE][0]:
    ans=sum(x[1] for x in cOO[:iO]+cOO[iO+1:])+sum(x[1] for x in cEE[:iE]+cEE[iE+1:])
else:
    iO2=iO+1
    iE2=iE+1
    ans1=sum(x[1] for x in cOO[:iO2]+cOO[iO2+1:])+sum(x[1] for x in cEE[:iE]+cEE[iE+1:])
    ans2=sum(x[1] for x in cOO[:iO]+cOO[iO+1:])+sum(x[1] for x in cEE[:iE2]+cEE[iE2+1:])
    ans=min(ans1,ans2)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
e = [0] * (n // 2)
o = [0] * (n // 2)
for i in range(n):
    if i % 2 == 0:
        e[i // 2] = v[i]
    else:
        o[(i-1) // 2] = v[i]
ce = Counter(e)
co = Counter(o)

if ce.most_common()[0][0] == co.most_common()[0][0]:
    if len(ce) == 1 and len(co) == 1:
        print(n // 2)
    else:
        print(n-max(ce.most_common()[0][1]+co.most_common()[1][1], ce.most_common()[1][1]+co.most_common()[0][1]))
else:
    print(n-ce.most_common()[0][1]-co.most_common()[0][1])

Submission status: Accepted

Solution code:
import collections

n = int(input())
array = list(map(int,input().split(" ")))

array1 = []
array2 = []

for i in range(int(n/2)):
    array1.append(array[i*2])
    array2.append(array[i*2+1])

a1 = collections.Counter(array1)
a2 = collections.Counter(array2)

a12 = sorted(a1.items(),key = lambda x: -x[1])
a22 = sorted(a2.items(),key = lambda x: -x[1])

if a12[0][0] != a22[0][0]:
    print(n- a12[0][1] - a22[0][1])
else:
    if len(a12) == 1:
        if len(a22) == 1:
            print(int(n/2))
        else:
            print(a22[0][1])
    elif len(a22) == 1:
        print(a12[0][1])
    else:
        print(n - max(a12[0][1] + a22[1][1],a12[1][1] + a22[0][1]))

Submission status: Accepted

Solution code:
n = int(input())
v = input().split()
for i in range(len(v)):
    v[i] = int(v[i])
nums = [[],[]]
fr = [[],[]]
for i in range(len(v)):
    idx = (i+1)%2
    if v[i] in nums[idx]:
        fr_idx = nums[idx].index(v[i])
        fr[idx][fr_idx] += 1
    else:
        nums[idx].append(v[i])
        fr[idx].append(1)
        


fr_sort = [[0,0,0,0]+sorted(fr[0]),[0,0,0,0]+sorted(fr[1])]
maxfr = [fr_sort[0][-1],fr_sort[1][-1],fr_sort[0][-2],fr_sort[1][-2]]
maxnum = [[],[],[],[]]

for i in [0,1,2,3]:
    for j in range(len(nums[i%2])):
        if fr[i%2][j] == maxfr[i]:
            maxnum[i].append(nums[i%2][j])
                   
if maxnum[0] == maxnum[1]:
    m = max([maxfr[0]+maxfr[3],maxfr[1]+maxfr[2]])
else:
    m = maxfr[0] + maxfr[1]
                   
print(n-m)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=list(map(int, input().split()))
b=[a[i] for i in range(n) if i%2==0]
c=[a[i] for i in range(n) if i%2==1]
b2=Counter(b)
c2=Counter(c)
b3=b2.most_common(3)
c3=c2.most_common(3)
ans=n
for i in b3:
    for j in c3:
        if i[0]!=j[0]:
            ans=min(ans,n-i[1]-j[1])
        else:
            ans=min(ans,n-max(i[1],j[1]))
print(ans)

Submission status: Accepted

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

d = [{}, {}]
for i in range(n):
    if vvv[i] not in d[i%2].keys():
        d[i%2][vvv[i]]=1
    else:
        d[i%2][vvv[i]]+=1

li1 = [(v,k) for (k,v) in d[0].items()]
li2 = [(v,k) for (k,v) in d[1].items()]

if max(li1)[1] != max(li2)[1]:
    print(n - (max(d[0].values()) + max(d[1].values())))
else:
    v0 = d[0].pop(max(li1)[1])
    v1 = d[1].pop(max(li2)[1])
    
    if len(d[0])==0:
        dtmp0 = 0
    else:
        dtmp0 = max(d[0].values())
        
    if len(d[1])==0:
        dtmp1 = 0
    else:
        dtmp1 = max(d[1].values())
    print(n - max(v0 +dtmp1, dtmp0+v1))

Submission status: Accepted

Solution code:
import collections

N = int(input())
V = list(map(int, input().split()))
v1 = []
v2 = []
for i in range(N):
  if i % 2 == 0:
    v1.append(V[i])
  else:
    v2.append(V[i])
    
c1 = collections.Counter(v1)
c2 = collections.Counter(v2)

a1 = c1.most_common()
a2 = c2.most_common()

if len(a1)==1:
  k1=0
else:
  k1=a1[1][1]
if len(a2)==1:
  k2=0
else:
  k2=a2[1][1]

if a1[0][0] != a2[0][0]:
  print(len(V)-a1[0][1]-a2[0][1])
elif a1[0][1]+k2 > k1+a2[0][1]:
  print(len(V)-a1[0][1]-k2) 
else:
  print(len(V)-k1-a2[0][1])

Submission status: Accepted

Solution code:
n=int(input())
#n,k=map(int,input().split())
l=[int(i) for i in input().split()]
e,o={0:0},{0:0}
for a,i in enumerate(l):
    if a%2:
        if i in o: o[i]+=1
        else: o[i]=1
    else:
        if i in e: e[i]+=1
        else: e[i]=1
chko,chke=[0,0,0,0],[0,0,0,0]
for i in e:
    if e[i]>=chke[1]:
        chke[3]=chke[1]
        chke[2]=chke[0]
        chke[1]=e[i]
        chke[0]=i
    elif e[i]>=chke[3]:
        chke[3]=e[i]
        chke[2]=i
for i in o:
    if o[i]>=chko[1]:
        chko[3]=chko[1]
        chko[2]=chko[0]
        chko[1]=o[i]
        chko[0]=i
    elif o[i]>=chko[3]:
        chko[3]=o[i]
        chko[2]=i
#print(e,o)
#print(chko,chke)
if chko[0]!=chke[0]:
    print((n//2-chko[1])+(n//2-chke[1]))
else:
    ans=min((n//2-chko[1])+(n//2-chke[3]),(n//2-chko[3])+(n//2-chke[1]))
    print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    N = int(input_raw[0])
    input_raw = read_func().strip().split()
    A_dic = {}
    B_dic = {}
    for i in range(0, N):
        if i % 2 == 0:
            if input_raw[i] in A_dic:
                A_dic[input_raw[i]] += 1
            else:
                A_dic[input_raw[i]] = 1
        else:
            if input_raw[i] in B_dic:
                B_dic[input_raw[i]] += 1
            else:
                B_dic[input_raw[i]] = 1

    max_A_num = 0
    max_B_num = 0
    max_A = 0
    max_B = 0
    for A in A_dic:
        if max_A_num < A_dic[A]:
            max_A_num = A_dic[A]
            max_A = A
    for B in B_dic:
        if max_B_num < B_dic[B]:
            max_B_num = B_dic[B]
            max_B = B

    max_A_num2 = 0
    max_B_num2 = 0
    if max_A == max_B:

            for A in A_dic:
                if A != max_A:
                    max_A_num2 = max(max_A_num2, A_dic[A])

            for B in B_dic:
                if B != max_B:
                    max_B_num2 = max(max_B_num2, B_dic[B])

            if max_A_num2 > max_B_num2:
                max_A_num = max_A_num2
            else:
                max_B_num = max_B_num2

    print N - max_A_num - max_B_num




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

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

vo=[]
ve=[]
i = 0
while i < n/2:
  ve.append(v[2*i])
  vo.append(v[2*i+1])
  i += 1
cve = collections.Counter(ve).most_common(2)
cvo = collections.Counter(vo).most_common(2)
ans = 0
if cve[0][0] == cvo[0][0]:
  if len(cve) == 1 and len(cvo) == 1:
    print(int(n/2))
  elif len(cve) == 1:
    print(int(n/2 - cvo[1][1]))
  elif len(cvo) == 1:
    print(int(n/2 - cve[1][1]))
  else:
    print(min(n-cvo[0][1]-cve[1][1], n-cvo[1][1]-cve[0][1]))
else:
  print(n - cve[0][1] - cvo[0][1])

Submission status: Accepted

Solution code:
# coding:utf-8

import numpy as np


INF = float('inf')


def inpl(): return list(map(int, input().split()))


N = int(input())
V = inpl()

A = [0 for _ in range(10**5+1)]
for i in range(0, N, 2):
    A[V[i]] += 1

B = [0 for _ in range(10**5+1)]
for i in range(1, N, 2):
    B[V[i]] += 1

A = np.array(A)
B = np.array(B)

if A.argmax() == B.argmax():
    A.sort()
    B.sort()
    tmp = max(A[-1] + B[-2] , A[-2] + B[-1])
    ans = N - tmp
else:
    A.sort()
    B.sort()
    ans = N - A[-1] - B[-1]

print(ans)

Submission status: Accepted

Solution code:
from operator import itemgetter
N = int(input())
A = list(map(int, input().split()))

O = {}
E = {}

for i in range(N//2):
  if A[2*i] in O:
    O[A[2*i]] += 1
  else:
    O[A[2*i]] = 1
  if A[2*i+1] in E:
    E[A[2*i+1]] += 1
  else:
    E[A[2*i+1]] = 1

O = sorted(O.items(), key=itemgetter(1), reverse=True)      
E = sorted(E.items(), key=itemgetter(1), reverse=True)   

if O[0][0] != E[0][0]:
  print(N - O[0][1] - E[0][1])
else:
  print(min(N - O[0][1] - (E[1][1] if len(E) > 1 else 0), N - E[0][1] - (O[1][1] if len(O) > 1 else 0)))

Submission status: Accepted

Solution code:
n = int(input())
odd = [[i, 0] for i in range(100000)]
even = [[i, 0] for i in range(100000)]
arr = list(map(int, input().split()))
odd_e = True
even_e = True
for i, e in enumerate(arr):
    if i % 2 == 0:
        odd[e - 1][1] += 1
        if odd_e and arr[0] != e:
            odd_e = False
    else:
        even[e - 1][1] += 1
        if even_e and arr[1] != e:
            even_e = False
odd.sort(key=lambda z: z[1])
even.sort(key=lambda z: z[1])
if odd[-1][0] == even[-1][0]:
    if odd_e or even_e:
        ans = n - even[-1][1]
    elif odd[-2][1] > even[-2][1]:
        ans = n - even[-1][1] - odd[-2][1]
    else:
        ans = n - even[-2][1] - odd[-1][1]
else:
    ans = n - odd[-1][1] - even[-1][1]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
O = [0 for k in range(max(v)+1)]
E = [0 for k in range(max(v)+1)]

for k in range(n):
    if k%2 == 0:
        E[v[k]] += 1
    else:
        O[v[k]] += 1
if E.index(max(E)) != O.index(max(O)):
    print(n-max(E)-max(O))
else:
    E = sorted(E)
    O = sorted(O)
    print(min((n-max(E)-O[-2]),(n-max(O)-E[-2])))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import defaultdict
from operator import itemgetter
def solve():
    n = int(input())
    V = list(map(int, input().split()))
    l = defaultdict(int)
    u = defaultdict(int)
    for i in range(0,n,2):
        l[V[i]] += 1
        u[V[i+1]] += 1

    l = sorted(list(l.items()),key=itemgetter(1))[::-1]
    u = sorted(list(u.items()),key=itemgetter(1))[::-1]
    if len(l) == len(u) == 1:
        if l[0][0] != u[0][0]:
            res = 0
        else:
            res = min(l[0][1], u[0][1])
    else:
        if l[0][0] != u[0][0]:
            res = sum(map(itemgetter(1), l[1:])) + sum(map(itemgetter(1), u[1:]))
        elif len(u) == 1 or (len(l) > 1 and l[1][1]+u[0][1] > l[0][1]+u[1][1]):
            res = l[0][1] + sum(map(itemgetter(1), l[2:])) + sum(map(itemgetter(1), u[1:]))
        else:
            res = u[0][1] + sum(map(itemgetter(1), l[1:])) + sum(map(itemgetter(1), u[2:]))

    return str(res)

if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = input().split(" ")
"""
n = 10000
# v = [_i % 10 for _i in range(n)]
v = [_i for _i in range(n)]
n = 10
v = [1, 1, 1, 1, 1, 1, 1, 1, 1, 2]
"""
if len(set(v)) == 1:
    print(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())

    print(min(ans1, ans2))

Submission status: Accepted

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

import collections

v_odd = v_list[::2]
v_even = v_list[1::2]

count_odd = collections.Counter(v_odd).most_common(2)
count_even = collections.Counter(v_even).most_common(2)

if count_odd[0][0] != count_even[0][0]:
    ans = len(v_list) - (count_odd[0][1] + count_even[0][1])
elif len(count_odd) == 1 and len(count_even) == 1:
    ans = int(len(v_list) / 2)
elif len(count_odd) == 1 or count_odd[0][1] + count_even[1][1] > count_odd[1][1] + count_even[0][1]:
    ans = len(v_list) - (count_odd[0][1] + count_even[1][1])
else:
    ans = len(v_list) - (count_odd[1][1] + count_even[0][1])

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
v=[int(x) for x in input().split()]
if len(set(v))==1:
    print(n//2)
    import sys
    sys.exit()
from collections import Counter
v1=[]
v2=[]
for i in range(n):
    if i%2==0:
        v1.append(v[i])
    else:
        v2.append(v[i])
v1=Counter(v1)
v2=Counter(v2)
ans=10**10
# print(v1,v2)
for i1 in v1.most_common(min(len(v1),10)):
    for i2 in v2.most_common(min(len(v1),10)):
        # print(n-i1[1]-i2[1],i1,i2)
        if i1==i2:
            continue
        ans=min(n-i1[1]-i2[1],ans)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = list(map(int,input().split()))
oddA = A[0::2]
evenA = A[1::2]

oc = Counter(oddA)
omode = oc.most_common()
ec = Counter(evenA)
emode = ec.most_common()
if omode[0][0] != emode[0][0]:
    print(n-omode[0][1]-emode[0][1])
else:
    if len(omode) == 1 and len(emode) == 1:
        print(n//2)
    elif len(omode) == 1 and len(emode) != 1:
        print(n//2-emode[1][1])
    elif len(emode) == 1 and len(omode) != 1:
        print(n//2-omode[1][1])
    else:
        a = n-omode[0][1]-emode[1][1]
        b = n-omode[1][1]-omode[0][1]
        print(min(a, b))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
ve = v[0::2]
vo = v[1::2]
edict = sorted(Counter(ve).items(), key=lambda x: x[1], reverse=True)[:2]
odict = sorted(Counter(vo).items(), key=lambda x: x[1], reverse=True)[:2]
ans = 10 ** 6
if len(edict) == 1 and len(odict) == 1 and edict[0][0] == odict[0][0]:
    ans = n // 2
else:
    for e in edict:
        for o in odict:
            if e[0] == o[0]:
                continue
            ans = min(ans, len(ve) - e[1] + len(vo) - o[1])
print(ans)

Submission status: Accepted

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

l=[v[i] for i in range(n) if i%2==0]
r=[v[i] for i in range(n) if i%2]

l.sort()
r.sort()

count=1
countl1=0
countl2=0

for i in range(1,len(l)):
    if l[i]==l[i-1]: count+=1
    else:
        if count>=countl1:
            countl1,countl2=count,countl1
            charl=l[i-1]
        elif count>countl2:
            countl2=count
        count=1
        
if count>=countl1:
    countl1,countl2=count,countl1
    charl=l[-1]
elif count>countl2:
    countl2=count 


count=1
countr1=0
countr2=0

for i in range(1,len(r)):
    if r[i]==r[i-1]: count+=1
    else:
        if count>=countr1:
            countr1,countr2=count,countr1
            charr=r[i-1]
        elif count>countr2:
            countr2=count
        count=1

if count>=countr1:
    countr1,countr2=count,countr1
    charr=r[-1]
elif count>countr2:
    countr2=count 
        
if charl==charr:
    print(n-max(countl1+countr2,countl2+countr1))
else: print(n-countl1-countr1)

Submission status: Accepted

Solution code:
N = int(input())
v = list(map(int, input().split()))
from collections import Counter
ai = v[::2]
bi = v[1::2]
an = Counter(ai).most_common(2)
bn = Counter(bi).most_common(2)

if(an[0][0] == bn[0][0]):
    if(len(an)==1):
        print(int(N/2))
    else:
        print(N - an[0][1] - max(an[1][1], bn[1][1]))
else:
    print(N - an[0][1] - bn[0][1])

Submission status: Accepted

Solution code:
n = int(input())

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

v_odd = {}
v_even = {}

for i in range(n):
    if i % 2 == 0:
        v = vs[i]
        if v in v_even:
            v_even[v] += 1
        else:
            v_even[v] = 1
    else:
        v = vs[i]
        if v in v_odd:
            v_odd[v] += 1
        else:
            v_odd[v] = 1

odd_sort = sorted(v_odd.items(), key=lambda x: -x[1])
even_sort = sorted(v_even.items(), key=lambda x: -x[1])

if odd_sort[0][0] == even_sort[0][0]:
    if len(odd_sort) >= 2:
        oddnext = odd_sort[1][1]
        if len(even_sort) >= 2:
            evennext = even_sort[1][1]
            ans = min(n-oddnext-even_sort[0][1], n-odd_sort[0][1]-evennext)
        else:
            ans = min(n-oddnext-even_sort[0][1], n-odd_sort[0][1])
    else:
        if len(even_sort) >= 2:
            evennext = even_sort[1][1]
            ans = min(n-odd_sort[0][1]-evennext, n-even_sort[0][1])
        else:
            ans = n // 2


else:
    ans = n - odd_sort[0][1] - even_sort[0][1]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A1 = [0] * (10 ** 5 + 10)
A2 = [0] * (10 ** 5 + 10)

for n, a in enumerate(A):
    if n % 2 == 0:
        A1[a] += 1
    else:
        A2[a] += 1

c1 = max(A1)
c2 = max(A2)

s1 = A1.index(c1)
s2 = A2.index(c2)

if s1 != s2:
    print(N - c1 - c2)
else:
    A1.sort()
    A2.sort()
    print(N - max(A1[-1] + A2[-2], A1[-2] + A2[-1]))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v_list = tuple([int(elem) for elem in input().split(' ')])
v_list_odd = v_list[::2]
v_list_even = v_list[1::2]
ctr_odd = Counter(v_list_odd)
ctr_even = Counter(v_list_even)

# O(NlogN)
most_commons_odd = ctr_odd.most_common()
most_commons_even = ctr_even.most_common()
if len(most_commons_odd) == 1 and len(most_commons_even) == 1 \
and most_commons_odd[0][0] == most_commons_even[0][0]:
    print(len(v_list_even))
    exit(0)

odd_cand = []
even_cand = []
distinct_num = set()
i = 0
while len(distinct_num) < 2:
    try:
        odd_elem = most_commons_odd[i]
        distinct_num.add(odd_elem[0])
        odd_cand.append(odd_elem)
    except:
        pass
    try:
        even_elem = most_commons_even[i]
        distinct_num.add(even_elem[0])
        even_cand.append(even_elem)
    except:
        pass
    i += 1

min_cost = 2**32 - 1
len_allodds = len(v_list_odd)
len_allevens = len(v_list_even)
for odd_elem in odd_cand:
    for even_elem in even_cand:
        if odd_elem[0] == even_elem[0]:
            continue
        else:
            min_cost = min(min_cost, \
            (len_allodds - odd_elem[1]) + (len_allevens - even_elem[1]))
print(min_cost)

Submission status: Accepted

Solution code:
n = int(input())
list_v = list((map(int, input().split())))
list_e = list_v[::2]
list_o = list_v[1::2]
dict_e = {}
dict_o = {}
for number in set(list_e):
    dict_e[number] = 0
for i in range(0,int(n/2)):
    dict_e[list_e[i]] += 1
for number in set(list_o):
    dict_o[number] = 0
for i in range(0,int(n/2)):
    dict_o[list_o[i]] += 1
e_max = 0
o_max = 0
for key in dict_e.keys():
    if dict_e[key] > e_max:
        e_max = dict_e[key]
        e_key = key

for key in dict_o.keys():
    if dict_o[key] > o_max:
        o_max = dict_o[key]
        o_key = key
ans = o_max + e_max
if e_key == o_key:
    e_max2 = 0
    for key in dict_e.keys():
        if dict_e[key] > e_max2 and key != o_key:
            e_max2 = dict_e[key]
            e_key2 = key
    o_max2 = 0
    for key in dict_o.keys():
        if dict_o[key] > o_max2 and key != e_key:
            o_max2 = dict_o[key]
            o_key2 = key
    ans = max(e_max + o_max2, e_max2 + o_max)

print(n - ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import collections


def main():
    n = int(input())
    xs = [int(a) for a in input().split()]

    xa = [xs[i] for i in range(0, n, 2)]
    xb = [xs[i] for i in range(1, n, 2)]

    ca = collections.Counter(xa)
    cb = collections.Counter(xb)

    ma, mb = ca.most_common(2), cb.most_common(2)
    val_a, count_a = ma[0]
    val_b, count_b = mb[0]

    if val_a != val_b:
        p = len(xa) - count_a + len(xb) - count_b
        print(p)
        return
    if len(ma) == 1:
        if len(mb) == 1:
            print(len(xa))
            return
        val_b, count_b = mb[1]
        print(len(xb) - count_b)
        return
    if len(mb) == 1:
        val_b, count_b = ma[1]
        print(len(xb) - count_b)
        return
    val_a2, count_a2 = ma[1]
    val_b2, count_b2 = mb[1]
    p1 = len(xb) - count_b + len(xa) - count_a2
    p2 = len(xb) - count_b2 + len(xa) - count_a
    print(min(p1, p2))
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

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

vodd = [v[2*i] for i in range(int(n/2))]
veven = [v[2*i+1] for i in range(int(n/2))]

codd = collections.Counter(vodd)
ceven = collections.Counter(veven)

aodd = codd.most_common()[0][0]
aeven = ceven.most_common()[0][0]

nodd = codd.most_common()[0][1]
neven = ceven.most_common()[0][1]

if aodd != aeven:
    print(n-nodd-neven)
else:
    if len(codd) == 1:
        if len(ceven) == 1:
            print(n-nodd)
        else:
            meven = ceven.most_common()[1][1]
            print(n-nodd-meven)
    elif len(ceven) == 1:
        modd = codd.most_common()[1][1]
        print(n-modd-neven)
    else:
        modd = codd.most_common()[1][1]
        meven = ceven.most_common()[1][1]
        print(min(n-nodd-meven,n-modd-neven))

Submission status: Accepted

Solution code:
import collections

def inputN(typ=str):
    return map(typ, raw_input().split())


N = inputN(int)[0]
v = inputN(int)

col0 = collections.Counter(v[::2])
col1 = collections.Counter(v[1::2])
X0 = col0.most_common()
X1 = col1.most_common()
#print X0
#print X1

if X0[0][0] == X1[0][0]:
    if len(X0) == 1 and len(X1) == 1:
        k0 = X0[0][1]
        k1 = 0
    elif len(X0) == 1:
        k0 = X0[0][1]
        k1 = X1[1][1]
    elif len(X1) == 1:
        k0 = X0[1][1]
        k1 = X1[0][1]
    elif X0[1][1] >= X1[1][1]:
        k0 = X0[1][1]
        k1 = X1[0][1]
    elif X0[1][1] < X1[1][1]:
        k0 = X0[0][1]
        k1 = X1[1][1]
else:
    k0 = X0[0][1]
    k1 = X1[0][1]

print N - k0 - k1

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
v1 = [v[2 * i] for i in range(0, n // 2)]
v2 = [v[2 * i + 1] for i in range(0, n // 2)]

import collections
c1 = collections.Counter(v1)
c2 = collections.Counter(v2)

mc1 = c1.most_common()
mc2 = c2.most_common()
if mc1[0][0] != mc2[0][0]:
    print(n - (mc1[0][1] + mc2[0][1]))
else:
    if len(mc1) == 1 and len(mc2) == 1:
        print(n // 2)
    elif len(mc1) == 1 or len(mc2) == 1:
        print(n // 2 - mc2[1][1] if len(mc1) == 1 else n // 2 - mc1[1][1])
    else:
        if (mc1[0][1] + mc2[1][1]) >= (mc1[1][1] + mc2[0][1]):
            print(n - (mc1[0][1] + mc2[1][1]))
        else:
            print(n - (mc1[1][1] + mc2[0][1]))

Submission status: Accepted

Solution code:
import collections
n = int(int(input())/2)
a = list(map(int,input().split()))
o = []
e = []
for i in range(n):
  o.append(a[i*2])
  e.append(a[i*2+1])
o_c = collections.Counter(o)
e_c = collections.Counter(e)
o_most = o_c.most_common()[0][0]
o_num = o_c.most_common()[0][1]
e_most = e_c.most_common()[0][0]
e_num = e_c.most_common()[0][1]
ans = 0
if(o_most == e_most and o_num + e_num == n*2):
  ans = n
elif(o_most != e_most and o_num == n and e_num == n):
  ans = 0
else:
  if(o_most == e_most):
    if(len(o_c.most_common()) == 1):
      e_num = e_c.most_common()[1][1]
    elif(len(e_c.most_common()) == 1):
      o_num = o_c.most_common()[1][1]
    else:
      e_t = e_c.most_common()[1][1]
      o_t = o_c.most_common()[1][1]
      if(o_num - o_t <= e_num - e_t):
        o_num = o_t
      else:
        e_num = e_t
  ans = n*2 - o_num - e_num
print(ans)

Submission status: Accepted

Solution code:
import collections
def inpl(): return list(map(int, input().split()))
n = int(input())
v = inpl()
even,odd=[],[]
for i in range(len(v)):
    if i%2 == 0:
        even.append(v[i])
    else:
        odd.append(v[i])

o = collections.Counter(odd)
e = collections.Counter(even)

omax,emax,ocan,ecan = 0,0,[],[]
for k,v in o.items():
    if v > omax:
        omax = v
        ocan = [k]
    elif v == omax:
        ocan.append(k)
for k,v in e.items():
    if v > emax:
        emax = v
        ecan = [k]
    elif v == emax:
        ecan.append(k)    
end = 0        
for i in ecan:
    for j in ocan:
        if i!=j:
            #print(omax,emax)#
            ans = n-(omax+emax)
            end = 1
            break
    if end == 1:
        break
else:
    esec,osec = 0,0
    for k,v in e.items():
        if v >= emax:
            continue
        esec = max(esec,v)
    for k,v in o.items():
        if v >= omax:
            continue
        osec = max(osec,v)
    ans = min(n-(esec+omax),n-(emax+osec))
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

e = defaultdict(int)
o = defaultdict(int)

for i in range(0, len(v), 2):
    e[v[i]] += 1

for i in range(1, len(v), 2):
    o[v[i]] += 1

f = False
if len(e.keys()) == 1:
    o = sorted(o.items(), key=lambda x: x[1], reverse=True)
    for k, v in o:
        if k == list(e.keys())[0]:
            continue
        f = True
        print(n // 2 - v)
        break
    if not f:
        print(n // 2)
elif len(o.keys()) == 1:
    e = sorted(e.items(), key=lambda x: x[1], reverse=True)
    for k, v in e:
        if k == list(o.keys())[0]:
            continue
        f = True
        print(n // 2 - v)
        break
    if not f:
        print(n // 2)
else:
    e = sorted(e.items(), key=lambda x: x[1], reverse=True)
    o = sorted(o.items(), key=lambda x: x[1], reverse=True)

    if e[0][0] != o[0][0]:
        print(n // 2 - e[0][1] +  n // 2 - o[0][1])
    else:
        c1 = n // 2 - e[0][1] + n // 2 - o[1][1]
        c2 = n // 2 - e[1][1] + n // 2 - o[0][1]
        print(min(c1, c2))

Submission status: Accepted

Solution code:
from collections import Counter as c
n=int(input())
*l,=map(int,input().split())

ca=sorted(c(l[1::2]).items(),key=lambda x:x[1])[::-1]
cb=sorted(c(l[0::2]).items(),key=lambda x:x[1])[::-1]

v1=sum([i[1] for i in ca])
v2=sum([i[1] for i in cb])

a1=ca[0][0]
for i in cb:
  if a1==i[0]:continue
  b=i[1]
  break
else:
  b=0
ans1=v2-b+v1-ca[0][1]

a1=cb[0][0]
for i in ca:
  if a1==i[0]:continue
  b=i[1]
  break
else:
  b=0
ans2=v1-b+v2-cb[0][1]

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
#! usr/bin/env python
# -*- coding: utf-8 -*-

MAX_V = 10 ** 5


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

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

    nc, ne = [i for i in range(MAX_V+1)], [i for i in range(MAX_V+1)]
    nc.sort(key=lambda no: odd[no], reverse=True)
    ne.sort(key=lambda no: even[no], reverse=True)

    if nc[0] == n // 2 and ne[0] == n // 2:
        # 1種類
        print(n//2)
    else:
        if nc[0] == ne[0]:
            print(min(n-odd[nc[0]]-even[ne[1]], n-even[ne[0]]-odd[nc[1]]))
        else:
            print(n-odd[nc[0]]-even[ne[0]])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

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

vo = [v[i] for i in range(n) if i % 2 == 0]
ve = [v[i] for i in range(n) if i % 2 == 1]

count_vo = collections.Counter(vo).most_common()
count_ve = collections.Counter(ve).most_common()

res = 200001

if count_vo[0][0] != count_ve[0][0]:
    print(n-count_vo[0][1]-count_ve[0][1])
elif len(count_vo) == 1 and len(count_ve) == 1:
    print(n//2)
elif len(count_vo) == 1:
    print(n//2-count_ve[1][1])
elif len(count_ve) == 1:
    print(n//2-count_vo[1][1])
else:
    if count_vo[1][1] >= count_ve[1][1]:
        print(n-count_vo[1][1]-count_ve[0][1])
    else:
        print(n - count_vo[0][1] - count_ve[1][1])

Submission status: Accepted

Solution code:
from collections import Counter

ans = int(input())
V = [int(i) for i in input().split()]

v1 = []
v2 = []

for i, v in enumerate(V):
    if i % 2 == 0:
       v1.append(v)
    else:
        v2.append(v)

v1c = Counter(v1).most_common()
v2c = Counter(v2).most_common()

k1, v1 = v1c[0][0], v1c[0][1]
k2, v2 = v2c[0][0], v2c[0][1]

if k1 == k2:
    ans -= v1
    m = 0
    if len(v1c) >= 2:
        m = max(m, v1c[1][1])
    if len(v2c) >= 2:
        m = max(m, v2c[1][1])
    ans -= m
else:
    ans -= v1 + v2

print(ans)

Submission status: Accepted

Solution code:
import collections as c
n = int(input())
v = list(map(int, input().split()))
v_odd  = []
v_even = []
for i in range(n):
    if i % 2 == 0: v_even.append(v[i])
    else: v_odd.append(v[i])
od_counter = c.Counter(v_odd)
ev_counter = c.Counter(v_even)
odmode = od_counter.most_common()
evmode = ev_counter.most_common()

if odmode[0][0] != evmode[0][0]:
    total = len(v) - odmode[0][1] - evmode[0][1]
    print(total)
else:
    if len(odmode) == 1:
        if len(evmode) == 1:
            print(min(len(v_odd), len(v_even)))
        else:
            print(len(v_even) - evmode[1][1])
    elif len(evmode) == 1:
        print(len(v_odd) - odmode[1][1])
    else:
        odc = len(v) - odmode[0][1] - evmode[1][1]
        evc = len(v) - evmode[0][1] - odmode[1][1]
        print(min(odc, evc))