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:
'''
問題文
	数列 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
v1 v2 ... vn

出力
書き換える要素数の最小値を出力してください。

入力例 1 
4
3 1 3 2
出力例 1 
1
数列 
3,1,3,2 は /\/\/\/ ではありませんが、
1 要素書き換えることで /\/\/\/ にすることができます。 例えば、4 要素目を書き換えて 3,1,3,1 とすればよいです。

入力例 2 
6
105 119 105 119 105 119
出力例 2 
0
数列 
105,119,105,119,105,119 は /\/\/\/ です。

入力例 3 
4
1 1 1 1
出力例 3 
Copy
2
数列 
1,1,1,1 は 1 種類の数からなる数列であるため、 /\/\/\/ ではありません。
'''

import collections

def calcChange(even_list, odd_list) :
	"""
	/\/\/\/にするために変更する要素数を算出

	Parameters
	----------
	even_list : list
			偶数番目の要素のリスト
	odd_list : list
			奇数番目の要素のリスト
	
	Returns
	-------
	change : int
			/\/\/\/にするために変更する要素数
			
	"""	

# 1:60   1:60
# 2:50   2:60
# 3:30   3:20
 
 
	# 変更回数を代入する変数
	change = 0

	# 各リストについて、要素名:要素数 の辞書を、Counterで作成
	even_counter = collections.Counter(even_list)
	odd_counter = collections.Counter(odd_list)
	
	# 最多要素を取得
	most_even = even_counter.most_common()
	most_odd = odd_counter.most_common()

	# 最多要素が被っていない
	if most_even[0][0] != most_odd[0][0] :
		even_change = len(even_list) - most_even[0][1]
		odd_change  = len(odd_list)  - most_odd[0][1]
		change = even_change + odd_change
	# 被ってる
	else :
		if len(most_even)==1 and len(most_odd)==1 :# 全かぶり
			change = len(even_list)
		else : # even と odd のどちらの最多要素を残すかを、それぞれの場合で比較して少ない方を採用する。
			even_1 = (len(even_list) - most_even[0][1]) + (len(odd_list)  - most_odd[1][1])
			odd_1  = (len(even_list) - most_even[1][1]) + (len(odd_list)  - most_odd[0][1])
			change = min(even_1, odd_1)


	return change



def main():
	'''
	メイン関数
	'''
	# 数列の大きさ
	n = int(input())
	# 数列
	v = list(map(int, input().split()))

	# 偶数番目を抽出した配列
	v_e = [ v[i] for i in range(0,n) if i%2==0 ]

	# 奇数番目を抽出した配列
	v_o = [ v[i] for i in range(0,n) if i%2==1 ]

	print(calcChange(v_e, v_o))


if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
from collections import Counter

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

V1 = [V[i] for i in range(0, len(V), 2)]
V2 = [V[i] for i in range(1, len(V), 2)]

x = Counter(V1).most_common(2)
y = Counter(V2).most_common(2)

if x[0][1] < y[0][1]:
    x, y = y, x
elif len(x)>1 and len(y) > 1 and x[0][1] == y[0][1] and x[1][1] > y[1][1]:
    x, y = y, x

l = int(N / 2)
res = l - x[0][1]
if y[0][0] == x[0][0]:
    res += l - (y[1][1] if len(y) > 1 else 0)
else:
    res += l - y[0][1]

print(res)

Submission status: Accepted

Solution code:
def get_first(even,even_max_ind,even_second_ind):
    even_max=0
    second=0
    for i in range(1,10**5+1):
        if(even[i]==0):
            continue
        if(even_max<even[i]):
            even_second_ind[0]=even_max_ind.pop()
            second=even[even_second_ind[0]]
            even_max_ind.clear()
            even_max_ind.append(i)
            even_max=even[i]
        elif(even_max==even[i]):
            even_max_ind.append(i)
        elif(second < even[i]):
            even_second_ind[0]=i
            second=even[even_second_ind[0]]


n=int(input())
e_max=0
o_max=0
even=[0]*(10**5+1)
odd=[0]*(10**5+1)
retu=list(map(int,input().split()))
for i in range(n):
    if(i%2==0):
        even[retu[i]]+=1
    else:
        odd[retu[i]]+=1

even_max_ind=[0]
even_second_ind=[0]
odd_max_ind=[0]
odd_second_ind=[0]

get_first(even,even_max_ind,even_second_ind)
get_first(odd,odd_max_ind,odd_second_ind)

if(len(even_max_ind)>=2 or len(odd_max_ind)>=2 or even_max_ind[0]!=odd_max_ind[0]):
    print(n-even[even_max_ind[0]] - odd[odd_max_ind[0]])
else:
    print(n-max([even[even_second_ind[0]] + odd[odd_max_ind[0]],even[even_max_ind[0]] + odd[odd_second_ind[0]]]))

Submission status: Accepted

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

dd_even = defaultdict(int)
dd_odd  = defaultdict(int)

# 偶数番目のヒストグラムを作る
for i in range(0, n, 2):
    dd_even[v[i]] += 1

l_even = list(dd_even.items())
l_even.sort(key= lambda x:x[1])
top_even = l_even[-1][0]
#print(l_even)

# 奇数番目のヒストグラムを作る
for i in range(1, n, 2):
    dd_odd[v[i]] += 1

l_odd = list(dd_odd.items())
l_odd.sort(key= lambda x:x[1])
#print(l_odd)
top_odd = l_odd[-1][0]

if top_even == top_odd:
    if len(l_odd) == 1: # つまり全部同じ数字のとき
        res = int(n / 2)
        print(res)
        sys.exit()
    else:
        # 2パターン試す
        l_odd_1 = [l for l in l_odd if l[0] != l_odd[-2][0]]
        sum_odd_1 = sum(l[1] for l in l_odd_1)
        l_even_1 = [l for l in l_even if l[0] != top_even]
        sum_even_1 = sum(l[1] for l in l_even_1)
        sum_1 = sum_odd_1 + sum_even_1

        l_odd_2 = [l for l in l_odd if l[0] != top_odd]
        sum_odd_2 = sum(l[1] for l in l_odd_2)
        l_even_2 = [l for l in l_even if l[0] != l_even[-2][0]]
        sum_even_2 = sum(l[1] for l in l_even_2)
        sum_2 = sum_odd_2 + sum_even_2

        if sum_1 < sum_2:
            print(sum_1)
        else:
            print(sum_2)
        sys.exit()
else:
    l_odd = [l for l in l_odd if l[0] != top_odd]

sum_odd = sum(l[1] for l in l_odd)
#print(l_odd, sum_odd)

l_even = [l for l in l_even if l[0] != top_even]
sum_even = sum(l[1] for l in l_even)
#print(l_even, sum_even)

res = sum_odd + sum_even
print(res)

Submission status: Accepted

Solution code:
import heapq
n=int(input().strip())
nu=list(map(int, input().strip().split()))
nu1=[]
nu2=[]
nu1memo=[0]*100001
nu2memo=[0]*100001
for i in range(0,n//2):
    nu1.append(nu[2*i])
    nu2.append(nu[2*i+1])
    nu1memo[nu[2*i]]+=1
    nu2memo[nu[2*i+1]]+=1
    
#mi1=max(zip(nu1memo, range(len(nu1memo))))[1]
mi1=heapq.nlargest(2, range(len(nu1memo)), key=nu1memo.__getitem__)
#mi2=max(zip(nu2memo, range(len(nu2memo))))[1]
mi2=heapq.nlargest(2, range(len(nu2memo)), key=nu2memo.__getitem__)
ans=0
if mi1[0]!=mi2[0]:
    ans=n-nu1memo[mi1[0]]-nu2memo[mi2[0]]
elif mi1[0]==mi2[0]:
    ans=min(n-nu1memo[mi1[0]]-nu2memo[mi2[1]],n-nu1memo[mi1[1]]-nu2memo[mi2[0]])
    
print(ans)

Submission status: Accepted

Solution code:
import collections

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

b = a[::2]
c = a[1::2]

b_ =collections.Counter(b)
c_ =collections.Counter(c)

baa = b_.most_common()
caa = c_.most_common()

baa.append([0,0])
caa.append([0,0])

if baa[0][0] != caa[0][0]:
    print(N - baa[0][1] - caa[0][1])
else:
    print(min(N - baa[0][1] - caa[1][1],N - caa[0][1] - baa[1][1]))

Submission status: Accepted

Solution code:
n=int(input())
t=list(map(int,input().split()))
import collections
t_set=list(set(t))
half_n=n//2
even_list=[]
odd_list=[]
if len(t_set)==1:
    print(half_n)
    exit()
for i in range(0,n,2):
    even_list.append(t[i])
for i in range(1,n,2):
    odd_list.append(t[i])
even_list=collections.Counter(even_list)
odd_list=collections.Counter(odd_list)
if even_list.most_common()[0][0]!=odd_list.most_common()[0][0]:
    ans=(half_n-even_list.most_common()[0][1])+(half_n-odd_list.most_common()[0][1])
else:
    ans=(half_n-even_list.most_common()[0][1])+(half_n-odd_list.most_common()[1][1])
    ans_2=(half_n-even_list.most_common()[1][1])+(half_n-odd_list.most_common()[0][1])
    ans=min(ans,ans_2)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

even = [[int(x),0] for x in range(10**5+1)]

odd = [[int(x),0] for x in range(10**5+1)]

for i in range(len(c)):
    if i % 2 == 0:
        even[c[i]][1] += 1
    else:
        odd[c[i]][1] += 1

even = sorted(even,key = lambda x:-x[1])
odd = sorted(odd,key = lambda x:-x[1])

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

Submission status: Accepted

Solution code:
def count_char_occurrence(seq):
    # count how many times characters occur and return results as a dictionary
    ret = {}
    for char in seq:
        if char in ret:
            ret[char] += 1
        else:
            ret[char] = 1
    return ret


def accum_char_frequency(seq):
    """
    accumulate frequency of each number in a inputted sequence
    Example:
        in: [a,a,a,a,b,b,b,b,c]
        out: { 4 times: [a,b] , 1 times: [c] }
    """
    frequencies = count_char_occurrence(seq)

    ret = {}
    for char, count in frequencies.items():
        if count in ret:
            ret[count].append(char)
        else:
            ret[count] = [char]
    return ret


def main():
    _ = input()
    seq = list(map(int, input().split()))

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

    if len(set(seq)) == 1:  # when all chars are unique, return the length of even numbered chars
        print(len(even))
        return

    no_freq_odd = accum_char_frequency(odd)
    freq_odd = sorted(no_freq_odd.items(), reverse=True)
    
    no_freq_even = accum_char_frequency(even)
    freq_even = sorted(no_freq_even.items(), reverse=True)

    if len(freq_odd[0][1]) == 1 and len(freq_even[0][1]) == 1 and freq_odd[0][1] == freq_even[0][1]:
        min_change_even = len(seq)
        if len(freq_even) != 1:
            min_change_even = (len(odd) - freq_odd[0][0]) + (len(even) - freq_even[1][0])

        min_change_odd = len(seq)
        if len(freq_odd) != 1:
            min_change_odd = (len(odd) - freq_odd[1][0]) + (len(even) - freq_even[0][0])

        print(min(min_change_even, min_change_odd))
        return
    else:
        min_change = (len(odd) - freq_odd[0][0]) + (len(even) - freq_even[0][0])
        print(min_change)
        return

main()

Submission status: Accepted

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

a = [arr[i] for i in range(0,n,2)]
b = [arr[y] for y in range(1,n,2)]

import collections
acnt = collections.Counter(a)
bcnt = collections.Counter(b)

lsacnt = sorted(acnt.items(), key=lambda x: x[1],reverse=True)
lsbcnt = sorted(bcnt.items(), key=lambda j: j[1],reverse=True)

def val(abcnt, ablscnt):
	if len(list(abcnt)) < 2:
		return 0
	else:
		return ablscnt[1][1]

if lsacnt[0][0] != lsbcnt[0][0]:
	ans = n - lsacnt[0][1] - lsbcnt[0][1]
elif lsacnt[0][0] == lsbcnt[0][0]:
	if lsacnt[0][1] > lsbcnt[0][1]:
		ans = n - lsacnt[0][1] - val(bcnt,lsbcnt)
	elif lsacnt[0][1] < lsbcnt[0][1]:
		ans = n - val(acnt,lsacnt) - lsbcnt[0][1]
	elif lsacnt[0][1] == lsbcnt[0][1]:
		ans = n - lsacnt[0][1] - max(val(acnt,lsacnt),val(bcnt,lsbcnt))

print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(int(i) for i in input().split())

a = v[:n:2]
b = v[1:n:2]
c = collections.Counter(a)
d = collections.Counter(b)

if len(c) ==1 and len(d) == 1:
    if c.most_common()[0][0] != d.most_common()[0][0]:
        print(n-c.most_common()[0][1]-d.most_common()[0][1])
    if c.most_common()[0][0] == d.most_common()[0][0]:
        print(n//2)

if len(c) ==1 and len(d) !=1:
    if c.most_common()[0][0] != d.most_common()[0][0]:
        print(n-c.most_common()[0][1]-d.most_common()[0][1])
    if c.most_common()[0][0] == d.most_common()[0][0]:
        print(n-c.most_common()[0][1]-d.most_common()[1][1])

if len(d) ==1 and len(c) !=1:
    if c.most_common()[0][0] != d.most_common()[0][0]:
        print(n-c.most_common()[0][1]-d.most_common()[0][1])
    if c.most_common()[0][0] == d.most_common()[0][0]:
        print(n-c.most_common()[1][1]-d.most_common()[0][1])

if len(c) != 1 and len(d) != 1:
    if c.most_common()[0][0] != d.most_common()[0][0]:
        print(n-c.most_common()[0][1]-d.most_common()[0][1])
    if c.most_common()[0][0] == d.most_common()[0][0]:
        if (c.most_common()[0][1]+d.most_common()[1][1]) >= (c.most_common()[1][1]+d.most_common()[0][1]):
            print(n-c.most_common()[0][1]-d.most_common()[1][1])
        else:
            print(n-c.most_common()[1][1]-d.most_common()[0][1])

Submission status: Accepted

Solution code:
# coding:utf-8
# 找第二大的数的个数
n = input()
array = map(int, raw_input().split())
e_list = [array[i] for i in range(0, n, 2)]
o_list = [array[i] for i in range(1, n+1, 2)]


def process(arr):
    arr.sort()
    num = arr[0]
    counter = 0
    amt_arr = []
    arr.append(0)
    max = 0
    max_amt_num = []
    for i in arr:
        if i == num:
            counter += 1
        else:
            if counter > max:
                max_amt_num = [num]
                max = counter
            elif counter == max:
                max_amt_num.append(num)
                max = counter
            num = i
            amt_arr.append(counter)
            counter = 1
    if len(amt_arr) == 1:
        amt_arr.append(0)
    if len(max_amt_num) > 1:
        max_amt_num = "haha"
    amt_arr.sort()

    return amt_arr[-1], amt_arr[-2], max_amt_num
v1, v2, a1 = process(e_list)
v3, v4, a2 = process(o_list)
if a1 != a2 and a1 != "haha":
    result = len(array) - v1 - v3
else:
    result = len(array) - max(v1+v4, v2+v3)
print result

Submission status: Accepted

Solution code:
# coding:utf-8
# 找第二大的数的个数
n = input()
array = map(int, raw_input().split())
e_list = [array[i] for i in range(0, n, 2)]
o_list = [array[i] for i in range(1, n+1, 2)]


def process(arr):
    arr.sort()
    num = arr[0]
    counter = 0
    amt_arr = []
    arr.append(0)
    max = 0
    max_amt_num = []
    for i in arr:
        if i == num:
            counter += 1
        else:
            if counter > max:
                max_amt_num = [num]
                max = counter
            elif counter == max:
                max_amt_num.append(num)
                max = counter
            num = i
            amt_arr.append(counter)
            counter = 1
    if len(amt_arr) == 1:
        amt_arr.append(0)
    if len(max_amt_num) > 1:
        max_amt_num = "haha"
    amt_arr.sort()

    return amt_arr[-1], amt_arr[-2], max_amt_num
v1, v2, a1 = process(e_list)
v3, v4, a2 = process(o_list)
if a1 != a2 and a1 != "haha":
    result = len(array) - v1 - v3
else:
    result = len(array) - max(v1+v4, v2+v3)
print result

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
set_v=set(v)
if len(set_v)==1:
    print(n//2)
    exit()
a=Counter(v[::2]).most_common() #(数字,何回出現)が出現回数が大きい方から
b=Counter(v[1::2]).most_common()
if a[0][0]==b[0][0]:
    print(min(n-b[0][1]-a[1][1],n-a[0][1]-b[1][1]))
else:
    print(n-a[0][1]-b[0][1])

Submission status: Accepted

Solution code:
import collections
n=int(input())
half_n=n//2
t=list(map(int,input().split()))
even_lis=[]
odd_lis=[]
set_t=list(set(t))
if len(set_t)==1:
    print(n//2)
    exit()
for i in range(0,n,2):
    even_lis.append(t[i])
for i in range(1,n,2):
    odd_lis.append(t[i])
even_count=collections.Counter(even_lis)
odd_count=collections.Counter(odd_lis)
if even_count.most_common()[0][0]!=odd_count.most_common()[0][0]:
    ans=(half_n-even_count.most_common()[0][1])+(half_n-odd_count.most_common()[0][1])
else:
    ans=(half_n-even_count.most_common()[0][1])+(half_n-odd_count.most_common()[1][1])
    ans_2=(half_n-even_count.most_common()[1][1])+(half_n-odd_count.most_common()[0][1])
    ans=min(ans,ans_2)
print(ans)

Submission status: Accepted

Solution code:
# abc111_c_new

from collections import Counter


def solve(n, seq):
    odd = seq[::2]  # odd numbers of the seq
    even = seq[1::2]  # even numbers of the seq

    # print(odd)
    # print(even)

    # odd = Counter(odd).most_common()  # example -> returns [(1, 5), (2, 5)]
    # even = Counter(even).most_common()
    odd = sorted([(k, v) for k, v in Counter(odd).items()], key=lambda x: x[1], reverse=True)
    even = sorted([(k, v) for k, v in Counter(even).items()], key=lambda x: x[1], reverse=True)

    # print(odd)
    # print(even)

    sol = 0
    if odd[0][0] == even[0][0]:
        if len(odd) == 1 and len(even) == 1:  # seq [1, 1, 1, 1]  (special case)
            sol = int(n // 2)  # [1, 1, 1, 1] sol = N / 2
        else:
            if len(odd) >= 2 and len(even) >= 2:
                sol = min(n - odd[0][1] - even[1][1], n - odd[1][1] - even[0][1])
            else:
                if odd[0][1] > even[1][1]:
                    # sol = n - odd[0][1] - even[1][1] ==
                    sol = (n // 2 - odd[0][1]) + (n // 2 - even[1][1])
                else:
                    # sol = n - odd[1][1] - even[0][1] ==
                    sol = (n // 2 - odd[1][1]) + (n // 2 - even[0][1])
    else:
        # sol = n - odd[0][1] - even[0][1] ==
        sol = (n // 2 - odd[0][1]) + (n // 2 - even[0][1])

    return sol


if __name__ == "__main__":
    n = int(input())
    seq = list(map(int, input("").split(" ")))
    print(solve(n, seq))

Submission status: Accepted

Solution code:
n=int(input())
A=[int(x) for x in input().split()]

O=[[int(x),0] for x in range(10**5+1)]
E=[[int(x),0] for x in range(10**5+1)]

for i in range(n):
  if i%2==0:
    E[A[i]][1]+=1
  else:
    O[A[i]][1]+=1

O=sorted(O,key=lambda x:-x[1])
E=sorted(E,key=lambda x:-x[1])

if O[0][0]==E[0][0]:
  print(min(n-O[0][1]-E[1][1],n-O[1][1]-E[0][1]))
else:
  print(n-O[0][1]-E[0][1])

Submission status: Accepted

Solution code:
from collections import Counter

N=int(input())
n=list(map(int,input().split()))
#Divide it into even and odd
n1=[]
n2=[]
for i in range(N//2):
    n1.append(n[i*2])
    n2.append(n[i*2-1])
#end

#make lile {key,value}             tabunn kokoga ikenai
cm1=Counter(n1).most_common(2)
cm2=Counter(n2).most_common(2)
#end

#for example(4,1 1 1 1)
cm1.append([0,0])
cm2.append([0,0])
#end


#answer
if cm1[0][0] != cm2[0][0]:
    ans=N-cm1[0][1]-cm2[0][1]
elif cm1[0][1]-cm1[1][1] > cm2[0][1]-cm2[1][1]:#prefer large disadvantage  
    ans=N-cm1[0][1]-cm2[1][1]
else:
    ans=N-cm1[1][1]-cm2[0][1]
print(ans)
#end

Submission status: Accepted

Solution code:
N = int(input())
*V, = map(int, input().split())
def calc(V):
    A = {-1: 0}
    for c in V:
        A[c] = A.get(c, 0) + 1
    return sorted(A.items(), key=lambda x:(x[1],x[0]), reverse=1)
P = calc(V[::2])
Q = calc(V[1::2])
if P[0][0] == Q[0][0]:
    print(N-max(P[0][1]+Q[1][1], P[1][1]+Q[0][1]))
else:
    print(N-P[0][1]-Q[0][1])

Submission status: Accepted

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

#Binary tree
class Node:
    def __init__(self, x):
        self.data  = x
        self.left  = None
        self.right = None
        self.count = 1

class BST:
    def __init__(self, number_list): #コンストラクタ
        self.root = None #ルートノード初期化
        for node in number_list: #数値を持つ配列から二分木を生成
            self.insert(node) #挿入メソッドを使ってノードを挿入する

    def insert(self, data):
        n = self.root #nにルートを代入
        if n == None: #ルートに何も入っていない場合
            self.root = Node(data) #ルートにノードを代入(二分探索木の一番上の部分が出来る)
            return #呼び出し部分に戻る
        else: #ルートノードに既に数値が入ってる場合
            while True: #return等が入るまでずっと実行
                entry = n.data #entryにnが持つデータ(数値)を入れる
                if data < entry: #entryとdata(代入するデータ)を比較してentryの方が大きい場合
                    if n.left is None: #nの左エッジに何も入っていない場合
                        n.left = Node(data) #nの左エッジにdata(代入するデータ)を代入
                        return ##呼び出し部分に戻る
                    n = n.left #nの左に何か入っている場合nにnのleftを代入
                elif data > entry: #entryとdata(代入するデータ)を比較してentryの方が小さい場合
                    if n.right is None: #nの右エッジに何も入っていない場合
                        n.right = Node(data) #nの右エッジにdata(代入するデータ)を代入
                        return #呼び出し部分に戻る
                    n = n.right #nの右エッジに何か入っている場合nにnのrightを代入
                else: #上2つの条件分岐以外の場合
                    n.data = data #nのdataにdata(代入するデータ)を代入
                    n.count+=1
                    return #呼び出し部分に戻る


    def traverse(self,node,func,len):
        if node is not None:
            len.append([node.count,node.data])
            self.traverse(node.right,func,len)
            self.traverse(node.left,func,len)
        return len


def print_data(x): 
    print(x)

def append_data(l,x):
    l.append(x)
#end

#Divide it into even and odd
n1=[]
n2=[]
for i in range(N//2):
    n1.append(n[i*2])
    n2.append(n[i*2-1])
#end

#insert tree
tree1=BST(n1)
tree2=BST(n2)
#end

#make lile {key,value} 
cn1=[]
cn1=tree1.traverse(tree1.root,append_data,cn1)
cn2=[]
cn2=tree2.traverse(tree2.root,append_data,cn2)
#end

#for example(4,1 1 1 1)
cn1.append([0,0])
cn2.append([0,0])
#end

#sort
    #first input
sum1=0
sum2=0
if cn1[0][0]>=cn1[1][0]:
    max11=cn1[0][0]
    num11=cn1[0][1]

    max12=cn1[1][0]
    num12=cn1[1][1]
else:
    max11=cn1[1][0]
    num11=cn1[1][1]

    max12=cn1[0][0]
    num12=cn1[0][1]


if cn2[0][0]>=cn2[1][0]:
    max21=cn2[0][0]
    num21=cn2[0][1]

    max22=cn2[1][0]
    num22=cn2[1][1]
else:
    max21=cn2[1][0]
    num21=cn2[1][1]

    max22=cn2[0][0]
    num22=cn2[0][1]
    #end

for i in range(2,len(cn1)):
    if max12<cn1[i][0]:
        if max11<cn1[i][0]: #if new one is max
            sum1+=max12

            max12=max11
            num12=num11

            max11=cn1[i][0]
            num11=cn1[i][1]
        else:                #if new one is second max
            sum1+=max12

            max12=cn1[i][0]
            num12=cn1[i][1]
    else:
        sum1+=cn1[i][0]


for i in range(2,len(cn2)):
    if max22<cn2[i][0]:
        if max21<cn2[i][0]: #if new one is max
            sum2+=max22

            max22=max21
            num22=num21

            max21=cn2[i][0]
            num21=cn2[i][1]
        else:               #if new one is second max
            sum2+=max22

            max22=cn2[i][0]
            num22=cn2[i][1]
    else:
        sum2+=cn2[i][0]
#end

#answer
if num11 != num21:
    ans=sum1+sum2+max12+max22
elif max11-max12 > max21-max22:#prefer large disadvantage  
    ans=sum1+sum2+max12+max21
else:
    ans=sum1+sum2+max11+max22
print(ans)
#end

Submission status: Accepted

Solution code:
import math as M
n=int(input())
Numbers=list(map(int,input().split(' ')))
Evens={}
Odds={}
for i in range(n):
    if i%2==0:
        if Numbers[i] in Evens:
            Evens[Numbers[i]]=Evens[Numbers[i]]+1
        else:
            Evens[Numbers[i]]=1

    if i%2!=0:
        if Numbers[i] in Odds:
            Odds[Numbers[i]]=Odds[Numbers[i]]+1
        else:
            Odds[Numbers[i]]=1

Evens=sorted(Evens.items(),key=lambda x:x[1])
Odds=sorted(Odds.items(),key=lambda x:x[1])
Evens.reverse()
Odds.reverse()

if Evens[0][0]==Odds[0][0]:
    if len(Evens)==1:
        Evens.append([Evens[0][0]+1,0])
    if len(Odds) == 1:
        Odds.append([Odds[0][0] + 1, 0])
    print(min(n,n-Evens[0][1]-Odds[1][1],n - Evens[1][1] - Odds[0][1]))
else:
    print(n-Evens[0][1]-Odds[0][1])

Submission status: Accepted

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

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

from collections import Counter

so_ev = Counter(v_even).most_common(2)
so_od = Counter(v_odd).most_common(2)

if so_ev[0][0] == so_od[0][0]:
    if (len(so_ev) == 1) and (len(so_od) == 1):
        print(n - so_ev[0][1])
    else:
        print(min(n - so_ev[1][1] - so_od[0][1], n - so_ev[0][1] - so_od[1][1]))
else:
    print(n - so_ev[0][1] - so_od[0][1])

Submission status: Accepted

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

even = []
odd = []

for index, i in enumerate(li):
    if index % 2 == 0:
        even.append(i)
    elif index % 2 != 0:
        odd.append(i)

from collections import Counter
so_ev = Counter(even).most_common(2)
so_od = Counter(odd).most_common(2)

if so_ev[0][0] == so_od[0][0]:
    if len(so_ev) == 1 and len(so_od) == 1:
        print(n - so_ev[0][1])
    else:
        print(min(n - so_ev[1][1] - so_od[0][1], n - so_ev[0][1] - so_od[1][1]))
else:
    print(n - so_ev[0][1] - so_od[0][1])

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = list(map(int,input().split()))
even1 = v[0::2]
odd1 = v[1::2]
even2 = Counter(even1)
odd2 = Counter(odd1)
a = even2.most_common()[0][0]
b = odd2.most_common()[0][0]
c = even2.most_common()[0][1]
d = odd2.most_common()[0][1]
if a != b:
 print(len(even1)-c+len(odd1)-d)
else:
 if len(list(set(even1))) == 1 and len(list(set(odd1))) == 1:
  print(len(even1))
 elif len(list(set(even1))) == 1:
  print(len(odd1)-odd2.most_common()[1][1])
 elif len(list(set(odd1))) == 1:
  print(len(even1)-even2.most_common()[1][1])
 else:
  e = even2.most_common()[1][1]
  f = odd2.most_common()[1][1]
  if c-e >= d-f:
   print(len(even1)*2-c-f)
  else:
   print(len(even1)*2-d-e)

Submission status: Accepted

Solution code:
import numpy as np

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

m = int(n / 2)
p = max(v)

evens = np.array([0] * p)
odds = np.array([0] * p)

for i in range(0, m):
    evens[v[2 * i] - 1] += 1
    odds[v[2 * i + 1] - 1] += 1

if len(evens) == 1 and len(odds) == 1:
    if evens.argsort()[-1] == odds.argsort()[-1]:
        print(m)
    else:
        print(0)
elif len(evens) ==1:
    mode_e1 = evens.argsort()[-1]
    mode_o2, mode_o1 = odds.argsort()[-2:]
    if mode_e1 == mode_o1:
        print(m - odds[mode_o2])
    else:
        print(m - odds[mode_o1])
elif len(odds) == 1:
    mode_e2, mode_e1 = evens.argsort()[-2:]
    mode_o1 = odds.argsort()[-1]
    if mode_e1 == mode_o1:
        print(m - evens[mode_e2])
    else:
        print(m - evens[mode_e1])
else:
    mode_e2, mode_e1 = evens.argsort()[-2:]
    mode_o2, mode_o1 = odds.argsort()[-2:]

    if mode_e1 == mode_o1:
        print(min(n - evens[mode_e1] - odds[mode_o2], n - evens[mode_e2] - odds[mode_o1]))
    else:
        print(n - evens[mode_e1] - odds[mode_o1])

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
a=Counter(v[::2]).most_common() #(数字,何回出現)が出現回数が大きい方から
b=Counter(v[1::2]).most_common()
if len(a)==1:
    a.append([0,0])
if len(b)==1:
    b.append([0,0])
if a[0][0]==b[0][0]:
    print(min(n-a[1][1]-b[0][1],n-a[0][1]-b[1][1]))
else:
    print(n-a[0][1]-b[0][1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
import collections

n = int(input().strip())
vs = [int(s) for s in input().strip().split()]

e = [vs[i*2] for i in range(n//2)]
o = [vs[i*2+1] for i in range(n//2)]

e_counter = collections.Counter(e).most_common()
o_counter = collections.Counter(o).most_common()

e1, e1_count = e_counter[0]
try:
    e2, e2_count = e_counter[1]
except IndexError:
    e2_count = 0

o1, o1_count = o_counter[0]
try:
    o2, o2_count = o_counter[1]
except IndexError:
    o2_count = 0

if e1 != o1:
    keep_num_count = e1_count + o1_count
else:
    keep_num_count = e1_count + o2_count
    if keep_num_count < e2_count + o1_count:
        keep_num_count = e2_count + o1_count

print(n - keep_num_count)

Submission status: Accepted

Solution code:
_ = input()
seq = list(map(int,input().split()))
 
seq1 = seq[::2]
seq2 = seq[1::2]
 
if len(set(seq)) == 1:
    print(len(seq2))
    exit()
    
def count_dictonary(seq):
    ret_dict={}
    for item in seq:
        if item not in ret_dict:
            ret_dict[item] = 1
        else:
            ret_dict[item]+=1
    return ret_dict
 
def modes(counting_dict):
    #(11,2),(2,1),(3,3)
    ret={}
    for item in counting_dict.items():
        no, count = item
        if counting_dict[no] in ret:
            ret[count].append(no)
        else:
            ret[count]=[no]
            
    return ret
 
 
counting1 = count_dictonary(seq1)
counting2 = count_dictonary(seq2)
 
modes1 = sorted(modes(counting1).items(),reverse=True)
modes2 = sorted(modes(counting2).items(),reverse=True)
 
min_change=len(seq)
if modes1[0][1] != modes2[0][1] or len(modes1[0][1]) >=2 or len(modes2[0][1]) >=2:
    min_change = (len(seq1) - modes1[0][0]) + (len(seq2) - modes2[0][0])
else:
    m01=len(seq)
    m10=len(seq)
    if len(modes2) !=1:
        m01 =  (len(seq1) - modes1[0][0]) + (len(seq2) - modes2[1][0])
    if len(modes1) !=1:
        m10 =  (len(seq1) - modes1[1][0]) + (len(seq2) - modes2[0][0])
    min_change = min(m01,m10)
    
print(min_change)

Submission status: Accepted

Solution code:
# coding:utf-8
import collections


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

    va = []
    vb = []
    for i in range(0, n, 2):
        va.append(v[i])
        vb.append(v[i + 1])

    if va == vb and len(set(va)) == 1:
        print(len(va))
        return

    c1mc = collections.Counter(va).most_common()
    c2mc = collections.Counter(vb).most_common()

    if c1mc[0][0] != c2mc[0][0]:
        print(n - c1mc[0][1] - c2mc[0][1])
        return

    else:
        a = n - c1mc[1][1] - c2mc[0][1]
        b = n - c1mc[0][1] - c2mc[1][1]

        if a < b:
            print(a)
        else:
            print(b)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
V = list(map(int,input().split()))
V_odd = V[::2]
V_eve = V[1::2]
odd_max = Counter(V_odd).most_common(1)
even_max = Counter(V_eve).most_common(1)

result = n - odd_max[0][1] - even_max[0][1]
if odd_max[0][0] == even_max[0][0]:
    odd_least,even_least = 0,0
    if Counter(V_odd).most_common(1) != Counter(V_odd).most_common(2):
        odd_least = (Counter(V_odd).most_common(2))[1][1]
    if Counter(V_eve).most_common(1) != Counter(V_eve).most_common(2):
        even_least = (Counter(V_eve).most_common(2))[1][1]
    if Counter(V_odd).most_common(1) == Counter(V_odd).most_common(2) and Counter(V_eve).most_common(1) == Counter(V_eve).most_common(2):
        result = int(n/2)
    else:
        result = min(n - odd_least - even_max[0][1],n - even_least - odd_max[0][1])

print(result)