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:
import collections

def main():
    n = input()
    a = map(int, raw_input().split())
    odd = [a[2 * i] for i in range(n/2)]
    even = [a[2*i + 1] for i in range(n/2)]
    codd = collections.Counter(odd)
    ceven = collections.Counter(even)
    coddmostitem = codd.most_common()[0][0]
    coddmosttimes = codd.most_common()[0][1]
    cevenmostitem = ceven.most_common()[0][0]
    cevenmosttimes = ceven.most_common()[0][1]
    if coddmostitem == cevenmostitem:
        if coddmosttimes > cevenmosttimes:
            if len(ceven) == 1:
                cevennoreplace = 0
            else:
                cevennoreplace = ceven.most_common()[1][1]
            coddnoreplace = codd.most_common()[0][1]
        if coddmosttimes < cevenmosttimes:
            if len(codd) == 1:
                coddnoreplace = 0
            else:
                coddnoreplace = codd.most_common()[1][1]
            cevennoreplace = ceven.most_common()[0][1]
        else:
            if len(ceven) == 1:
                cevennoreplace = 0
                coddnoreplace = coddmosttimes
            elif len(codd) == 1:
                cevennoreplace = cevenmosttimes
                coddnoreplace = 0
            elif codd.most_common()[1][1] >= ceven.most_common()[1][1]:
                cevennoreplace = ceven.most_common()[0][1]
                coddnoreplace = codd.most_common()[1][1]
            elif codd.most_common()[1][1] < ceven.most_common()[1][1]:
                cevennoreplace = ceven.most_common()[1][1]
                coddnoreplace = codd.most_common()[0][1]
    else:
        coddnoreplace = coddmosttimes
        cevennoreplace = cevenmosttimes
    coddreplace = n/2 - coddnoreplace
    cevenreplace = n/2 - cevennoreplace
    print(coddreplace+cevenreplace)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

d = [{-1: 0},{-1: 0}]
for i in range(n):
    d[i%2][v[i]] = d[i%2].get(v[i],0)+1

odd_max = max(d[0], key=d[0].get)
even_max = max(d[1],key=d[1].get)

if odd_max != even_max:
    ans = n - d[0][odd_max] - d[1][even_max]
else:
    odd_max_num = d[0].pop(odd_max)
    odd_second_num = max(d[0].values())
    even_max_num = d[1].pop(even_max)
    even_second_num = max(d[1].values())
    ans = n - max(odd_max_num+even_second_num,odd_second_num+even_max_num)

print(ans)

Submission status: Accepted

Solution code:
import itertools

n = int(input())
v = [int(w) for w in input().split()]
assert len(v) == n

def elem_count(ls):
    '''Return the list of (num of elem, elem).'''
    ls.sort()
    ls = itertools.groupby(ls)
    cs = [(len(list(s)), e) for e, s in ls]
    cs.sort(reverse=True)
    return cs

e = [v[i] for i in range(0, n, 2)]
o = [v[i] for i in range(1, n, 2)]

e = elem_count(e)
o = elem_count(o)

if e[0][1] != o[0][1]:
    ans = n - (e[0][0] + o[0][0])

elif len(e) == len(o) == 1:
    ans = n - e[0][0]

elif len(e) == 1:
    ans = n - (e[0][0] + o[1][0])

elif len(o) == 1:
    ans = n - (e[1][0] + o[0][0])

elif e[0][0] + o[1][0] > e[1][0] + o[0][0]:
    ans = n - (e[0][0] + o[1][0])
else:
    ans = n - (e[1][0] + o[0][0])

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

def count(V, V_dic):
    for v in V:
        if v in V_dic.keys():
            V_dic[v] += 1
        else:
            V_dic[v] = 1
    return

N = int(input())
V = [int(i) for i in input().split(' ')]
V1 = []
V2 = []
for i in range(N//2):
    V1.append(V[i*2])
    V2.append(V[i*2+1])
V1_dic = {1000000:0}
V2_dic = {1000000:0}
count(V1, V1_dic)
count(V2, V2_dic)
sort_V1 = sorted(V1_dic.items(), key=lambda V: V[1], reverse=True)
sort_V2 = sorted(V2_dic.items(), key=lambda V: V[1], reverse=True)

if sort_V1[0][0] == sort_V2[0][0]:
    im1 = sort_V1[0][1] + sort_V2[1][1]
    im2 = sort_V1[1][1] + sort_V2[0][1]
    if im1 >= im2:
        print(N - im1)
    else:
        print(N - im2)
else:
    print(N - (sort_V1[0][1] + sort_V2[0][1]))

Submission status: Accepted

Solution code:
import collections

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

v_odd = []
for i in range(0, n, 2):
    v_odd.append(v[i])
v_even = []
for i in range(1, n, 2):
    v_even.append(v[i])

odd_dic = collections.Counter(v_odd)
odd_sort = sorted(odd_dic.items(), key=lambda x: -x[1])
odd_maxkey = odd_sort[0][0]
odd_cnt1 = odd_sort[0][1]
if len(odd_sort) > 1:
    odd_cnt2 = odd_sort[1][1]
else:
    odd_cnt2 = 0

even_dic = collections.Counter(v_even)
even_sort = sorted(even_dic.items(), key=lambda x: -x[1])
even_maxkey = even_sort[0][0]
even_cnt1 = even_sort[0][1]
if len(even_sort) > 1:
    even_cnt2 = even_sort[1][1]
else:
    even_cnt2 = 0

ans = len(v_odd) + len(v_even)
if odd_maxkey == even_maxkey:
    ans -= max(odd_cnt1 + even_cnt2, odd_cnt2 + even_cnt1)
else:
    ans -= odd_cnt1 + even_cnt1


#print(odd_cnt1)
#print(odd_cnt2)
#print(even_cnt1)
#print(even_cnt2)

print(ans)

Submission status: Accepted

Solution code:
iN = int(input())
iM = iN // 2
aV = [int(x) for x in input().split()]

aV0={}
aV1={}
for i in range(iN):
    iA = aV[i]
    if i % 2 == 0:
        if iA in aV0:
            aV0[iA] += 1
        else:
            aV0[iA] = 1
    else:
        if iA in aV1:
            aV1[iA] += 1
        else:
            aV1[iA] = 1
aV0[0]=0
aV1[0]=0

aV0dec = sorted(aV0,key=aV0.get,reverse=True)
aV1dec = sorted(aV1,key=aV1.get,reverse=True)

iMax1aV0 = aV0dec[0]
iMax1aV1 = aV1dec[0]

if iMax1aV0 != iMax1aV1:
    print(iN - aV0[iMax1aV0] - aV1[iMax1aV1])
else:
    iMax1aV0sum =aV0[iMax1aV0]
    iMax1aV1sum =aV0[iMax1aV1]
    #ilenaV0 = len(aV0dec)
    #ilenaV1 = len(aV0dec)
    #if iMax1aV0sum == iMax1aV1sum :
    #       print(iN - iMax1aV0sum - max(aV0[aV0dec[1]],aV1[aV1dec[1]]))
    #else:
    print(iN - max(iMax1aV0sum,iMax1aV1sum) - max(aV0[aV0dec[1]],aV1[aV1dec[1]]))

Submission status: Accepted

Solution code:
import collections

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

a = []
b = []
a_f = 0
b_f = 0
a_s = 0
b_s = 0
for i in range(N):
    if i%2 == 0:
        a.append(l[i])
    else:
        b.append(l[i])
        
c_a = collections.Counter(a)
c_b = collections.Counter(b)

a_f = c_a.most_common()[0]
b_f = c_b.most_common()[0]

if a_f[0] != b_f[0]:
    print(N - a_f[1] - b_f[1])

elif len(set(a)) == 1 and len(set(b)) == 1:
    print(int(N/2))
elif len(set(a)) == 1:
    print(int(N/2 - c_b.most_common()[1][1]))
elif len(set(b)) == 1:
    print(int(N/2 - c_a.most_common()[1][1]))
elif a_f[1] == b_f[1]:
    second_a = c_a.most_common()[1][1]
    second_b = c_b.most_common()[1][1]
    if second_a >= second_b:
        print(int(N - b_f[1] - second_a))
    else:
        print(int(N - a_f[1] - second_b))
elif a_f[1] > b_f[1]:
    print(int(N - a_f[1] - c_b.most_common()[1][1]))
else:
    print(int(N - b_f[1] - c_a.most_common()[1][1]))

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
v = tuple(int(i) for i in input().split())

def f(v):
    d = defaultdict(int)
    d[0] = 0

    for i in v:
        d[i] += 1
    return sorted(d.items(), key=lambda x: x[1], reverse=True)

t1 = f(v[::2])
t2 = f(v[1::2])

if t1[0][0] == t2[0][0]:
    if t1[0][1] + t2[1][1] > t1[1][1] + t2[0][1]:
        print(n - t1[0][1] - t2[1][1])
    else:
        print(n - t1[1][1] - t2[0][1])
else:
    print(n - t1[0][1] - t2[0][1])

Submission status: Accepted

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

M0=[]
M1=[]

for i in range(n):
	if i%2==0: M0.append(A[i])
	else: M1.append(A[i])

C0=Counter(M0)
C1=Counter(M1)

L0=sorted(C0.items(), key=lambda x:x[1], reverse=True)
L1=sorted(C1.items(), key=lambda x:x[1], reverse=True)

a0=None
a1=None

if L0[0][0]!=L1[0][0]:
	a0=L0[0][0]
	a1=L1[0][0]
	b0=L0[0][0]
	b1=L1[0][0]
elif L0[0][0]==L1[0][0]:
	a0=L0[0][0]
	if len(L1)==1:
		a1=-1
	else:
		a1=L1[1][0]

	b1=L1[0][0]
	if len(L0)==1:
		b0=-1
	else:
		b0=L0[1][0]

ans0=0
ans1=0
for x in M0:
	if x!=a0:
		ans0+=1

for x in M1:
	if x!=a1:
		ans0+=1

ans1=0
for x in M0:
	if x!=b0:
		ans1+=1

for x in M1:
	if x!=b1:
		ans1+=1

print min(ans0, ans1)

Submission status: Accepted

Solution code:
# coding: utf-8
import collections

n = int(input())
v = [int(s) for s in input().strip().split(" ")]
el = [0] * (n // 2)
ol = [0] * (n // 2)
ans = 0

for i in range(n):
    if i % 2 == 0:
        el[i//2] = v[i]
    else:
        ol[(i-1)//2] = v[i]
        
ec = collections.Counter(el)
oc = collections.Counter(ol)
e_num = ec.most_common()[0][0]
e_count = ec.most_common()[0][1]
o_num = oc.most_common()[0][0]
o_count = oc.most_common()[0][1]

if len(set(v)) == 1:
    ans = n //2
elif e_num != o_num:
    ans = n - e_count - o_count
elif e_count > o_count:
    #o_num = oc.most_common()[1][0]
    o_count = oc.most_common()[1][1]
    ans = n - e_count - o_count
elif e_count < o_count:
    #e_num = ec.most_common()[1][0]
    e_count = ec.most_common()[1][1]
    ans = n - e_count - o_count
elif e_count == o_count:
    if ec.most_common()[1][1] >= oc.most_common()[1][1]:
        e_count = ec.most_common()[1][1]
    else:
        o_count = oc.most_common()[1][1]
    ans = n - e_count - o_count

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

input()
numbers = tuple(map(int, input().split()))

evenNumberToCount = dict()

oddNumberToCount = dict()

for i, n in enumerate(numbers):
    if i % 2 == 0:
        evenNumberToCount[n] = evenNumberToCount.get(n, 0) + 1
    else:
        oddNumberToCount[n] = oddNumberToCount.get(n, 0) + 1

evenCountToNumber = list()
for n, i in evenNumberToCount.items():
    evenCountToNumber.append((i, n))
evenCountToNumber.sort(reverse=True)

oddCountToNumber = list()
for n, i in oddNumberToCount.items():
    oddCountToNumber.append((i, n))
oddCountToNumber.sort(reverse=True)

if oddCountToNumber[0][1] == evenCountToNumber[0][1]:
    if len(numbers) // 2 == oddCountToNumber[0][0] == evenCountToNumber[0][0]:
        print(len(numbers) // 2)
    elif oddCountToNumber[0][0] < evenCountToNumber[0][0]:
        print(len(numbers) - oddCountToNumber[1][0] - evenCountToNumber[0][0])
    elif oddCountToNumber[0][0] > evenCountToNumber[0][0]:
        print(len(numbers) - oddCountToNumber[0][0] - evenCountToNumber[1][0])
    else:
        first = len(numbers) - oddCountToNumber[0][0] - evenCountToNumber[1][0]
        second = len(numbers) - oddCountToNumber[1][0] - evenCountToNumber[0][0]
        print(min(first, second))
else:
    print(len(numbers) - oddCountToNumber[0][0] - evenCountToNumber[0][0])

Submission status: Accepted

Solution code:
import sys
Input = sys.stdin.readline

n = int(Input())
v = list(map(int, Input().split()))
mi = float("inf")
count1 = 0
count2 = 0

v1 = v[0::2]
v1_count = {}
for i in v1:
    if i not in v1_count:
        v1_count[i] = 1
    else:
        v1_count[i] += 1

v1_count = sorted(v1_count.items(), key=lambda i: i[1], reverse=True)
count1 += len(v1) - v1_count[0][1]

v2 = v[1::2]
v2_count = {}
for i in v2:
    if i not in v2_count:
        v2_count[i] = 1
    else:
        v2_count[i] += 1

v2_count = sorted(v2_count.items(), key=lambda i: i[1], reverse=True)
count2 += len(v2) - v2_count[0][1]

if v1_count[0][0] != v2_count[0][0]:
    mi = count1 + count2
else:
    lv2 = len(v2)
    for k, i in v2_count[1:]:
        result = count1 + lv2 - i
        if mi > result:
            mi = result
    else:
        if mi > count1 + lv2:
            mi = count1 + lv2

    lv1 = len(v1)
    for k, i in v1_count[1:]:
        result = count2 + lv1 - i
        if mi > result:
            mi = result
    else:
        if mi > count2 + lv1:
            mi = count2 + lv1

print(mi)

Submission status: Accepted

Solution code:
import collections

N = int(input())
v = list(int(i) for i in input().split())


gu = v[0::2]
ki = v[1::2]


gc = collections.Counter(gu)
kc = collections.Counter(ki)

guc = gc.most_common()
kic = kc.most_common()

gv = gc.values()
kv = kc.values()

if guc[0][0] != kic[0][0]:
    gcount = sum(gv) - max(gv)
    kcount = sum(kv) - max(kv)
    print(gcount+kcount)
else:
    if len(guc) == 1 and len(kic) == 1 :
        print(str(guc[0][1]))
    else:
        if len(guc) == 1 and len(kic) != 1 :
            print(str(sum(gv)+sum(kv)-max(gv)-kic[1][1]))
        elif len(guc) != 1 and len(kic) == 1 :
            print(str(sum(gv)+sum(kv)-max(kv)-guc[1][1]))
        else:
            print(str(sum(gv)+sum(kv)-max((guc[1][1] + kic[0][1]), (guc[0][1] + kic[1][1]))))

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
ev = [0]*(max(v)+1)
od = [0]*(max(v)+1)
for i in range(n):
    if i % 2 == 0:
        ev[v[i]-1] += 1
    else:
        od[v[i] - 1] += 1
e = sorted(ev)
o = sorted(od)
if ev.index(e[-1]) == od.index(o[-1]) and e[-2] != e[-1] and o[-2] != o[-1]:
    print(n-max(e[-1]+o[-2], e[-2]+o[-1]))
else:
    print(n-e[-1]-o[-1])

Submission status: Accepted

Solution code:
import math
n = int(input())
lis = list(map(int,input().split()))
lisa = lis[0::2]
lisb = lis[1::2]
  # print(lisa,lisb)
cou = [0 for i in range(10 ** 5+1)]
co = [0 for i in range(10 ** 5+1)]
for i in range(len(lisa)):
  cou[lisa[i]] += 1
for i in range(len(lisb)):
  co[lisb[i]] += 1
if cou.index(max(cou)) != co.index(max(co)):
  print(n -(max(cou) + max(co)))
else:
  num = max(cou)
  cou[cou.index(max(cou))] = 0
  co[co.index(max(co))] = 0
  ans = cou + co
  print(n -(num + max(ans)))

Submission status: Accepted

Solution code:
from collections import Counter

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

count_odd = Counter(v[1::2]).most_common()
count_even = Counter(v[0::2]).most_common()


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

print(int(ans))

Submission status: Accepted

Solution code:
import collections


n = input()
s = input().split(' ')

even = collections.Counter(s[1::2])
odd = collections.Counter(s[0::2])
maxeven = even.most_common()[0]
maxodd = odd.most_common()[0]
ans = 0
if maxeven[0] != maxodd[0]:
    ans = len(s) - maxeven[1] - maxodd[1]
else:
    if len(s) == maxeven[1] + maxodd[1]:
        ans = len(s) - maxeven[1]
    else:
        if maxeven[1] == maxodd[1]:
            nextodd = odd.most_common()[1]
            nexteven = even.most_common()[1]
            ans = len(s) - maxeven[1] -max(nextodd[1], nexteven[1]) 

        elif maxeven[1] > maxodd[1]:
            nextodd = odd.most_common()[1]
            ans = len(s) - maxeven[1] - nextodd[1]
        else:
            nexteven = even.most_common()[1]
            ans = len(s) - maxodd[1] - nexteven[1]

print(ans)

Submission status: Accepted

Solution code:
#n = list(map(int, input().split()))
import numpy as np
n = int(input())
v = list(map(int, input().split()))
data1 = [0]*(10**5 + 1)
data2 = [0]*(10**5 + 1)
for i in range(len(v) // 2):
    data1[v[2 * i]] += 1
    data2[v[2 * i + 1]] += 1
result = None
if np.argmax(np.array(data1)) == np.argmax(np.array(data2)):
    data1.sort()
    data2.sort()
    data1.reverse()
    data2.reverse()
    result = min(len(v) - data1[0] - data2[1], len(v) - data1[1] - data2[0])
else:
    data1.sort()
    data2.sort()
    data1.reverse()
    data2.reverse()
    result = len(v) - data1[0] - data2[0]
print(result)

Submission status: Accepted

Solution code:
from collections import Counter

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

# n = 8
# v = [33,4,33,4,33,4,33,2]


ki = v[::2]
gu = v[1::2]

ki = Counter(ki)
gu = Counter(gu)

ki = ki.most_common()
gu = gu.most_common()

ans = 0
if ki[0][0] == gu[0][0]:
    if len(ki) == 1 and len(gu) == 1:
        ans = int(n/2)

    elif len(ki) == 1:
        ans = n - ki[0][1] - gu[1][1]

    elif len(gu) == 1:
        ans = n - ki[1][1] - gu[0][1]

    else:
        if ki[1][1] > gu[1][1]:
            ans = n - ki[1][1] - gu[0][1]
        else:
            ans = n - ki[0][1] - gu[1][1]
else:
    ans = n - ki[0][1] - gu[0][1]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))
d0 = {0:0}
d1 = {0:0}
for i,a in enumerate(arr):
  if i%2==0:
    if a not in d0:
      d0[a] = 1
    else:
      d0[a] += 1
  else:
    if a not in d1:
      d1[a] = 1
    else:
      d1[a] += 1
pairs0 = sorted(d0.items(), key=lambda x:x[1])[::-1]
pairs1 = sorted(d1.items(), key=lambda x:x[1])[::-1]
a0, maxlen0 = pairs0[0]
a1, maxlen1 = pairs1[0]

if a0!=a1:
  print(n - maxlen0 - maxlen1)
else:
  if maxlen0<maxlen1:
    print(n - maxlen1 - pairs0[1][1])
  elif maxlen0>maxlen0:
    print(n - maxlen0 - pairs1[1][1])
  else:
    if len(pairs0)>len(pairs1):
      pairs0, pairs1 = pairs1, pairs0
    flag = False
    for i,p in enumerate(pairs0):
      if i==0:
        continue
      x0, x1 = p[1], pairs1[i][1]
      if x0!=x1:
        flag = True
        break
    if flag:
      print(n - maxlen0 - max(x0, x1))
    else:
      print(n - maxlen0 - pairs1[1][1])

Submission status: Accepted

Solution code:
import collections
n=int(input())
v=list(map(int,input().split()))
v1=[]
v2=[]
c=0
for i in range(len(v)):
    if c==0:
        v1.append(v[i])
        c=1
    else:
        v2.append(v[i])
        c=0
m1=collections.Counter(v1)
w1=m1.most_common()
m2=collections.Counter(v2)
w2=m2.most_common()
if w1[0][0]==w2[0][0]:
    if w1[0][1]>w2[0][1]:
        print(n-w1[0][1]-w2[1][1])
    elif w1[0][1]<w2[0][1]:
        print(n-w1[1][1]-w2[0][1])
    elif len(w1)>1:
        if w1[1][1]>=w2[1][1]:
            print(n-w1[1][1]-w2[0][1])
        else:
            print(n-w1[0][1]-w2[1][1])
    else:
        print(n//2)
else:
    print(n-w1[0][1]-w2[0][1])

Submission status: Accepted

Solution code:
def main():
    """
    数列 a1,a2,...,anが以下の条件を満たすとき、 /\/\/\/ と呼ぶ

        各 i=1,2...,n-2 について、ai=ai+2
        数列に現れる数はちょうど 2種類偶数長の数列 

    v1,v2,...,vnが与えられます
    要素をいくつか書き換えることでこの数列を /\/\/\/ にしたいです。
    書き換える要素の数は最小でいくつになるか求めてください。

    2 <= n <= 10^5, nは偶数
    1 <= vi <= 10^5, viは整数
    """
    n = int(input())
    *v, = map(int, input().split())

    ans = f(n, v)
    print(ans)


def f(n, v):
    """
    3 1 3 2:
        3種類
        a1=a3, a2 != a4
    """
    ans = float("inf")
    v_idx_even = []
    v_idx_odd = []
    for i, x in enumerate(v):
        if i % 2 == 0:
            v_idx_even.append(x)
        else:
            v_idx_odd.append(x)

    from collections import Counter
    ve = Counter(v_idx_even)
    vo = Counter(v_idx_odd)

    if len(ve) == 1 and len(vo) == 1:
        # 両方1種類
        ans = 0
        if ve.most_common()[0][0] == vo.most_common()[0][0]:
            ans = n // 2

    elif len(ve) == 1 or len(vo) == 1:
        # 片方1種類
        idx = 0
        if ve.most_common()[0][0] == vo.most_common()[0][0]:
            idx = 1
        if len(ve) == 1:
            ans = n // 2 - vo.most_common()[idx][1]
        else:
            ans = n // 2 - ve.most_common()[idx][1]

    else:
        # 両方2種類以上
        if ve.most_common()[0][0] != vo.most_common()[0][0]:
            ans = n - ve.most_common()[0][1] - vo.most_common()[0][1]
        else:
            ans = n - \
                max(ve.most_common()[0][1] + vo.most_common()[1][1],
                    ve.most_common()[1][1] + vo.most_common()[0][1])

    return ans


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
a=[];b=[]
for i in range(len(v)):
  if i%2==0:a.append(v[i])
  else:b.append(v[i])
A=Counter(a);B=Counter(b)
c=A.most_common()[0][0];d=B.most_common()[0][0]
if len(set(a))!=1 and len(set(b))!=1:
  if c==d:
    e=A.most_common()[1][1];f=B.most_common()[1][1]
    if e<=f:
      d=B.most_common()[1][0]
    else:
      c=A.most_common()[1][0]
  x=len(a)-a.count(c)
  y=len(b)-b.count(d)
  print(x+y)
elif len(set(a))==1 and len(set(b))==1:
  if a[0]!=b[0]:print(0)
  else:print(len(a))
elif len(set(a))==1:
  if c==d:
    d=B.most_common()[1][0]
  x=len(a)-a.count(c)
  y=len(b)-b.count(d)
  print(x+y)
else:
  if c==d:
    c=A.most_common()[1][0]
  x=len(a)-a.count(c)
  y=len(b)-b.count(d)
  print(x+y)

Submission status: Accepted

Solution code:
from collections import Counter
n=input()
lists=input().split()
hi=[]
low=[]
c=True
for i in range(len(lists)):
    if(i==0):
        same_c=lists[i]
    if(same_c==lists[i] and c):
        pass
    else:
        c=False

    if(i%2==0):
        hi.append(lists[i])
    else:
        low.append(lists[i])
if(c):
    print(len(hi))
    exit(0)
hi_counter=Counter(hi)
low_counter=Counter(low)
while(True):
    if(hi_counter.most_common()[0][0]!=low_counter.most_common()[0][0]):
        print(len(hi)-hi_counter.most_common()[0][1]+len(low)-low_counter.most_common()[0][1])
        exit(0)
    else:
        if(hi_counter.most_common()[0][1]-hi_counter.most_common()[1][1]>low_counter.most_common()[0][1]-low_counter.most_common()[1][1]):
            print(len(hi)-hi_counter.most_common()[0][1]+len(low)-low_counter.most_common()[1][1])
            exit(0)
        else:
            print(len(hi)-hi_counter.most_common()[1][1]+len(low)-low_counter.most_common()[0][1])
            exit(0)

Submission status: Accepted

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

# v = [1, 1] * 5
# v += [2, 2] * 4
v = np.array(v)

if len(np.unique(v)) == 1:
    print(int(len(v)/2))
else:
    vodd = v[np.arange(0,len(v),2)]
    odd_count = Counter(vodd)

    veven = v[np.arange(1,len(v),2)]
    even_count = Counter(veven)
    

    d_odd = []
    odd_lis = list(odd_count.keys())
    for key in odd_count.keys():
        d_odd.append(odd_count[key])

    d_even = []
    even_lis = list(even_count.keys())
    for key in even_count.keys():
        d_even.append(even_count[key])

    
    if even_lis[np.argmax(d_even)] != odd_lis[np.argmax(d_odd)]:
        score = np.max(d_even) + np.max(d_odd)
    else:
#         print('a')
        score1 = d_even[np.argsort(d_even)[-1]] + d_odd[np.argsort(d_odd)[-2]]
        score2 = d_even[np.argsort(d_even)[-2]] + d_odd[np.argsort(d_odd)[-1]]
        score = max(score1,score2)
    print(len(v) - score)

Submission status: Accepted

Solution code:
n = int(input())
lst = [int(x) for x in input().split()]
d1 = {None : -1}
d2 = {None : -1}
for i in range(n // 2):
    if lst[2 * i] in d1:
        d1[lst[2 * i]] += 1
    else:
        d1[lst[2 * i]] = 0 # 登場回数
        
    if lst[2 * i + 1] in d2:
        d2[lst[2 * i + 1]] += 1
    else:
        d2[lst[2 * i + 1]] = 0
        

u1 = sorted(d1.items(), key = lambda x : x[1], reverse = True)
u2 = sorted(d2.items(), key = lambda x : x[1], reverse = True)

MAKE = lambda x : x[1] + 1

if u1[0][0] != u2[0][0]:
    a = MAKE(u1[0]) + MAKE(u2[0])
    print(n - a)
else:
    a1 = MAKE(u1[0]) + MAKE(u2[1])
    a2 = MAKE(u1[1]) + MAKE(u2[0])
    a = max(a1, a2)
    print(n - a)

Submission status: Accepted

Solution code:
n = int(input())
v = [int(x) for x in input().split()]
odd = {}
even = {}
for i in range(n):
	x = v[i]
	if i&1==1:
		if x in odd:
			odd[x]+= 1
		else:
			odd[x] = 1
	else:
		if x in even:
			even[x]+= 1
		else:
			even[x] = 1
			
oddl = []
for p, q in odd.items():
	oddl.append((p, q))
evenl = []
for p, q in even.items():
	evenl.append((p, q))
	
oddl = sorted(oddl, key=lambda x: x[1], reverse=True)
evenl = sorted(evenl, key=lambda x: x[1], reverse=True)

oddn = (n+1)>>1
evenn = n-oddn
result = []
if oddl[0][0] == evenl[0][0]:
	if len(oddl) > 1:
		result.append((oddn-oddl[1][1])+(evenn-evenl[0][1]))
	if len(evenl) > 1:
		result.append((oddn-oddl[0][1])+(evenn-evenl[1][1]))
	if len(oddl) == len(evenl) == 1:
		result.append(evenn)
else:
	result.append((oddn-oddl[0][1])+(evenn-evenl[0][1]))
print(min(result))

Submission status: Accepted

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

v1 = {}; v2 = {}

for i in range(0, n, 2):
    v = V[i]
    if v in v1.keys():
        v1[v] += 1
    else:
        v1[v] = 1

for i in range(1, n, 2):
    v = V[i]
    if v in v2.keys():
        v2[v] += 1
    else:
        v2[v] = 1

max_v1 = max(v1.items(), key=lambda x: x[1])
max_v2 = max(v2.items(), key=lambda x: x[1])

ans = 0
if max_v1[0] != max_v2[0]:
    ans = n - max_v1[1] - max_v2[1]
elif len(v1) == len(v2) == 1:
    ans = int(n/2)
elif len(v1) == 1:
    max_v2_ = max(v2.items(), key=lambda x: x[1])
    ans = n - max_v1[1] - max_v2_[1]
elif len(v2) == 1:
    max_v1_ = max(v1.items(), key=lambda x: x[1])
    ans = n - max_v1_[1] - max_v2[1]
else:
    v1.pop(max_v1[0])
    v2.pop(max_v2[0])
    max_v1_ = max(v1.items(), key=lambda x: x[1])
    max_v2_ = max(v2.items(), key=lambda x: x[1])
    
    if max_v1_[1] >= max_v2_[1]:
        ans = n - max_v1_[1] - max_v2[1]
    else:
        ans = n - max_v1[1] - max_v2_[1]

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N = int(input())
vv = inpl()

cnt1 = defaultdict(int)
cnt2 = defaultdict(int)
ed1 = []
ed2 = []

for i in range(N):
    v = vv[i]
    if i%2 == 0:
        if cnt1[v] == 0:
            ed1.append(v)
        cnt1[v] += 1
    else:
        if cnt2[v] == 0:
            ed2.append(v)
        cnt2[v] += 1

n1 = n2 = 0
cnts1 = []
cnts2 = []
for e in ed1:
    cnts1.append([cnt1[e],e])
for e in ed2:
    cnts2.append([cnt2[e],e])

cnts1.sort(reverse=True)
cnts2.sort(reverse=True)
cnts1.append([0,0])
cnts2.append([0,0])

if cnts1[0][1] != cnts2[0][1]:
    print(N-cnts1[0][0]-cnts2[0][0])
else:
    ans1 = cnts1[1][0] + cnts2[0][0]
    ans2 = cnts1[0][0] + cnts2[1][0]
    print(N-max(ans1,ans2))

Submission status: Accepted

Solution code:
a = input()
dict1 = dict()
dict2 = dict()
counter = 0
b = input().split()
for i in b:
	if counter % 2 == 0:
		if i not in dict1:
			dict1[i] = 1
		else:
			dict1[i] += 1
	elif counter % 2 == 1:
		if i not in dict2:
			dict2[i] = 1
		else:
			dict2[i] += 1
			
	counter += 1

dict1list = []
for i in dict1.keys():
	dict1list.append([i, dict1[i]])

dict2list = []
for i in dict2.keys():
	dict2list.append([i, dict2[i]])

dict1list.sort(key = lambda x: x[1])
dict2list.sort(key = lambda x: x[1])
if dict1list[-1][0] == dict2list[-1][0] and len(dict2list) > 1 and dict1list[-1][1]  > dict2list[-1][1]:
	print(len(b) - dict1list[-1][1] - dict2list[-2][1])
elif dict1list[-1][0] == dict2list[-1][0] and len(dict2list) == 1 and dict1list[-1][1]  > dict2list[-1][1]:
	print(len(b) - dict1list[-1][1])
elif dict1list[-1][0] == dict2list[-1][0] and len(dict1list) > 1 and dict1list[-1][1]  < dict2list[-1][1]:
	print(len(b) - dict1list[-2][1] - dict2list[-1][1])
elif dict1list[-1][0] == dict2list[-1][0] and len(dict1list) == 1 and dict1list[-1][1]  < dict2list[-1][1]:
	print(len(b) - dict2list[-1][1])
elif dict1list[-1][0] == dict2list[-1][0] and dict1list[-1][1]  == dict2list[-1][1]:
	if len(dict1list) == 1 and len(dict2list) == 1:
		print(len(b) - dict2list[-1][1])
	elif len(dict1list) == 1 and len(dict2list) > 1:
		print(len(b) - dict2list[-2][1] - dict1list[-1][1])
	elif len(dict1list) > 1 and len(dict2list) == 1:
		print(len(b) - dict2list[-1][1] - dict1list[-2][1])
	else:
		if (dict2list[-2][-1] >= dict1list[-2][-1]):
			print(len(b) - dict2list[-2][1] - dict1list[-1][1])
		elif (dict2list[-2][-1] < dict1list[-2][-1]):
			print(len(b) - dict2list[-1][1] - dict1list[-2][1])
else:
	print(len(b) - dict1list[-1][1] - dict2list[-1][1])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
a1 = A[::2]
a2 = A[1::2]

count1 = dict()
count2 = dict()

for a in a1:
    if a in count1.keys():
        count1[a] += 1
    else:
        count1[a] = 1

for a in a2:
    if a in count2.keys():
        count2[a] += 1
    else:
        count2[a] = 1

kf = lambda x: -x[1]

A1 = sorted(count1.items(), key=kf)
A2 = sorted(count2.items(), key=kf)

if A1[0][0] == A2[0][0]:
    if len(A1) == 1:
        if len(A2) == 1:
            keep = A1[0][1]
        else:
            keep = A1[0][1] + A2[1][1]
    else:
        keep = max([A1[1][1], A2[1][1]]) + A1[0][1]
else:
    keep = A1[0][1] + A2[0][1]

print(N - keep)

Submission status: Accepted

Solution code:
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])

sv1 = set(v1)
sv2 = set(v2)
if len(sv1) == 1 and len(sv2) == 1 :
    if sv1 != sv2:
        print(0)
    else:
        print(n//2)
else:
    dic1 = { i : 0 for i in sv1}
    dic2 = { i : 0 for i in sv2}
    for i in v1:
        dic1[i] = dic1[i] + 1
    for i in v2:
        dic2[i] = dic2[i] + 1
    #print(dic1, dic2)
    dic1 = sorted(dic1.items(), key=lambda x: -x[1])
    dic2 = sorted(dic2.items(), key=lambda x: -x[1])
    if dic1[0][0] != dic2[0][0]:
        print(n-(dic1[0][1]+dic2[0][1]))
    else:
        if len(dic1)==1:
            print(n- (dic1[0][1]+dic2[1][1]))
        elif len(dic2)==1:
            print(n- (dic1[1][1]+dic2[0][1]))
        elif dic1[1][1]+dic2[0][1] >= dic1[0][1]+dic2[1][1] :
            print(n- (dic1[1][1]+dic2[0][1]))
        else :
            print(n- (dic1[0][1]+dic2[1][1]))

Submission status: Accepted

Solution code:
from collections import Counter
from operator import itemgetter

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

c0 = Counter(v)
if len(c0.values()) == 1:
    ans = n // 2
else:
    c1 = Counter(v[::2])
    c2 = Counter(v[1::2])

    x1 = sorted(c1.items(), reverse=True, key=itemgetter(1))
    x2 = sorted(c2.items(), reverse=True, key=itemgetter(1))

    ans = n
    if x1[0][0] == x2[0][0]:
        if len(x1) == 1:
            ans -= x2[0][1]
        elif len(x2) == 1:
            ans -= x1[0][1]
        else:
            if x1[0][1] + x2[1][1] < x2[0][1] + x1[1][1]:
                ans -= x2[0][1]
                ans -= x1[1][1]
            else:
                ans -= x1[0][1]
                ans -= x2[1][1]
    else:
        ans -= x1[0][1]
        ans -= x2[0][1]
print(ans)