Problem:


Score : 300 points

Problem Statement

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

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

Exactly two different numbers appear in the sequence.

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

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

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

Sample Input 1

4
3 1 3 2

Sample Output 1

1

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

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

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

Sample Input 3

4
1 1 1 1

Sample Output 3

2

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



Sample input:
4
3 1 3 2


Sample output:
1

Submission status: Accepted

Solution code:
n = int(input())
v = [int(i) for i in input().split()]
m = n//2

a = [v[2*i] for i in range(0,m)]
b = [v[2*i+1] for i in range(0,m)]

def mode(arr):
        r = [0] * (max(arr) + 1)
        for a in arr:
            r[a] += 1
        return r.index(max(r))

A = mode(a)
B = mode(b)

v.remove(A)

if v == []:
    print(m)
    exit()
if A == B:
    if a.count(A) < b.count(B):
        a.remove(A)
        C = mode(a)
        print(n - a.count(C) - b.count(B))
    else:
        if b.count(B) == m:
            print(n - m)
        else:
            if a.count(A) == b.count(B):
                t = a.count(A)
                a.remove(A)
                b.remove(B)
                D = mode(a)
                E = mode(b)
                if a.count(D) > b.count(E):
                    print(n - t - a.count(D))
                else:
                    print(n - t - b.count(E))
            else:
                b.remove(B)
                C = mode(b)
                print(n - a.count(A) - b.count(C))
else:
    print(n - a.count(A) - b.count(B))

Submission status: Accepted

Solution code:
import collections

n=int(input())
v = input().split()

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

c1 = collections.Counter(v1)
c2 = collections.Counter(v2)
if(c1.most_common()[0][0] != c2.most_common()[0][0]):
	print(n - c1.most_common()[0][1] - c2.most_common()[0][1])
else:
	if(c1.most_common()[0][1] == n/2):
		count1 = c2.most_common()[0][1]
	else:
		count1 = c1.most_common()[1][1] + c2.most_common()[0][1]

	if(c2.most_common()[0][1] == n/2):
		count2 = c1.most_common()[0][1]
	else:
		count2 = c1.most_common()[0][1] + c2.most_common()[1][1]

	if(count1 < count2):
		print(n - count2)
	else:
		print(n - count1)

Submission status: Accepted

Solution code:
import collections

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

if len(list(set(v))) == 1:
    print(int(n/2))
    exit()
    
v1 = []
v2 = []
for i in range(n):
    if i%2:
        v1.append(v[i])
    else:
        v2.append(v[i])
v1 = collections.Counter(v1)
v2 = collections.Counter(v2)

v1_cnt = v1.most_common()
v2_cnt = v2.most_common()

if len(v1_cnt) > 1:
    e1 = v1_cnt[0]
    e2 = v1_cnt[1]
else:
    e1 = v1_cnt[0]
    e2 = e1
if len(v2_cnt) > 1:
    d1 = v2_cnt[0]
    d2 = v2_cnt[1]
else:
    d1 = v2_cnt[0]
    d2 = d1

if e1[0] != d1[0]:
    print(n - e1[1] - d1[1])
else:
    more = max(e1[1] + d2[1], e2[1] + d1[1])
    print(n - more)

Submission status: Accepted

Solution code:
from collections import Counter
 
def f(n,v):
	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]:
		return n-(a[0][1]+b[0][1])
	else:
		return min(n-(a[1][1]+b[0][1]),n-(a[0][1]+b[1][1]))
 
if __name__=="__main__":
	x=int(input())
	data=[int(i) for i in input().split()]
	print(f(x,data))

Submission status: Accepted

Solution code:
from collections import Counter

def f(n,v):
	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]:
		return n-(a[0][1]+b[0][1])
	else:
		return min(n-(a[1][1]+b[0][1]),n-(a[0][1]+b[1][1]))

if __name__=="__main__":
	x=int(input())
	data=[int(i) for i in input().split()]
	print(f(x,data))

Submission status: Accepted

Solution code:
from collections import Counter

a = int(input())

b = list(map(int,input().split()))
d = max(b)
c = [0 for i in range(pow(10,5)+1)]
cc = [0 for i in range(pow(10,5)+1)]
for i in range(len(b)):
    if i % 2 == 0:
        c[b[i]] += 1
    else:
        cc[b[i]] += 1
first = Counter(b[::2]).most_common(2) + [(0, 0)]
latter = Counter(b[1::2]).most_common(2) + [(0, 0)]
one = 0
onee = 0
two = 0
twoo = 0
k = 0
kk = 0
for i in range(d+1):
    if one <= c[i]:
        onee = one
        one = c[i]
        k = i
    elif onee <= c[i]:
        onee = c[i]
    if two <= cc[i]:
        twoo = two
        two = cc[i]
        kk = i
    elif twoo <= cc[i]:
        twoo = cc[i]
t = max(one+twoo,onee+two)

if k != kk:
    print(a-one-two)
else:
    print(a-t)

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    r"""
    数列 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)
    assert ans == simple(n, v)
    print(ans)


def simple(n, v):
    # most_commonを事前に使い短くする
    # 1種類しかないならば存在しない種類を0個で足して条件分岐を潰す
    c1 = Counter(v[::2]).most_common() + [(0, 0)]
    c2 = Counter(v[1::2]).most_common() + [(0, 0)]

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

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)

    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:
import collections
n=int(input())
v=list(map(int,input().split()))
s=[]
t=[]
for i in range(n):
    if i%2==0:
        s.append(v[i])
    else:
        t.append(v[i])
p=collections.Counter(s)
q=collections.Counter(t)
a=p.most_common()[0][0]
a1=p.most_common()[0][1]
b=q.most_common()[0][0]
b1=q.most_common()[0][1]
if a!=b:
    print(n-a1-b1)
elif len(set(s))==1 and len(set(t))==1:
            print(int(n/2))
else:
     c=p.most_common()[1][1]
     d=q.most_common()[1][1]
     print(n-max(c+b1,d+a1))

Submission status: Accepted

Solution code:
import collections

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

odd = []
even = []

for i in range(n):
	if i % 2 == 0:
		even.append(nums[i])
	else:
		odd.append(nums[i])

counter_even = collections.Counter(even)
counter_odd = collections.Counter(odd)

change_even = len(even) - counter_even.most_common()[0][1]
change_odd = len(odd) - counter_odd.most_common()[0][1]
if len(counter_even.most_common()) == 1 and len(counter_odd.most_common()) == 1 and counter_even.most_common()[0][0] == counter_odd.most_common()[0][0]:
	print(n//2)
elif counter_even.most_common()[0][0] != counter_odd.most_common()[0][0]:
	print(change_odd + change_even)
else:
	if change_even < change_odd:
		change_odd_2 = len(odd) - counter_odd.most_common()[1][1]
		print(change_odd_2 + change_even)
	elif change_even > change_odd:
		change_even_2 = len(even) - counter_even.most_common()[1][1]
		print(change_even_2 + change_odd)
	else:
		change_even_2 = len(even) - counter_even.most_common()[1][1]
		change_odd_2 = len(odd) - counter_odd.most_common()[1][1]
		if change_even_2 < change_odd_2:
			print(change_even_2 + change_odd)
		else:
			print(change_odd_2 + change_even)

Submission status: Accepted

Solution code:
from collections import Counter

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

# 最終的な2値をx, yとしたとき書き換える要素が最小の場合は、
# xは、書き換えが最も少ない数。つまり、もともと最も多い数
# yも同様。
# ただ、xとyがかぶる場合があるので、それに対処。
if len(Counter(v)) > 1:
    v1 = v[0:n+1:2]
    # print(v)
    # print(v1)
    v2 = v[1:n+2:2]
    # print(v2)
    v1_count = Counter(v1)
    v2_count = Counter(v2)

    v1_1st_common = v1_count.most_common(1)
    v2_1st_common = v2_count.most_common(1)
    sols = []
    # print(v1_1st_common[0][1], v2_1st_common[0][1])
    # v1を基準として多い方を考える場合。
    n1 = v1_1st_common[0][1]
    # v1の1位とv2の1位がかぶる場合
    if v1_1st_common[0][0] == v2_1st_common[0][0]:
        if len(v2_count) >= 2:
            v2_2nd_common = v2_count.most_common(2)
            n2 = v2_2nd_common[1][1]
        else:
            print(n//2)
    else:
        n2 = v2_1st_common[0][1]
    sols.append(n-n1-n2)

    # v2を基準として多い方を考える場合。
    n2 = v2_1st_common[0][1]
    # v1の1位とv2の1位がかぶる場合
    if v1_1st_common[0][0] == v2_1st_common[0][0]:
         if len(v1_count) >= 2:
            v1_2nd_common = v1_count.most_common(2)
            n1 = v1_2nd_common[1][1]
         else:
            print(n//2)
    else:
        n1 = v1_1st_common[0][1]
    sols.append(n-n1-n2)
    print(min(sols))
else:
    print(n//2)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = list(map(int, input().split()))
odd = []
even = []

cnt = 1
for i in v:
	if cnt % 2 != 0:
		odd.append(i)
	else:
		even.append(i)
	cnt += 1

x = Counter(even).most_common()
y = Counter(odd).most_common()

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

Submission status: Accepted

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

k = n // 2
half = [Counter(), Counter()]
for i in range(k):
    for par in range(2):
        half[par][v[2*i+par]] += 1

cmn = [0, 0]
for par in range(2):
    cmn[par] = half[par].most_common(2)
    if len(cmn[par]) == 1:
        cmn[par].append(('ghost', 0))
if cmn[0][0][0] != cmn[1][0][0]:
    ret = n - cmn[0][0][1] - cmn[1][0][1]
else:
    ret = n - max(cmn[0][0][1] + cmn[1][1][1], cmn[0][1][1] + cmn[1][0][1])
print(ret)

Submission status: Accepted

Solution code:
import sys
import math

N = sys.stdin.readline().strip()
N = int(N)

L = sys.stdin.readline().strip().split()
L = [int(x) for x in L]

d0={}
d1={}


for i in range(0,N,2):
  a = L[i]
  c = d0.get(a)
  if c is None:
    d0[a] = 1
  else:
    d0[a] = c+1
  a = L[i+1]
  c = d1.get(a)
  if c is None:
    d1[a] = 1
  else:
    d1[a] = c+1

d01 = sorted(d0.items(), key=lambda x: -x[1])[0:2]
d11 = sorted(d1.items(), key=lambda x: -x[1])[0:2]


#print(d01)
#print(d11)


res = 0

if (d01[0][0] != d11[0][0]):
  res = N - (d01[0][1] + d11[0][1])
else:
  # ずらしたらそろえられる。どっちにそろえたほうが得？
  flag = 0
  if (len(d01)<=1):
    flag = 1
  if (len(d11)<=1):
    flag = flag + 2

  temp = 0
  if flag == 3:
    # 全部同じなら一方をすべてずらす
    temp = int(N/2)
  elif flag == 1:
    temp = d01[0][1] + d11[1][1]
  elif flag == 2:
    temp = d01[1][1] + d11[0][1]
  else:
    temp = max(d01[0][1] + d11[1][1], d01[1][1] + d11[0][1])
  res = N - temp

print(res)

Submission status: Accepted

Solution code:
import collections

#入力の取得
n = int(input())
v = list(map(int, input().split()))
odd = []
even = []

#奇数番と偶数番のリストに分割
for i in range(n):
    if i % 2 == 1:
        even.append(v[i])
    else:
        odd.append(v[i])

#それぞれのリストで一番多い数字を調べる
e = collections.Counter(even)
o = collections.Counter(odd)
v_count = collections.Counter(v)
even_max = e.most_common()[0][1]
odd_max = o.most_common()[0][1]
v_max = v_count.most_common()[0][1]

#それぞれのリストで二番目に多い数字を調べる
if e.most_common()[0][1] == int(n/2): #リスト参照でエラー回避の為の条件分岐
    even_max2 = 0
elif o.most_common()[0][1] == int(n/2):
    odd_max2 = 0
else:
    even_max2 = e.most_common()[1][1]
    odd_max2 = o.most_common()[1][1]

if e.most_common()[0][0] == o.most_common()[0][0]:#それぞれのリストで一番多い数字がかぶっていた場合
    if v_max == n: #全部同じ数字だった場合はn/2が答え
        print(int(n/2))

    elif (even_max - even_max2) <= (odd_max - odd_max2):#奇数と偶数リストのどちらを2番目に大きい数字に変えたほうがいいか　変えることによる回数増加が小さい方を採用
        print(n - even_max2 - odd_max) #偶数の方で2番目を使う
    else:
        print(n - even_max - odd_max2) #奇数の方で2番目を使う

else:
    print(n - even_max - odd_max) #それぞれ一番多い数字に他の数字を変換する　(n/2 - even_max) + (n/2 - odd_max)

Submission status: Accepted

Solution code:
import collections

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

list_e = [list[i] for i in range(0,n-1,2)]
list_o = [list[i] for i in range(1,n,2)]
e = collections.Counter(list_e)
o = collections.Counter(list_o)
mode_e=e.most_common(2)
mode_o=o.most_common(2)
mode_e.append((-1,0))
mode_o.append((-1,0))
if mode_e[0][0]==mode_o[0][0]:
    print(n-max(mode_e[0][1]+mode_o[1][1],mode_e[1][1]+mode_o[0][1]))
    # if mode_e[0][1]>mode_o[0][1]:
    #     mode_e_value=mode_e[0][1]
    #     mode_o_value=mode_o[1][1]
    # else:
    #     mode_e_value=mode_e[1][1]
    #     mode_o_value=mode_o[0][1]
else:
    mode_e_value = mode_e[0][1]
    mode_o_value = mode_o[0][1]
    print(n-mode_e_value-mode_o_value)

Submission status: Accepted

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

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

v1.sort()
v2.sort()

v1.append(-1)
v2.append(-1)


count1= 1
maxcount1=0
maxcount1sub=0
num1=0
num1sub=0

count2=1
maxcount2=0
maxcount2sub=0
num2=0
num2sub=0

for i in range(int(n/2)):
    if v1[i] == v1[i+1]:
        count1+=1
    else:
        if maxcount1sub<=count1:
            if maxcount1<=count1:
                maxcount1sub=maxcount1
                maxcount1=count1
                num1sub=num1
                num1=v1[i]
                
            else:
                maxcount1sub=count1
                num1sub=v1[i]                       
        count1=1
        
        
for i in range(int(n/2)):
    if v2[i] == v2[i+1]:
        count2+=1
    else:
        if maxcount2sub<=count2:
            if maxcount2<=count2:
                maxcount2sub=maxcount2
                maxcount2=count2
                num2sub=num2
                num2=v2[i]
                
            else:
                maxcount2sub=count2
                num2sub=v2[i]                       
        count2=1        
        
if num1!=num2:
    print(n-maxcount1-maxcount2)
else:
    print(min(n-maxcount1-maxcount2sub, n-maxcount2-maxcount1sub))

#print(v1)
#print(v2)
#print(maxcount1,maxcount1sub)
#print(num1, num1sub)
#print(maxcount2,maxcount2sub)
#print(num2, num2sub)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
lst = list(map(int, input().split()))
m1 = {}
m2 = {}
for i in range(0, n, 2):
    if lst[i] in m1:
        m1[lst[i]] += 1
    else:
        m1[lst[i]] = 1
    if lst[i+1] in m2:
        m2[lst[i+1]] += 1
    else:
        m2[lst[i+1]] = 1
if max(m1, key=m1.get) != max(m2, key=m2.get):
    mm = n - max(m1.values()) - max(m2.values())
else:
    mm1 = max(m1.values())
    mm2 = max(m2.values())
    m1[max(m1, key=m1.get)] = 0
    m2[max(m2, key=m2.get)] = 0
    mm = min(n - max(m1.values()) - mm2, n - max(m2.values()) - mm1) 
print(mm)

Submission status: Accepted

Solution code:
import collections

n = int(raw_input())
v = map(int, raw_input().split())

even = [vi for i,vi in enumerate(v) if i % 2 == 0]
odd  = [vi for i,vi in enumerate(v) if i % 2 == 1]

evenc = collections.Counter(even)
oddc = collections.Counter(odd)

evenc_most_common = evenc.most_common(len(set(even))) + [(None, 0)]
oddc_most_common = oddc.most_common(len(set(odd))) + [(None, 0)]

m = []

for i in range(2):
    for j in range(2):
        if evenc_most_common[i][0] != oddc_most_common[j][0]:
            a = len(even) - evenc_most_common[i][1]
            b = len(odd) - oddc_most_common[j][1]
            m.append(a+b)
print(min(m))

Submission status: Accepted

Solution code:
import collections

N = int(input())
v = list(map(int, input().split()))
ov = collections.Counter(v)
if ov.most_common()[0][1] == len(v):
  v[0] = (v[0] + 1) % 10
  v[1] = (v[1] + 1) % 10

odd = [v[2*i+1] for i in range(int(N/2))]
even = [v[2*i] for i in range(int(N/2))]
oo = collections.Counter(odd)
oe = collections.Counter(even)
max_odd = oo.most_common()[0][0]
max_even = oe.most_common()[0][0]

if oo.most_common()[0][0] == oe.most_common()[0][0]:
  if oo.most_common()[1][1] > oe.most_common()[1][1]:
    max_odd =  oo.most_common()[1][0]
  else:
    max_even = oe.most_common()[1][0]

print(len(odd)+len(even)-odd.count(max_odd)-even.count(max_even))

Submission status: Accepted

Solution code:
N = int(input())
v = list(map(int, input().split()))
d0 = {0: 0}
d1 = {0: 0}
for j, i in enumerate(v):
    if j % 2 == 0:
        if i in d0:
            d0[i] += 1
        else:
            d0[i] = 1
    else:
        if i in d1:
            d1[i] += 1
        else:
            d1[i] = 1

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

if l0[0][0] == l1[0][0]:
    a = min(N - l0[0][1] - l1[1][1], N - l0[1][1] - l1[0][1])
    print(a)
else:
    print(N - l0[0][1] - l1[0][1])

Submission status: Accepted

Solution code:
from collections import Counter

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

first = Counter(V[::2]).most_common(2) + [(0, 0)]
latter = Counter(V[1::2]).most_common(2) + [(0, 0)]

if first[0][0] == latter[0][0]:
    X = N - first[0][1] - latter[1][1]
    Y = N - first[1][1] - latter[0][1]
    cnt = min(X, Y)
else:
    cnt = N - first[0][1] - latter[0][1]

print(cnt)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False

# --------------------------------------------

dp = None


def main():
    N = int(input())
    V = li_input()

    VE = V[::2]
    VO = V[1::2]

    DE = collections.defaultdict(lambda: 0)
    DO = collections.defaultdict(lambda: 0)

    for ve in VE:
        DE[ve] += 1

    for vo in VO:
        DO[vo] += 1

    E1_k, E1_v = max(DE, key=DE.get), DE[max(DE, key=DE.get)]
    O1_k, O1_v = max(DO, key=DO.get), DO[max(DO, key=DO.get)]

    DE[max(DE, key=DE.get)] = 0
    DO[max(DO, key=DO.get)] = 0

    E2_k, E2_v = max(DE, key=DE.get), DE[max(DE, key=DE.get)]
    O2_k, O2_v = max(DO, key=DO.get), DO[max(DO, key=DO.get)]

    if E1_k != O1_k:
        ans = 0
        ans += len(VE) - E1_v
        ans += len(VO) - O1_v

    else:
        if set(VE) == set(VO) and len(set(VE)) == 1:
            ans = len(VO)
        else:
            ans_prio_E = 0
            ans_prio_E += len(VE) - E1_v
            ans_prio_E += len(VO) - O2_v

            ans_prio_O = 0
            ans_prio_O += len(VE) - E2_v
            ans_prio_O += len(VO) - O1_v

            ans = min(ans_prio_E, ans_prio_O)
    
    print(ans)

main()

Submission status: Accepted

Solution code:
#encoding utf-8
from collections import Counter
def main(n,v):
    odd = Counter(v[0::2]).most_common(2) + [(0,0)]
    even = Counter(v[1::2]).most_common(2) + [(0,0)]

    if odd[0][0] == even[0][0]:
        ans = min(n-odd[0][1]-even[1][1],
        n-odd[1][1]-even[0][1])
    else:
        ans = n-odd[0][1]-even[0][1]
    print(ans)


if __name__ == "__main__":
    n = int(input())
    v = list(int(i) for i in input().split())
    main(n,v)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
checklist = [0 for i in range(max(v))]
cnt = 0
for i in range(0, n, 2):
    checklist[v[i] - 1] += 1
    cnt += 1
ans = cnt - (max(checklist))
cnt2 = 0
checklist2 = [0 for i in range(max(v))]
for i in range(1, n, 2):
    checklist2[v[i] - 1] += 1
    cnt2 += 1

checklist.append(0)
checklist2.append(0)

#print(checklist), checklist2

sortchecklist = sorted(checklist)
sortchecklist2 = sorted(checklist2)

e1 = checklist.index(sortchecklist[-1])
e2 = checklist.index(sortchecklist[-2])
o1 = checklist2.index(sortchecklist2[-1])
o2 = checklist2.index(sortchecklist2[-2])
#print(sortchecklist, sortchecklist2)
anse1 = 0
anse2 = 0
anso1 = 0
anso2 = 0

for i in range(0, n, 2):
    if v[i] - 1 != e1:
        anse1 += 1
    if v[i] - 1 != e2:
        anse2 += 1
for i in range(1, n, 2):
    if v[i] - 1 != o1:
        anso1 += 1
    if v[i] - 1!= o2:
        anso2 += 1
#print(e1, e2, o1, o2)
#print(anse1, anse2, anso1, anso2)
if e1 != o1:
    print(anse1 + anso1)
else:
    print(min(anse1 + anso2, anse2 + anso1))

Submission status: Accepted

Solution code:
from collections import Counter
def main(n,v):
    # n = int(input())
    # v = list(map(int, input().split()))
    odd = Counter(v[0::2]).most_common(2) + [(0, 0)]
    even = Counter(v[1::2]).most_common(2) + [(0, 0)]

    n_half = int(n / 2)

    if (len(odd) == 1 and len(even) == 1) and odd[0][0] == even[0][0]:
        ans = n_half
    elif odd[0][0] == even[0][0]:
        ans = min(n_half - odd[0][1] + n_half - even[1][1],\
                n_half - odd[1][1] + n_half - even[0][1])
    else:
        ans = n_half - odd[0][1] + n_half - even[0][1]

    print(ans)
    # return ans

if __name__ == "__main__":
    n = int(input())
    v = list(map(int, input().split()))
    main(n,v)

Submission status: Accepted

Solution code:
import collections

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

gusuList=[]
kisuList=[]

for i in range(n):
  if(i % 2==0):
    gusuList.append(vList[i])
  else:
    kisuList.append(vList[i])
#print(gusuList)
#print(kisuList)

g = collections.Counter(gusuList)
gStatus=g.most_common()

k = collections.Counter(kisuList)
kStatus=k.most_common()

ans=-100
if(not gStatus[0][0]==kStatus[0][0]):
    ans=(n/2-gStatus[0][1])+(n/2-kStatus[0][1])
else:
    ans1=-101
    ans2=-102
    if(not len(kStatus)==1):
      ans1=(n/2-gStatus[0][1])+(n/2-kStatus[1][1])
    else:
      ans1=(n/2-gStatus[0][1])+n/2

    if(not len(gStatus)==1):
      ans2=(n/2-gStatus[1][1])+(n/2-kStatus[0][1])
    else:
      ans2=n/2+(n/2-kStatus[0][1])

    ans=min(ans1,ans2)

print(int(ans))

Submission status: Accepted

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

s = [[],[]]
for i in range(n):
    s[i % 2].append(v[i])

t = []
t.append(list(set(s[0])))
t.append(list(set(s[1])))

#print(s, t)

t_dict = [{}, {}]

for j in range(2):
    for i in range(len(t[j])):
        t_dict[j][t[j][i]] = 0

for j in range(2):
    for i in range(len(s[j])):
        t_dict[j][s[j][i]] += 1

for j in range(2):
    t_dict[j] = list(reversed(sorted(t_dict[j].items(), key=lambda x:x[1])))

#print(t_dict)
if t_dict[0][0] != t_dict[1][0]:
    print( len(s[0]) - t_dict[0][0][1] + len(s[1]) - t_dict[1][0][1])
elif len(t[0]) == 1 and len(t[1]) == 1:
    print(n // 2)
elif len(t[1]) == 1:
    print(1)
else:
    print(len(s[0]) + len(s[1]) - t_dict[0][0][1] - max(t_dict[0][1][1], t_dict[1][1][1]))

Submission status: Accepted

Solution code:
from collections import Counter
def main(n,v):
    # n = int(input())
    # v = list(map(int, input().split()))
    odd = Counter(v[0::2]).most_common(2) + [(0, 0)]
    even = Counter(v[1::2]).most_common(2) + [(0, 0)]

    n_half = int(n / 2)

    if (len(odd) == 1 and len(even) == 1) and odd[0][0] == even[0][0]:
        ans = n_half
    elif odd[0][0] == even[0][0]:
        ans = min(n_half - odd[0][1] + n_half - even[1][1],\
                n_half - odd[1][1] + n_half - even[0][1])
    else:
        ans = n_half - odd[0][1] + n_half - even[0][1]

    print(ans)
    return 0

if __name__ == "__main__":
    n = int(input())
    v = list(map(int, input().split()))
    main(n,v)

Submission status: Accepted

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

odd = v[::2]
even = v[1::2]

def makeRank(items):
	rank = {}
	for i in items:
		if i in rank.keys():
			rank[i] += 1
		else:
			rank[i] = 1
	return sorted(rank.items(), key=lambda x: -x[1])

rankOdd = makeRank(odd)
rankEven = makeRank(even)

O1 = rankOdd[0]
E1 = rankEven[0]

if len(rankOdd) == 1 and len(rankEven) == 1:
	if O1[0] == E1[0]:
		print(n // 2)
	else:
		print(0)
elif len(rankOdd) == 1:
	E2 = rankEven[1]
	if O1[0] == E1[0]:
		print(n // 2 - E2[1])
	else:
		print(n // 2 - E1[1])
elif len(rankEven) == 1:
	O2 = rankOdd[1]
	if O1[0] == E1[0]:
		print(n // 2 - O2[1])
	else:
		print(n // 2 - O1[1])
else:
	E2 = rankEven[1]
	O2 = rankOdd[1]
	if O1[0] == E1[0]:
		print(n - O1[1] - max(O2[1], E2[1]))
	else:
		print(n - O1[1] - E1[1])

Submission status: Accepted

Solution code:
from collections import Counter

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

evens = Counter(v[::2])
odds = Counter(v[1::2])

evens[0] = 0  # 一種類の数値しか含まれないときの対策
odds[0] = 0

keep = 0
for even in evens.most_common(2):
    for odd in odds.most_common(2):
        if even[0] != odd[0]:
            keep = max(keep, even[1] + odd[1])

print(n - keep)

Submission status: Accepted

Solution code:
import collections
n=int(input())
v = list(map(int,input().split()))
od=[]
ev=[]
k=int(n/2)
cev=0
cod=0

for i in range(0,n,2):
  ev.append(v[i])
  od.append(v[i+1])

ev.sort()
od.sort()

c=collections.Counter(ev)
d=collections.Counter(od)

e=c.most_common()[0][1]
f=d.most_common()[0][1]

if len(set(v))==1:
  e=k
  f=0
else:
  if(c.most_common()[0][0]==d.most_common()[0][0])and(c.most_common()[0][1]==d.most_common()[0][1]):
    e=max(c.most_common()[1][1],d.most_common()[1][1])
print(n-e-f)

Submission status: Accepted

Solution code:
import collections

n = input()
line = input().split()
odd = line[::2]
even = line[1::2]
o = collections.Counter(odd)
e = collections.Counter(even)
maxoddcnt = o.most_common()[0][1]
maxevencnt = e.most_common()[0][1]

#すべての値が同じときは半分を置換する
if odd == even and len(o.most_common()) == 1:
    print(len(odd))
    exit(0)

#奇数列，偶数列それぞれの最頻の文字が同じ場合は，いずれかを2番目に多い文字に置換しなければならない
if o.most_common()[0][0] == e.most_common()[0][0]:
    if len(o.most_common()) > 1 and len(e.most_common()) <= 1:
        maxoddcnt = o.most_common()[1][1]
    if len(o.most_common()) <= 1 and len(e.most_common()) > 1:
        maxevencnt = e.most_common()[1][1]
    if o.most_common()[1][1] + e.most_common()[0][1] >= o.most_common()[0][1] + e.most_common()[1][1]:
        maxoddcnt = o.most_common()[1][1]
    else: #if o.most_common()[1][1] + e.most_common()[0][1] <= o.most_common()[0][1] + e.most_common()[1][1] :
        maxevencnt = e.most_common()[1][1]

print(len(odd)-maxoddcnt+len(even)-maxevencnt)

Submission status: Accepted

Solution code:
import collections

N = int(input())
V = [int(_) for _ in input().split()]

def calc(vs):
    cs = collections.Counter(vs)
    cs[0] = 0
    return cs.most_common()

ve = calc(V[::2])
vo = calc(V[1::2])

result = 0

if ve[0][0] != vo[0][0]:
    result = N - ve[0][1] - vo[0][1]
else:
    result = N - max(ve[0][1] + vo[1][1], ve[1][1] + vo[0][1])

print(result)

Submission status: Accepted

Solution code:
from collections import Counter
from sys import stdin

n = int(stdin.readline().rstrip())
a = [int(i) for i in stdin.readline().rstrip().split()]

even = []
odd = []
for i in range(n):
    if i % 2 == 0:
        even.append(a[i])
    else:
        odd.append(a[i])

even_conter = Counter(even)
odd_conter = Counter(odd)

even_sorted = even_conter.most_common()
odd_sorted = odd_conter.most_common()

even_key1 = even_sorted[0][0]
even_val1 = even_sorted[0][1]
odd_key1 = odd_sorted[0][0]
odd_val1 = odd_sorted[0][1]

ans = n
if even_key1 == odd_key1:
    if len(even_sorted) >= 2:
        even_val2 = even_sorted[1][1]
        odd_val2 = odd_sorted[1][1]
        ans -= max((even_val1 + odd_val2), (odd_val1 + even_val2))
    else:
        ans -= max(even_val1, odd_val1)
else:
    ans -= (even_val1 + odd_val1)

print(ans)