Problem:


Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

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

Sample Input 1

4
3 1 3 2

Sample Output 1

1

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

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

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

Sample Input 3

4
1 1 1 1

Sample Output 3

2

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



Sample input:
4
3 1 3 2


Sample output:
1

Submission status: Accepted

Solution code:
from collections import Counter

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

l0 = l[::2]
l1 = l[1::2]
c0 = Counter(l0)
c1 = Counter(l1)
ll0 = len(l0)
ll1 = len(l1)

c0m = c0.most_common()[:2] + [(0, 0)]
c1m = c1.most_common()[:2] + [(0, 0)]

if c0m[0][0] != c1m[0][0]:
  print(ll0 - c0m[0][1] + ll1 - c1m[0][1])
else:
  print(min(ll0 - c0m[1][1] + ll1 - c1m[0][1], ll0 - c0m[0][1] + ll1 - c1m[1][1]))

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
dic_odd = {}
dic_even = {}

for i in range(0,n,2):
    if v[i] in dic_odd:
        dic_odd[v[i]] += 1
    else:
        dic_odd[v[i]] = 1

for i in range(1,n,2):
    if v[i] in dic_even:
        dic_even[v[i]] += 1
    else:
        dic_even[v[i]] = 1

odd_sorted = sorted(dic_odd.items(), key=lambda x:x[1])
even_sorted = sorted(dic_even.items(), key=lambda x:x[1])

if odd_sorted[len(odd_sorted)-1][0] == even_sorted[len(even_sorted)-1][0]:
    if len(odd_sorted) == 1 and len(even_sorted) == 1:
        print(n//2)
    elif len(odd_sorted) == 1:
        print(n//2-even_sorted[len(even_sorted)-2][1])
    elif len(even_sorted) == 1:
        print(n//2-odd_sorted[len(odd_sorted)-2][1])
    else:
        if odd_sorted[len(odd_sorted)-1][1] < even_sorted[len(even_sorted)-1][1]:
            print(n - (odd_sorted[len(odd_sorted)-2][1] + even_sorted[len(even_sorted)-1][1]))
        elif  odd_sorted[len(odd_sorted)-1][1] > even_sorted[len(even_sorted)-1][1]:
            print(n - (odd_sorted[len(odd_sorted)-1][1] + even_sorted[len(even_sorted)-2][1]))
        else:
            if odd_sorted[len(odd_sorted)-2][1] < even_sorted[len(even_sorted)-2][1]:
                print(n - (odd_sorted[len(odd_sorted)-1][1] + even_sorted[len(even_sorted)-2][1]))
            else:
                print(n - (odd_sorted[len(odd_sorted)-2][1] + even_sorted[len(even_sorted)-1][1]))
else:
    print(n-(odd_sorted[len(odd_sorted)-1][1]+even_sorted[len(even_sorted)-1][1]))

Submission status: Accepted

Solution code:
import math
import sys
from itertools import accumulate
from functools import reduce
from collections import Counter
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

sys.setrecursionlimit(10000)

mod = 1000000007

"""
# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]

# 標準入力取得
## 文字列
    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())

## 数値
    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき
"""

if __name__ == "__main__":
    N = int(sys.stdin.readline())
    L = list(map(int, sys.stdin.readline().split()))
    eL = Counter([L[i] for i in range(N) if i % 2 != 0])
    oL = Counter([L[i] for i in range(N) if i % 2 == 0])
    eLc = eL.most_common() # even list most common
    oLc = oL.most_common() # odd  list most common

    if eLc[0][0] != oLc[0][0]:
        print(N - (eLc[0][1] + oLc[0][1]))
    else:
        if len(eLc) == 1 or len(oLc) == 1:
            print(N // 2)
        else:
            print(min(N - (eLc[1][1] + oLc[0][1]), N - (eLc[0][1] + oLc[1][1])))

Submission status: Accepted

Solution code:
import math
import sys
from itertools import accumulate
from functools import reduce
from collections import Counter
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

sys.setrecursionlimit(10000)

mod = 1000000007

"""
# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]

# 標準入力取得
## 文字列
    = sys.stdin.readline().rstrip()
    = list(sys.stdin.readline().rstrip())

## 数値
    = int(sys.stdin.readline())
    = map(int, sys.stdin.readline().split())
    = list(map(int, sys.stdin.readline().split()))
    = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき
"""

if __name__ == "__main__":
    N = int(sys.stdin.readline())
    L = list(map(int, sys.stdin.readline().split()))
    l = N // 2
    eL = Counter([L[i] for i in range(N) if i % 2 != 0])
    oL = Counter([L[i] for i in range(N) if i % 2 == 0])
    eLc = eL.most_common() # even list most common
    oLc = oL.most_common() # odd  list most common

    if len(eLc) == 1 or len(oLc) == 1:
        if len(eLc) == 1 and len(oLc) == 1:
            if eLc[0] != oLc[0]:
                print(0)
            else:
                print(l)
        elif len(eLc) == 1:
            print((l - oLc[0][1]))
        else:
            print((l - eLc[0][1]))
    else:
        if eLc[0][0] != oLc[0][0]:
            print((l - eLc[0][1]) + (l - oLc[0][1]))
        else:
            print(min((l - eLc[1][1]) + (l - oLc[0][1]), (l - eLc[0][1]) + (l - oLc[1][1])))

Submission status: Accepted

Solution code:
import collections

n = int(input())
v = list(map(int, input().split()))
v1 = []
v2 = []
ans = 0

for i in range(n):
    if i % 2 == 0:
        v1.append(v[i])
    else:
        v2.append(v[i])

c1 = collections.Counter(v1)
mostChar1, mostCount1 = c1.most_common()[0]
#ndChar1, ndCount1 = c1.most_common()[1]

c2 = collections.Counter(v2)
mostChar2, mostCount2 = c2.most_common()[0]
#ndChar2, ndCount2 = c2.most_common()[1]

if mostChar1 == mostChar2:
    if len(c1.most_common()) == 1 and len(c1.most_common()) == 1:
        ans = n // 2
    elif len(c1.most_common()) == 1:
        ndChar2, ndCount2 = c2.most_common()[1]
        ans = n - mostCount1 - ndCount2
    elif len(c2.most_common()) == 1:
        ndChar1, ndCount1 = c1.most_common()[1]
        ans = n - mostCount2 - ndCount1
    else:
        ndChar1, ndCount1 = c1.most_common()[1]
        ndChar2, ndCount2 = c2.most_common()[1]
        temp1 = n - mostCount1 - ndCount2
        temp2 = n - mostCount2 - ndCount1
        ans = min(temp1, temp2)
else:
    ans = n - mostCount1 - mostCount2

print(ans)

Submission status: Accepted

Solution code:
x=[]
y=[]
n=int(input())
a=list(map(int,input().split()))
for i in range(10**5+1):
	x.append([i,0])
	y.append([i,0])
for i in range(n//2):
	x[a[i*2]][1]+=1
	y[a[i*2+1]][1]+=1
x.sort(key=lambda x:-x[1])
y.sort(key=lambda x:-x[1])
if x[0][0]!=y[0][0]:
	print(n-x[0][1]-y[0][1])
else:
	print(n-max(x[0][1]+y[1][1],
		x[1][1]+y[0][1]))

Submission status: Accepted

Solution code:
import collections as c
import sys


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

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

length1 = len(v1)
length2 = len(v2)

v1 = c.Counter(v1)
v2 = c.Counter(v2)

if len(set(v)) == 1:
    print(len(v) // 2)
    sys.exit()


if v1.most_common()[0][0] != v2.most_common()[0][0]:
    ans = (length1 - v1.most_common()[0][1]) + (length2 - v2.most_common()[0][1])
else:
    a1 = length1 - v1.most_common()[0][1]
    a2 = length1 - v1.most_common()[1][1]
    b1 = length2 - v2.most_common()[0][1]
    b2 = length2 - v2.most_common()[1][1]

    ans = min([a1+b2, a2+b1])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
v0 = v[0::2]
v1 = v[1::2]
c0 = Counter(v0).most_common()
c1 = Counter(v1).most_common()
if c0[0][0] == c1[0][0] :
    if len(c0)*len(c1) == 1 :
        print(n//2)
    elif len(c0) == 1 :
        print(n-c0[0][1]-c1[1][1])
    elif len(c1) == 1 :
        print(n-c1[0][1]-c0[1][1])
    else :
        if c0[0][1]+c1[1][1] > c1[0][1]+c0[1][1] :
            print(n-c0[0][1]-c1[1][1])
        else :
            print(n-c1[0][1]-c0[1][1])
else :
    print(n-c0[0][1]-c1[0][1])

Submission status: Accepted

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

if len(set(v))==1:
    print(n//2)
else:
    a=[]
    b=[]
    for i in range(n):
        if i%2==0:
            a.append(v[i])
        else:
            b.append(v[i])
    a=collections.Counter(a)
    a0=a.most_common()[0]
    b=collections.Counter(b)
    b0=b.most_common()[0]
    if a0[0]!=b0[0]:
        print(n-a0[1]-b0[1])
    elif a0[0]==b0[0]:
        if a0[1]>b0[1]:
            print(n-a0[1]-b.most_common()[1][1])
        elif a0[1]<b0[1]:
            print(n-b0[1]-a.most_common()[1][1])
        elif a0==b0:
            if b.most_common()[1][1]>=a.most_common()[1][1]:
                print(n-a0[1]-b.most_common()[1][1])
            elif b.most_common()[1][1]<=a.most_common()[1][1]:
                print(n-b0[1]-a.most_common()[1][1])

Submission status: Accepted

Solution code:
N=int(input())
lv=list(map(int,input().split()))
dev={}
dod={}
#cev=0#devの種類数
#cod=0#dodの種類数
max1_ev=[0,0]#数字,個数
max2_ev=[0,0]
max1_od=[0,0]#数字,個数
max2_od=[0,0]

for i in range(N):
    if i%2==0:#even
        #max1_ev=[0,0]#数字,個数
        #max2_ev=[0,0]
        #毎回初期化していた！！

        if lv[i] in dev:#辞書の更新
            dev[lv[i]]+=1
        else:
            dev[lv[i]]=1
            #maxの更新
            #cev+=1
            #if cev==1:
            #    max1_ev=[i,lv[i]]
            #elif cev==2:
            #    max2_ev=[i,lv[i]]
        ##3種目以降
        #最大個数の更新
        if dev[lv[i]]>max1_ev[1]:
            a=max1_ev
            max1_ev=[lv[i],dev[lv[i]]]
            max2_ev=a
        #elif dev[lv[i]]>max2_ev[1]:
        elif dev[lv[i]]>=max2_ev[1]:
            max2_ev=[lv[i],dev[lv[i]]]
        #print("dev[lv[i]]",dev[lv[i]])
        #print("max1__ev[1]",max1_ev[1])
        #print("max1_ev",max1_ev)
        #print("max2_ev",max2_ev)
        #if i==0:
        #    max1=[0,1]
        #elif i==1:
        #    max2
    else:#odd
        #max1_od=[0,0]
        #max2_od=[0,0]
        if lv[i] in dod:
            dod[lv[i]]+=1
        else:
            dod[lv[i]]=1
        if dod[lv[i]]>max1_od[1]:
            a=max1_od
            max1_od=[lv[i],dod[lv[i]]]
            max2_od=a
        #elif dod[lv[i]]>max2_od[1]:
        elif dod[lv[i]]>=max2_od[1]:
            max2_od=[lv[i],dod[lv[i]]]
#print("dev",dev)#ok
#print("dod",dod)#ok
#print("max1_ev",max1_ev)
#print("max2_ev",max2_ev)
#デバッグ消し忘れ

#if len(dev)==1 and len(dod)==1:
#    print(int(N/2))
#elif len(dev)==1:
    #for i in range(len(dod)):
    #    if i==0:
    #        max1_od=
#    if max1_ev[0]!=max1_od[0]:
#        print(N-max1_ev[1]-max1_od[1])
##        print(max)
if max1_ev[0]!=max1_od[0]:
    print(N-max1_ev[1]-max1_od[1])
elif len(dev)==1 and len(dod)==1:
    print(int(N/2))
elif len(dev)==1:
    print(int(N/2)-max2_od[1])
elif len(dod)==1:
    print(int(N/2)-max2_ev[1])
else:
    #print(max(N-max1_ev[1]-max2_od[1],N-max2_ev[1]-max1_od[1]))#minをmaxとしていた
    print(min(N-max1_ev[1]-max2_od[1],N-max2_ev[1]-max1_od[1]))

Submission status: Accepted

Solution code:
N=int(input())
lv=list(map(int,input().split()))
dev={}
dod={}
for i in range(N):
    if i%2==0:#even
        if lv[i] in dev:
            dev[lv[i]]+=1
        else:
            dev[lv[i]]=1
    else:#odd
        if lv[i] in dod:
            dod[lv[i]]+=1
        else:
            dod[lv[i]]=1

max1_ev=[0,0]
max2_ev=[0,0]
max1_od=[0,0]
max2_od=[0,0]
#for i in range(len(dev)):
for v in dev:#max even
    if dev[v]>max1_ev[1]:
        a=max1_ev
        max1_ev=[v,dev[v]]
        max2_ev=a
    elif dev[v]>max2_ev[1]:
        max2_ev=[v,dev[v]]
for v in dod:#max odd
    if dod[v]>max1_od[1]:
        a=max1_od
        max1_od=[v,dod[v]]
        max2_od=a
    elif dod[v]>max2_od[1]:
        max2_od=[v,dod[v]]
if max1_ev[0]!=max1_od[0]:
    print(N-max1_ev[1]-max1_od[1])
else:
    print(min(N-max1_ev[1]-max2_od[1],N-max2_ev[1]-max1_od[1]))

Submission status: Accepted

Solution code:
import collections
n=int(input())
v=list(map(int,input().split()))
ans=0
e=v[::2]
o=v[1::2]
ec=collections.Counter(e)
oc=collections.Counter(o)
E=len(e)
O=len(o)

E1=ec.most_common()[0][0]
e1=ec.most_common()[0][1]
try:
  E2=ec.most_common()[1][0]
  e2=ec.most_common()[1][1]
except:
  e2=0
O1=oc.most_common()[0][0]
o1=oc.most_common()[0][1]
try:
  O2=oc.most_common()[1][0]
  o2=oc.most_common()[1][1]
except:
  o2=0

if E1!=O1:
  ans=(E-e1)+(O-o1)
else:
  ans=min((E-e1)+(O-o2),(E-e2)+(O-o1))
print(ans)

Submission status: Accepted

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

a = {}
for i in range(0, len(v), 2):
    a[v[i]] = a.get(v[i], 0) + 1

b = {}
for i in range(1, len(v), 2):
    b[v[i]] = b.get(v[i], 0) + 1

sorted_a = sorted(a.items(), reverse=True, key=lambda x: x[1])
sorted_b = sorted(b.items(), reverse=True, key=lambda x: x[1])

res = 0
if sorted_a[0][0] == sorted_b[0][0]:
    if sorted_a[0][1] > sorted_b[0][1]:
        sorted_b[0], sorted_b[1] = sorted_b[1], sorted_b[0]
    elif sorted_a[0][1] < sorted_b[0][1]:
        sorted_a[0], sorted_a[1] = sorted_a[1], sorted_a[0]
    else:
        if len(sorted_b) == 1:
            res += sorted_b[0][1]
        elif sorted_a[1][1] > sorted_b[1][1]:
            sorted_a[0], sorted_a[1] = sorted_a[1], sorted_a[0]
        else:
            sorted_b[0], sorted_b[1] = sorted_b[1], sorted_b[0]

res += len(v) - sorted_a[0][1] - sorted_b[0][1]

print(res)

Submission status: Accepted

Solution code:
n=int(input())
a,b=[None]*100000,[None]*100000
for i in range(100000):
    a[i]=[None]*2
    a[i][0]=0
    a[i][1]=i
    b[i]=[None]*2
    b[i][0]=0
    b[i][1]=i
l=list(map(int,input().split()))
for i in range(n//2):
    a[l[2*i]-1][0]+=1
    b[l[2*i+1]-1][0]+=1
a.sort(reverse=True)
b.sort(reverse=True)
if a[0][1]==b[0][1]:
    print(n-max(a[0][0]+b[1][0],a[1][0]+b[0][0]))
else:
    print(n-a[0][0]-b[0][0])

Submission status: Accepted

Solution code:
N=int(input())
lv=list(map(int,input().split()))
dev={}
dod={}
#cev=0#devの種類数
#cod=0#dodの種類数
max1_ev=[0,0]#数字,個数
max2_ev=[0,0]
max1_od=[0,0]#数字,個数
max2_od=[0,0]

for i in range(N):
    if i%2==0:#even
        #max1_ev=[0,0]#数字,個数
        #max2_ev=[0,0]
        #毎回初期化していた！！

        if lv[i] in dev:#辞書の更新
            dev[lv[i]]+=1
        else:
            dev[lv[i]]=1
            #maxの更新
            #cev+=1
            #if cev==1:
            #    max1_ev=[i,lv[i]]
            #elif cev==2:
            #    max2_ev=[i,lv[i]]
        ##3種目以降
        #最大個数の更新
        if dev[lv[i]]>max1_ev[1]:
            max1_ev=[lv[i],dev[lv[i]]]
        #elif dev[lv[i]]>max2_ev[1]:
        elif dev[lv[i]]>=max2_ev[1]:
            max2_ev=[lv[i],dev[lv[i]]]
        #print("dev[lv[i]]",dev[lv[i]])
        #print("max1__ev[1]",max1_ev[1])
        #print("max1_ev",max1_ev)
        #print("max2_ev",max2_ev)
        #if i==0:
        #    max1=[0,1]
        #elif i==1:
        #    max2
    else:#odd
        #max1_od=[0,0]
        #max2_od=[0,0]
        if lv[i] in dod:
            dod[lv[i]]+=1
        else:
            dod[lv[i]]=1
        if dod[lv[i]]>max1_od[1]:
            max1_od=[lv[i],dod[lv[i]]]
        #elif dod[lv[i]]>max2_od[1]:
        elif dod[lv[i]]>=max2_od[1]:
            max2_od=[lv[i],dod[lv[i]]]
#print("dev",dev)#ok
#print("dod",dod)#ok
#print("max1_ev",max1_ev)
#print("max2_ev",max2_ev)


#if len(dev)==1 and len(dod)==1:
#    print(int(N/2))
#elif len(dev)==1:
    #for i in range(len(dod)):
    #    if i==0:
    #        max1_od=
#    if max1_ev[0]!=max1_od[0]:
#        print(N-max1_ev[1]-max1_od[1])
##        print(max)
if max1_ev[0]!=max1_od[0]:
    print(N-max1_ev[1]-max1_od[1])
elif len(dev)==1 and len(dod)==1:
    print(int(N/2))
elif len(dev)==1:
    print(int(N/2)-max2_od[1])
elif len(dod)==1:
    print(int(N/2)-max2_ev[1])
else:
    #print(max(N-max1_ev[1]-max2_od[1],N-max2_ev[1]-max1_od[1]))#minをmaxとしていた
    print(min(N-max1_ev[1]-max2_od[1],N-max2_ev[1]-max1_od[1]))

Submission status: Accepted

Solution code:
import math,fractions
from collections import Counter
n = int(input())
v = [int(i) for i in input().split()]
a = Counter(v[1::2]).most_common()
b = Counter(v[0::2]).most_common()
a.append((0,0))
b.append((0,0))

if a[0][0] != b[0][0]:
    print(n//2 - a[0][1] + n//2 - b[0][1])
else:
    tmp_x = n//2 - a[1][1] + n//2 - b[0][1]
    tmp_y = n//2 - a[0][1] + n//2 - b[1][1]
    print(min(tmp_x, tmp_y))

Submission status: Accepted

Solution code:
import math,fractions
from collections import Counter
n = int(input())
v = [int(i) for i in input().split()]
a = [v[i] for i in range(n) if i%2]
b = [v[i] for i in range(n) if i%2==0]
cnt_a = Counter(a)
cnt_b = Counter(b)
max_a = cnt_a.most_common()[0][1]
max_b = cnt_b.most_common()[0][1]

str_a = cnt_a.most_common()[0][0]
str_b = cnt_b.most_common()[0][0]

max_2a = 0
max_2b = 0

for i in cnt_a:
    if str_a != i  and cnt_a[i] > max_2a:
        max_2a = cnt_a[i]

for i in cnt_b:
    if str_b != i  and cnt_b[i] > max_2b:
        max_2b = cnt_b[i]

if str_a != str_b:
    print(n//2 - max_a + n//2 - max_b)
else:
    tmp_a = n//2 - max_2a + n//2 - max_b
    tmp_b = n//2 - max_a + n//2 - max_2b
    print(min(tmp_a,tmp_b))

Submission status: Accepted

Solution code:
import collections

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

hantei = 0

for i in range(0,n):
    if u[0] != u[i]:
        hantei = 1
        break

if hantei == 0:
    print(n // 2)
else:
   u1 = [] 
   u2 = [] 
   ans = 0
   for j in range(n):
       if j % 2 == 0:
           u2.append(u[j])
       else:
           u1.append(u[j])
   u1_list =collections.Counter(u1)
   u2_list =collections.Counter(u2)
   values1, counts1 = u1_list.most_common()[0]
   values2, counts2 = u2_list.most_common()[0]
   if values1 != values2:
       ans += n // 2 - counts1
       ans += n // 2 - counts2
   else:
       if len(u1_list) > 1 and len(u2_list) > 1:
           values3, counts3 = u1_list.most_common()[1]
           values4, counts4 = u2_list.most_common()[1]
           ans1 = n - counts1 - counts4
           ans2 = n - counts2 - counts3
           ans = max(ans1,ans2)
       elif len(u1_list) > 1 and len(u2_list) < 2:
           values3, counts3 = u1_list.most_common()[1]
           ans = n - counts2 - counts3
       elif len(u1_list) < 2 and len(u2_list) > 1:
           values3, counts3 = u2_list.most_common()[1]
           ans = n - counts1 - counts3

       ans = min(ans1,ans2)
   print(ans)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
o = []
e = []
for i, va in enumerate(v):
    if i % 2 == 0:
        o.append(va)
    else:
        e.append(va)
import collections
o = collections.Counter(o).most_common()
e = collections.Counter(e).most_common()
if o[0][0] == e[0][0]:
    if len(o) == 1 and len(e) == 1:
        print(min(o[0][1], e[0][1]))
    elif len(o) == 1:
        print(n - o[0][1] - e[1][1])
    elif len(e) == 1:
        print(n - o[1][1] - e[0][1])
    else:
        print(n - o[0][1] - max(o[1][1], e[1][1]))
else:
    print(n - o[0][1] - e[0][1])

Submission status: Accepted

Solution code:


import collections
import itertools
import sys


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

n = getint()
vs = getints()


even = vs[0::2]
odds = vs[1::2]

even_counter = collections.Counter()
odds_counter = collections.Counter()

for e in even:
    even_counter[e] += 1
for e in odds:
    odds_counter[e] += 1

even_counter = list(sorted(list(even_counter.items()), key=lambda s: -s[1]))
odds_counter = list(sorted(list(odds_counter.items()), key=lambda s: -s[1]))

if len(even_counter) < 2:
    even_counter.append((-1, 0))
if len(odds_counter) < 2:
    odds_counter.append((-2, 0))

res = 2 * n
for i in range(2):
    for j in range(2):
        even_val = even_counter[i][0]
        odds_val = odds_counter[j][0]
        if even_val == odds_val:
            continue
        tmp1 = len(list(filter(lambda s: s != even_val, even)))
        tmp2 = len(list(filter(lambda s: s != odds_val, odds)))
        res = min(res, tmp1 + tmp2)

print(res)

Submission status: Accepted

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

a = Counter(v[0::2]).most_common()
b = Counter(v[1::2]).most_common()
a.append([0,0])
b.append([0,0])
if a[0][0] != b[0][0]:
  print(n-(a[0][1]+b[0][1]))
else:
  print(min(n-(a[0][1]+b[1][1]),n-(a[1][1]+b[0][1])))

Submission status: Accepted

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

even=Counter(v[0::2]).most_common()     #num,count
odd=Counter(v[1::2]).most_common()

i=0
while i<len(even):
    e=even[i]
    o=odd[i]
    if e[0]!=o[0]:
        if e[1]>=o[1]:
            ans=n-e[1]-odd[0][1]
            break
        else:
            ans=n-o[1]-even[0][1]
            break
    i+=1
else:
    ans=n-even[0][1] if len(even)==1 else n-even[0][1]-odd[1][1]
print(ans)

Submission status: Accepted

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

vo = [0] * (10**5 + 1)
ve = [0] * (10**5 + 1)

for i in range(n):
    if (i+1) % 2 == 0:
        ve[v[i]] += 1
    else:
        vo[v[i]] += 1
ve1 = sorted(ve, reverse = True)
vo1 = sorted(vo, reverse = True)
if vo.index(max(vo)) != ve.index(max(ve)):
    print(n - max(vo) - max(ve))
    
elif max(vo) > max(ve):    
    print(n - max(vo) - ve1[1])
elif max(vo) < max(ve):
    print(n - max(ve) - vo1[1])
else:
    print(n - max(vo) - max(ve1[1], vo1[1]))

Submission status: Accepted

Solution code:
#list(map(int,input().split()))
import math
import sys
import collections
n = int(input())
vvv = list(map(int,input().split()))
odd = [vvv[i] for i in range(1,n,2)]
even = [vvv[i] for i in range(0,n,2)]
c_o = collections.Counter(odd)
_, odd_c = zip(*c_o.most_common())
c_e = collections.Counter(even)
_, even_c = zip(*c_e.most_common())
if c_o.most_common()[0][0] == c_e.most_common()[0][0]:
  print(min(sum(list(odd_c)[1:]) + list(even_c)[0] + sum(list(even_c)[2:]),
      sum(list(even_c)[1:]) + list(odd_c)[0] + sum(list(odd_c)[2:])))
  sys.exit()
ans = sum(list(odd_c)[1:]) + sum(list(even_c)[1:])
print(ans)

Submission status: Accepted

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

vo = [0]*(10**5+1)
ve = [0]*(10**5+1)

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

if vo.index(max(vo)) == ve.index(max(ve)):
    ans1 = sum(vo) - max(vo) + sum(ve) - sorted(ve)[-2]
    ans2 = sum(vo) - sorted(vo)[-2] + sum(ve) - max(ve)
    print(min(ans1, ans2))
else:
    print(sum(vo) - max(vo) + sum(ve) - max(ve))

Submission status: Accepted

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

vo=[0]*(10**5+1)
ve=[0]*(10**5+1)

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


if vo.index(max(vo))==ve.index(max(ve)):
    ans1 = sum(vo)-max(vo)+sum(ve)-sorted(ve)[-2]
    ans2 = sum(vo)-sorted(vo)[-2]+sum(ve)-max(ve)
    print(min(ans1,ans2))
else:
    print(sum(vo)-max(vo)+sum(ve)-max(ve))

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
v = list(map(int, input().split()))
e = []
o = []
for idx, i in enumerate(v):
    if idx % 2 == 0:
        o.append(i)
    else:
        e.append(i)

e = Counter(e).most_common()
o = Counter(o).most_common()
e1 = e[0][1]
e2 = e[1][1] if len(e) > 1 else 0
o1 = o[0][1]
o2 = o[1][1] if len(o) > 1 else 0
cnt = 0
if e[0][0] != o[0][0]:
    cnt = N - e1 - o1
else:
    o = N - e1 - o2
    e = N - e2 - o1
    cnt = min(o, e)
print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter
from operator import itemgetter

def mpsort(m):
	ary = sorted([[k, v] for (k, v) in m.items()], key=itemgetter(1), reverse=True)
	if len(ary) == 1:
		return ary[0], [0, 0]
	return ary[0], ary[1]

def main():
	n = int(input().strip())
	v = list(map(int, input().strip().split()))
	gu = [i for i in v[::2]]
	ki = [i for i in v[1::2]]
	gu_map = Counter(gu)
	ki_map = Counter(ki)
	s = n//2
	# print(gu_map)
	gu_f, gu_s = mpsort(gu_map)
	ki_f, ki_s = mpsort(ki_map)

	if gu_f[0] == ki_f[0]:
			print(min(s - gu_s[1] + s - ki_f[1],
					  s - gu_f[1] + s - ki_s[1]))
	else:
		print(s - gu_f[1] + s - ki_f[1])

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter
from operator import itemgetter

def mpsort(m):
	ary = sorted([[k, v] for (k, v) in m.items()], key=itemgetter(1), reverse=True)
	if len(ary) == 1:
		return ary[0], [0, 0]
	return ary[0], ary[1]

def main():
	n = int(input().strip())
	v = list(map(int, input().strip().split()))
	gu = [i for i in v[::2]]
	ki = [i for i in v[1::2]]
	gu_map = Counter(gu)
	ki_map = Counter(ki)
	gu_sum = sum(gu_map.values())
	ki_sum = sum(ki_map.values())
	# print(gu_map)
	gu_f, gu_s = mpsort(gu_map)
	ki_f, ki_s = mpsort(ki_map)

	if gu_f[0] == ki_f[0]:
			print(min(gu_sum - gu_s[1] + ki_sum - ki_f[1],
					  gu_sum - gu_f[1] + ki_sum - ki_s[1]))
	else:
		print(gu_sum - gu_f[1] + ki_sum - ki_f[1])
		
if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
even = []
odd = []
for i in range(n//2):
  even.append(v[2*i]-1)
  odd.append(v[2*i+1]-1)
#print(even,odd)
EVEN = [[i,0] for i in range(100000)]
ODD = [[i,0] for i in range(100000)]
for i in range(n//2):
  EVEN[even[i]][1] += 1
  ODD[odd[i]][1] += 1
maxE = max(EVEN, key=lambda x:x[1])
maxO = max(ODD,  key=lambda x:x[1])
#print(maxE,maxO)
if maxE[0] != maxO[0]:
  print( n - maxE[1] - maxO[1] )
else:
  del EVEN[maxE[0]]
  del ODD[maxO[0]]
  canE = maxE[1] + max(ODD,  key=lambda x:x[1])[1]
  canO = maxO[1] + max(EVEN, key=lambda x:x[1])[1]
  if canE > canO:
    print( n - canE )
  else:
    print( n - canO )

Submission status: Accepted

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

k = [[0]*2 for i in range(10**5+1)]
g = [[0]*2 for i in range(10**5+1)]
# k = [[0]*2 for i in range(10)]
# g = [[0]*2 for i in range(10)]

_ksum = 0
_gsum = 0

for i in range(N):
    if i % 2 == 0:
        k[A[i]][0] += 1
        k[A[i]][1] = A[i]
        _ksum += 1
    else:
        g[A[i]][0] += 1
        g[A[i]][1] = A[i]
        _gsum += 1

_kmax = 0
_gmax = 0

b_k = k.copy()
b_g = g.copy()

b_k.sort()
b_g.sort()

if b_k[-1][1] != b_g[-1][1]:
    print(_ksum-b_k[-1][0]+_gsum-b_g[-1][0])

else:
    print(min(_ksum-b_k[-1][0]+_gsum-b_g[-2][0], _ksum-b_k[-2][0]+_gsum-b_g[-1][0]))

Submission status: Accepted

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

v0 = v[::2]
v1 = v[1::2]
counter0 = [0] * (10 ** 5 + 1)
counter1 = [0] * (10 ** 5 + 1)

for vi in v0:
    counter0[vi] += 1
for vi in v1:
    counter1[vi] += 1

sorted_counter0 = sorted(counter0, reverse=True)
sorted_counter1 = sorted(counter1, reverse=True)

if counter0.index(max(counter0)) == counter1.index(max(counter1)):
    print(min(len(v) - sorted_counter0[0] - sorted_counter1[1],
              len(v) - sorted_counter0[1] - sorted_counter1[0]))
else:
    print(len(v) - sorted_counter0[0] - sorted_counter1[0])

Submission status: Accepted

Solution code:
from collections import Counter


def solve(V):
    n = len(V)
    ac = Counter(V[::2]).most_common(2)
    bc = Counter(V[1::2]).most_common(2)
   
    if ac[0][0] != bc[0][0]:
        return n - ac[0][1] - bc[0][1]
    else:
        if len(bc) == 1 or len(ac) == 1:
            return n // 2
        else:
            return n - max(ac[0][1] + bc[1][1], ac[1][1] + bc[0][1])


if __name__ == "__main__":
    n = int(input())
    l = input()

    print(solve(l.split(" ")))

Submission status: Accepted

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

l0 = l[::2]
l1 = l[1::2]

l0.sort()
l1.sort()

# print(l0)
# print(l1)

memo0 = [[1, l0[0]]]
memo1 = [[1, l1[0]]]
for i in range(1, n//2):
    if memo0[-1][1] == l0[i]:
        memo0[-1][0] += 1
    else:
        memo0.append([1, l0[i]])

    if memo1[-1][1] == l1[i]:
        memo1[-1][0] += 1
    else:
        memo1.append([1, l1[i]])

memo0.sort(reverse=True)
memo1.sort(reverse=True)

memo0.append([0, -1])
memo1.append([0, -1])

# print(memo0)
# print(memo1)

if memo0[0][1]!=memo1[0][1]:
    print(n-memo0[0][0]-memo1[0][0])
else:
    ans1 = n- memo0[0][0]-memo1[1][0]
    ans2 = n- memo0[1][0]-memo1[0][0]
    print(min(ans1, ans2))

Submission status: Accepted

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

kisuus = [a_list[i] for i in range(0,N,2)]
guusuus = [a_list[i] for i in range(1,N,2)]

n_kisuu = len(kisuus)
n_guusuus = len(guusuus)

from collections import Counter
kisuu_cnter = Counter(kisuus)
guusuu_cnter = Counter(guusuus)

def func(cnter):
    tmp = [(k,v) for k, v in sorted(cnter.items(), key=lambda x: -x[1])] + [(-1,0)]
    tmp0 = tmp[0]
    tmp1 = tmp[1]
    return tmp0,tmp1

(k0,kcnt0),(k1,kcnt1) = func(kisuu_cnter)
(g0,gcnt0),(g1,gcnt1) = func(guusuu_cnter)
kcnt0 = n_kisuu-kcnt0
kcnt1 = n_kisuu-kcnt1
gcnt0 = n_guusuus-gcnt0
gcnt1 = n_guusuus-gcnt1

if k0 != g0:
    print(kcnt0+gcnt0)
else:
    print(min(kcnt0+gcnt1,kcnt1+gcnt0))