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())
v = [int(i) for i in input().split()]


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

odd2 = Counter(odd)
even2 = Counter(even)

if len(list(set(v))) == 1:
    print(n//2)

else:
    if odd2.most_common()[0][0] == even2.most_common()[0][0]:
        if odd2.most_common()[0][1] > even2.most_common()[0][1]:
            print(n - odd2.most_common()[0][1] - even2.most_common()[1][1])
        elif odd2.most_common()[0][1] < even2.most_common()[0][1]:
            print(n - odd2.most_common()[1][1] - even2.most_common()[0][1])
        else:
            if odd2.most_common()[1][1] > even2.most_common()[1][1]:
                print(n - odd2.most_common()[1][1] - even2.most_common()[0][1])
            else:
                print(n - odd2.most_common()[0][1] - even2.most_common()[1][1])
    else:
        print(n - odd2.most_common()[0][1] - even2.most_common()[0][1])

Submission status: Accepted

Solution code:
import collections as cole


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

v1 = v[::2]   #odd
v2 = v[1::2]  #even

d1 = cole.Counter(v1)
d2 = cole.Counter(v2)

values1, counts1 = zip(*d1.most_common())
values2, counts2 = zip(*d2.most_common())

max1 = values1[0]
max2 = values2[0]

if max1 == max2:
    if len(counts1) != 1 and counts1[0] == counts1[1]:
        max1 = values1[1]
    elif len(counts2) != 1 and counts2[0] == counts2[1]:
        max2 = values2[1]
    elif len(counts1) != 1 and len(counts2) != 1:
        if counts1[1] > counts2[1]:
            max1 = values1[1]
        else:
            max2 = values2[1]
    elif len(counts1) != 1:
        max1 = values1[1]
    elif len(counts2) != 1:
        max2 = values2[1]


count = 0
if max1 != max2:
    count += len([x for x in v1 if x != max1])
    count += len([y for y in v2 if y != max2])

else:
    count += len(v1)


print(count)

Submission status: Accepted

Solution code:
import collections

N = int(raw_input().replace("\n", ""))

a_list = map(int, raw_input().replace("\n", "").split(" "))
even_list = a_list[::2]
odd_list = a_list[1::2]

len_vec = N / 2

even_c = collections.Counter(even_list)
odd_c = collections.Counter(odd_list)

even_c_most_common = even_c.most_common()
odd_c_most_common = odd_c.most_common()

even_first_value, even_first_count = even_c_most_common[0]
odd_first_value, odd_first_count  = odd_c_most_common[0]
#(even_first_value, even_first_count), (even_second_value, even_second_count) = even_c.most_common()[:2]
#(odd_first_value, odd_first_count), (odd_second_value, odd_second_count) = odd_c.most_common()[:2]

if even_first_value == odd_first_value and len(even_c) == 1 and len(odd_c) == 1:
    cost = len_vec
elif even_first_value != odd_first_value:
    cost =  (even_first_count + odd_first_count)
elif len(even_c) == 1 and len(odd_c) > 1:
    odd_second_value, odd_second_count = odd_c_most_common[1]
    cost = len_vec + odd_second_count
elif len(odd_c) == 1 and len(even_c) > 1:
    even_second_value, even_second_count = even_c_most_common[1]
    #cost = len_vec + odd_second_count
    cost = len_vec + even_second_count
else: # ryouhou no first ga onaji de hutatuijou arutoki
    odd_second_value, odd_second_count = odd_c_most_common[1]
    even_second_value, even_second_count = even_c_most_common[1]
    cost = max(even_first_count+odd_second_count, even_second_count+odd_first_count)

print(2* len_vec - cost)

Submission status: Accepted

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

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

sl1 = sl[0::2]
sl2 = sl[1::2]
c1 = collections.Counter(sl1)
c2 = collections.Counter(sl2)
mc1k = c1.most_common()[0][0]
mc1v = c1.most_common()[0][1]
mc2k = c2.most_common()[0][0]
mc2v = c2.most_common()[0][1]

if mc1k == mc2k:
    mc1ks = len(c1.most_common())
    mc2ks = len(c2.most_common())
    if mc1ks > 1 and mc2ks > 1:
        mc1_2v = c1.most_common()[1][1]
        mc2_2v = c2.most_common()[1][1]
        if mc1_2v > mc2_2v:
            mc1v = mc1_2v
        else:
            mc2v = mc2_2v
    elif mc1ks > 1:
        mc1_2v = c1.most_common()[1][1]
        mc1v = mc1_2v
    elif mc2ks > 1:
        mc2_2v = c2.most_common()[1][1]
        mc2v = mc2_2v
    else:
        mc2v = 0
print(n - mc1v - mc2v)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
v=list(map(int, input().split()))
v1=[]
v2=[]
for i in range(0,n-1,2):
    v1.append(v[i])
    v2.append(v[i+1])

v1C=list(sorted(list(Counter(v1).items()), key=lambda x: x[1]))
v2C=list(sorted(list(Counter(v2).items()), key=lambda x: x[1]))

a=v1C[-1][1]
b=v2C[-1][1]
if v1C[-1][0]==v2C[-1][0]:

    if len(v1C)>1 and len(v2C)>1:
        if (len(v1)-v1C[len(v1C)-2][1]+len(v2)-b)>(len(v1)-a+len(v2)-v2C[len(v2C)-2][1]):
            b=v2C[len(v2C)-2][1]
        else:
            a=v1C[len(v1C)-2][1]
    elif a>=b and (len(v1C)>1 or len(v2C)>1):
        b=v2C[len(v2C)-2][1]
    elif a<b and (len(v1C)>1 or len(v2C)>1):
        a=v1C[len(v1C)-2][1]
    else:
        b=0

print(len(v1)-a+len(v2)-b)

Submission status: Accepted

Solution code:
import collections
n = int(raw_input())
a = map(int,raw_input().split())
a1 = []
a2 = []
for i in range(1,n+1,2):
  a1.append(a[i-1])
for i in range(2,n+2,2):
  a2.append(a[i-1])
c = collections.Counter(a1)
d = collections.Counter(a2)
b1 = c.most_common()
b2 = d.most_common()
x = b1[0]
x1 = x[1]
y = b2[0]
y1 = y[1]
if x1==n/2 and y1==n/2 and x[0]==y[0]:
  print n/2
elif x1==n/2 and x[0]==y[0]:
  w = b2[1]
  w1 = w[1]
  print n/2-w1
elif x1==n/2:
  print n/2-y1
elif y1==n/2 and x[0]==y[0]:
  z = b1[1]
  z1 = z[1]
  print n/2-z1
elif y1==n/2:
  print n/2-x1
else:
  z = b1[1]
  z1 = z[1]
  w = b2[1]
  w1 = w[1]
  if x[0]==y[0]:
    if x1+w1 >= y1+z1:
      print n-x1-w1
    else:
      print n-y1-z1
  else:
    print n-x1-y1

Submission status: Accepted

Solution code:
n = int(input())
l = [int(_) for _ in input().split()]

dict_e = {}
for i in range(1, n, 2):
    if l[i] in dict_e.keys():
        dict_e[l[i]] += 1
    else:
        dict_e[l[i]] = 1

even_key, even_value = max(dict_e.items(),key = lambda x:x[1])

dict_a = {}
for i in range(0, n, 2):
    if l[i] in dict_a.keys():
        dict_a[l[i]] += 1
    else:
        dict_a[l[i]] = 1

add_key, add_value = max(dict_a.items(),key = lambda x:x[1])

if even_key != add_key:
    ans = n - even_value - add_value
else:
    if even_value > add_value:
        if len(dict_a) >= 2:
            add_value = sorted(dict_a.items(), key = lambda x:x[1])[-2][1]
        else:
            add_value = 0
        ans = n - even_value - add_value
    elif even_value == add_value:
        if len(dict_e) == 1:
            even_value = 0
            ans = n - add_value - even_value
        else:
            even_value = sorted(dict_e.items(), key = lambda x:x[1])[-2][1]
            add_value = sorted(dict_a.items(), key = lambda x:x[1])[-2][1]
            if even_value > add_value:
                add_value = max(dict_a.items(),key = lambda x:x[1])[1]
                ans = n - even_value - add_value
            else:
                even_value = max(dict_e.items(),key = lambda x:x[1])[1]
                ans = n - even_value - add_value
    else:
        if len(dict_e) >= 2:
            even_value = sorted(dict_e.items(), key = lambda x:x[1])[-2][1]
        else:
            even_value = 0
        ans = n - even_value - add_value

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
c1 = Counter(A[::2])
c2 = Counter(A[1::2])
c1m = max(c1.items(), key=lambda x:x[1])
c2m = max(c2.items(), key=lambda x:x[1])
if len(set(A)) == 1:
    print(N // 2)
    exit()
c1c2 = max(c for k,c in c2.items() if c1m[0]!=k)
c2c1 = max(c for k,c in c1.items() if c2m[0]!=k)
print(min(N - c1m[1] - c1c2, N - c2m[1] - c2c1))

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int, input().split()))
vo = [v[i] for i in range(n) if i%2 == 0]
ve = [v[i] for i in range(n) if i%2 == 1]
vo_dict = collections.Counter(vo)
ve_dict = collections.Counter(ve)
if vo_dict == ve_dict and len(vo_dict) == 1:
    ans = n//2
else:
    if vo_dict.most_common()[0][0] != ve_dict.most_common()[0][0]:
        ans = n - vo_dict.most_common()[0][1] - ve_dict.most_common()[0][1]
    else:
        ans = min(n - vo_dict.most_common()[0][1] - ve_dict.most_common(2)[1][1],
                  n - vo_dict.most_common(2)[1][1] - ve_dict.most_common()[0][1])
print(ans)

Submission status: Accepted

Solution code:

import collections

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


def map_all(li):
    return all([e == li[0] for e in li[1:]]) if li else False


evel = collections.Counter()
evel2 = []
odd = collections.Counter()
odd2 = []
for index, v in enumerate(V):
    if index % 2 == 0:
        evel[v] += 1
        evel2.append(v)
    else:
        odd[v] += 1
        odd2.append(v)

if map_all(evel2) and map_all(odd2):
    # 各2数列全て同じ値
    if evel2[0] == odd2[0]:
        # 2つの数列全て同じ値
        print(int(n / 2))
    else:
        print(0)
else:
    el = evel.most_common(100)
    ol = odd.most_common(100)
    max_val = 0
    for index_e, num_e in el:
        for index_o, num_o in ol:
            if index_e != index_o:
                max_val = max(max_val, num_e + num_o)
    ans = n - max_val
    print(ans)

Submission status: Accepted

Solution code:
import copy
n=int(input())
V=list(map(int, input().split(" ")))
d1=[0 for i in range(100000)]
for i in V[::2]:
    d1[i-1]+=1
d2=[0 for i in range(100000)]
for i in V[1::2]:
    d2[i-1]+=1

m1=max(d1)
m2=max(d2)
maxind1 = [i for i, x in enumerate(d1) if x == m1]
maxind2 = [i for i, x in enumerate(d2) if x == m2]

#in1=d1.index(max(d1))
#in2=d2.index(max(d2))



if (maxind1[0] == maxind2[0]) and (len(maxind1) == 1) and (len(maxind2) == 1):
    #if m1>m2:
    #d2[maxind2[0]]=0
    tmp=copy.copy(d2)
    tmp[maxind2[0]]=0
    result1=n-max(d1)-max(tmp)

    #else:
    #d1[maxind1[0]]=0
    tmp=copy.copy(d1)
    tmp[maxind1[0]]=0
    result2=n-max(tmp)-max(d2)
    print(int(min(result1,result2)))
else:
#print(d1[:5])
#print(d2[:5])
    tmp1=n/2-max(d1)
    tmp2=n/2-max(d2)
    print(int(tmp1+tmp2))

Submission status: Accepted

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

min_num = 0

even = {}
odd = {}

for i, v in enumerate(v_list):
    if i % 2 == 0:
        if v in even:
            even[v] += 1
        else:
            even[v] = 1
    else:
        if v in odd:
            odd[v] += 1
        else:
            odd[v] = 1

even_max = sorted(even.items(), key=lambda x: -x[1])
odd_max = sorted(odd.items(), key=lambda x: -x[1])

even_num = even_max[0][0]
odd_num = odd_max[0][0]

if odd_num == even_num:
    if len(odd_max) == 1 and len(even_max) == 1:
        odd_num = 10 ** 5 + 1
    elif len(odd_max) == 1:
        even_num = even_max[1][0]
    elif len(even_max) == 1:
        odd_num = odd_max[1][0]
    else:
        first_e = even_max[0][1]
        first_d = odd_max[0][1]
        if first_e > first_d:
            odd_num = odd_max[1][0]
        elif first_e < first_d:
            even_num = even_max[1][0]
        elif first_e == first_d:
            second_e = even_max[1][1]
            second_d = odd_max[1][1]
            if second_e >= second_d:
                even_num = even_max[1][0]
            else:
                odd_num = odd_max[1][0]

for i, v in enumerate(v_list):
    if i % 2 == 0:
        if v != even_num:
            min_num += 1
    else:
        if v != odd_num:
            min_num += 1
print(min_num)

Submission status: Accepted

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

if len(set(v)) == 1:
    print(N//2)
else:
    v_gu = v[::2]
    v_ki = v[1::2]
    gu_con = collections.Counter(v_gu)
    ki_con = collections.Counter(v_ki)
    gugu = gu_con.most_common()
    kiki = ki_con.most_common()
    
    if gugu[0][0] != kiki[0][0]:
        print(N-gugu[0][1]-kiki[0][1])
    
    elif len(gugu) == 1 and len(kiki) >=2:
        print(N-gugu[0][1]-kiki[1][1])
        
    elif len(gugu) >= 2 and len(kiki) == 1:
        print(N-gugu[1][1]-kiki[0][1])
    else:
        print(min(N-gugu[0][1]-kiki[1][1],N-gugu[1][1]-kiki[0][1]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 29 16:33:51 2018
ABC111C
@author: maezawa
"""

import collections

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

v0 = [v[i] for i in range(0,n,2)]
v1 = [v[i] for i in range(1,n,2)]

v0cnt = collections.Counter(v0)
v1cnt = collections.Counter(v1)

v0most = v0cnt.most_common()[0]
v1most = v1cnt.most_common()[0]
if len(v0cnt) > 1:
    v0next = v0cnt.most_common()[1]
if len(v1cnt) > 1:
    v1next = v1cnt.most_common()[1]
if v0most[0] != v1most[0]:
    ans = n - v0most[1] -v1most[1]
else:
    if v0most[1] == v1most[1]:
        if v0most[1] == n//2:
            ans = n//2
        else:
            if v0next[1] > v1next[1]:
                ans = n - v1most[1] - v0next[1]
            else:
                ans = n - v0most[1] - v1next[1]            
    elif v0most[1] > v1most[1]:
        ans = n - v0most[1] - v1next[1]
    else:
        ans = n - v1most[1] - v0next[1]

print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
nums = list(map(int,input().split()))
a = []
b = []
for i in range(n):
    if i%2 == 0:
        b.append(nums[i])
    else:
        a.append(nums[i])
acol = collections.Counter(a)
bcol = collections.Counter(b)
amost = acol.most_common()[0][1]
bmost = bcol.most_common()[0][1]
if acol.most_common()[0][0] != bcol.most_common()[0][0]:    
    f = amost + bmost
    print(n-f)
elif len(acol.most_common()) != 1 and len(bcol.most_common()) != 1:
    if acol.most_common()[0][1] + bcol.most_common()[1][1] > acol.most_common()[1][1] + bcol.most_common()[0][1]:
        print(n-(acol.most_common()[0][1] + bcol.most_common()[1][1]))
    else:
        print(n-(acol.most_common()[1][1] + bcol.most_common()[0][1]))
elif len(acol.most_common()) == 1 and len(bcol.most_common()) != 1:
    print(n-(acol.most_common()[0][1]+bcol.most_common()[1][1]))
elif len(bcol.most_common()) == 1 and len(acol.most_common()) != 1:
    print(n-(acol.most_common()[1][1]+bcol.most_common()[0][1]))
else:
    print(n//2)

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int, input().split()))
v_odd = v[::2]
v_even = v[1::2]
v_odd = collections.Counter(v_odd)
v_even = collections.Counter(v_even)
v_odd = v_odd.most_common()
v_even = v_even.most_common()
if v_odd[0][0] == v_even[0][0]:
  if len(v_odd) == len(v_even) == 1:
    print(int(n/2))
  else:
    print(min(n - v_odd[1][1] -v_even[0][1],n - v_odd[0][1] -v_even[1][1]))
else:
  print(n - v_odd[0][1] -v_even[0][1])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = n
b = {}
c = {}
for i in range(0,len(a)):
    if i%2 == 0:
        b[a[i]] = b.get(a[i],0) + 1
    else:
        c[a[i]] = c.get(a[i],0) + 1
b = sorted(b.items(), key=lambda x: x[1])
c = sorted(c.items(), key=lambda x: x[1])

bk,bv = b[-1]
ck,cv = c[-1]
if ck != bk:
    ans = n-cv-bv
else:
    if bv > cv:
        ans -= bv
        cv = 0
        if len(c) > 1:
            ck,cv= c[-2]
        ans -= cv
    if bv < cv:
        ans -= cv
        bv = 0
        if len(b) > 1:
            bk,bv= b[-2]
        ans -= bv
    if bv == cv:
        ans -= cv
        if len(b) > 1 and len(c) > 1:
            bk,bv = b[-2]
            ck,cv = c[-2]
            ans -= max(bv,cv)
        else:
            if len(b) > 1:
                bk,bv = b[-2]
                ans -= bv
            elif len(c) > 1:
                ck,cv = c[-2]
                ans -= cv
print(ans)






#

Submission status: Accepted

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

res = ''

ht1 = dict()
ht2 = dict()

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


for j in range(1, n, 2):
    if v[j] not in ht2:
        ht2[v[j]] = 1
    else:
        ht2[v[j]] += 1

sorted1 = sorted(ht1.items(), key=lambda x: x[1])
sorted2 = sorted(ht2.items(), key=lambda x: x[1])

if (sorted1[-1][0] != sorted2[-1][0]):
    res = n - sorted1[-1][1] - sorted2[-1][1]
else:
    if len(sorted1) == 1 and len(sorted2) == 1:
        res = int(n / 2)
    elif len(sorted1) == 1:
        print('aaa')
        res = n - sorted2[-1][1]
    elif len(sorted2) == 1:
        print('bbb')
        res = n - sorted1[-1][1]
    elif sorted1[-2][1] < sorted2[-2][1]:
        res = n - sorted1[-1][1] - sorted2[-2][1]
    else:
        res = n - sorted2[-1][1] - sorted1[-2][1]

print(res)

Submission status: Accepted

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

if max(v)==min(v):
	print(int(n/2))
else:
	dic1 = {}
	dic2 = {}
	
	for i in range(n):
		if i % 2 == 1:
			if str(v[i]) in dic1.keys():
				dic1[str(v[i])] = dic1[str(v[i])] + 1
			else:
				dic1[str(v[i])] = 1
	
		if i % 2 == 0:
			if str(v[i]) in dic2.keys():
				dic2[str(v[i])] = dic2[str(v[i])] + 1
			else:
				dic2[str(v[i])] = 1
	
	max1 = max(dic1, key=dic1.get)
	max2 = max(dic2, key=dic2.get)
	p1 = dic1[max1]
	p2 = dic2[max2]
	if max1 == max2:
		dic1.pop(max1)
		dic2.pop(max2)
		if dic1 == {}:
			p3 = 0
		else:
			max3 = max(dic1, key=dic1.get)
			p3 = dic1[max3]
		if dic2 == {}:
			p4 = 0
		else:
			max4 = max(dic2, key=dic2.get)
			p4 = dic2[max4]
		if p1 + p4 > p2 + p3:
			p2 = p4
		else:
			p1 = p3
	
	print(n-p1-p2)

Submission status: Accepted

Solution code:
import collections

def get_max_value(dict1, dict2):
    max_dict1=max(dict1, key=dict1.get)
    max_dict2=max(dict2, key=dict2.get)
    return max_dict1, max_dict2

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

if len(collections.Counter(a))==1:
    print(int(n/2))
else:
    a_even=[a[i] for i in range(0,n,2)]
    a_odd=[a[i]for i in range(1,n,2)]

    a_even_c=collections.Counter(a_even)
    a_odd_c=collections.Counter(a_odd)

    while(True):
        max_a_even, max_a_odd=get_max_value(a_even_c, a_odd_c)
        if max_a_even!=max_a_odd:
            replace_count=n-a_even_c[max_a_even]-a_odd_c[max_a_odd]
            break
        else:
            if a_even_c[max_a_even]>a_odd_c[max_a_odd]:
                del a_odd_c[max_a_odd]
            elif a_even_c[max_a_even]==a_odd_c[max_a_odd]:
                temp=a_even_c[max_a_even]
                del a_odd_c[max_a_odd]
                del a_even_c[max_a_even]
                even_2, odd_2=get_max_value(a_even_c, a_odd_c)
                if a_even_c[even_2]>=a_odd_c[odd_2]:
                    a_odd_c[max_a_odd]=temp
                else:
                    a_even_c[max_a_even]=temp
            else:
                del a_even_c[max_a_even]

    print(replace_count)

Submission status: Accepted

Solution code:
import collections
n=int(input())
v=[int(i) for i in input().split()]
p=v
u=v[0::2]
q=p[1::2]
U= collections.Counter(u)
Q= collections.Counter(q)
a=n/2-U.most_common()[0][1]
b=n/2-Q.most_common()[0][1]
if(a==0)and(b==0)and(U.most_common()[0][0]==Q.most_common()[0][0]):
  print(int(n/2))
else:
  if(U.most_common()[0][0]==Q.most_common()[0][0]):
    if(U.most_common()[1][1]<Q.most_common()[1][1]):
      b=n/2-Q.most_common()[1][1]
    else:
      a=n/2-U.most_common()[1][1]
  print(int(a+b))

Submission status: Accepted

Solution code:
from collections import Counter
from collections import deque

if __name__ == '__main__':
    n = int(input())
    vs0 = []
    vs1 = []
    for i,v in enumerate(map(int, input().split())):
        if i % 2 == 0:
            vs0.append(v)
        else:
            vs1.append(v)
    vs0 = deque(sorted(Counter(vs0).items(), key=lambda t: t[1]))
    vs1 = deque(sorted(Counter(vs1).items(), key=lambda t: t[1]))
    t0 = vs0.pop()
    t1 = vs1.pop()
    if t0[0] != t1[0]:
        vs0 = (t[1] for t in vs0)
        vs1 = (t[1] for t in vs1)
        print(sum(vs0) + sum(vs1))
    else:
        if len(vs1) == 0:
            if len(vs0) == 0:
                print(t1[1])
            else:
                vs0 = (t[1] for t in vs0)
                print(sum(vs0))
        elif len(vs0) == 0:
            vs1 = (t[1] for t in vs1)
            print(sum(vs1))
        else:
            t00 = vs0.pop()
            t11 = vs1.pop()
            a = t00[1] + t1[1]
            b = t11[1] + t0[1]
            vs0 = (t[1] for t in vs0)
            vs1 = (t[1] for t in vs1)
            print(sum(vs0) + sum(vs1) + (a if a < b else b))

Submission status: Accepted

Solution code:
n = int(input())

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

a = {}
b = {}
for i in range(n):
	if i % 2 == 1:
		if v[i] in a:
			a[v[i]] += 1
		else:
			a[v[i]] = 1
	else:
		if v[i] in b:
			b[v[i]] += 1
		else:
			b[v[i]] = 1
a[0] = 0
b[0] = 0
a = sorted(a.items(), key=lambda x:x[1])
b = sorted(b.items(), key=lambda x:x[1])
a.reverse()
b.reverse()
c = a[0]
d = b[0]
e = a[1]
f = b[1]
if c[0] == d[0]:
    g = max(c[1]+f[1],d[1]+e[1])
    print(n-g)
else:
    print(n-c[1]-d[1])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
vn = list(map(int,input().split()))
v0 = [0]*(n//2)
v1 = [0]*(n//2)
for i in range(n//2):
  v0[i] = vn[i*2+0]
  v1[i] = vn[i*2+1]
v0 = Counter(v0)
v1 = Counter(v1)
v0c0 = v0.most_common()[0]
v1c0 = v1.most_common()[0]
v0c1 = [0,0] if (len(v0.most_common()) == 1) else v0.most_common()[1]
v1c1 = [0,0] if (len(v1.most_common()) == 1) else v1.most_common()[1]

if v0c0[0] != v1c0[0]:
  print(n-v0c0[1]-v1c0[1])
elif v0c0[1] < v1c0[1]:
  print(n-v0c1[1]-v1c0[1])
elif v0c0[1] > v1c0[1]:
  print(n-v0c0[1]-v1c1[1])
elif v0c1[1] < v1c1[1]:
  print(n-v0c0[1]-v1c1[1])
elif v0c1[1] > v1c1[1]:
  print(n-v0c1[1]-v1c0[1])
else:
  print(n-v0c0[1]-v1c1[1])

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
d1={}
d2={}
r=len(a)
sum=0
maxs=0
maxs2=0
d1[maxs]=d2[maxs2]=0
for i in range(r):
    if i%2:
        if a[i] in d1:
            d1[a[i]]+=1
            if d1[a[i]]>d1[maxs]:
                maxs=a[i]
        else:
            d1[a[i]]=1
            sum+=1
            if d1[a[i]]>d1[maxs]:
                maxs=a[i]
    else:
        if a[i] in d2:
            d2[a[i]]+=1
            if d2[a[i]]>d2[maxs2]:
                maxs2=a[i]
        else:
            d2[a[i]]=1
            sum+=1
            if d2[a[i]]>d2[maxs2]:
                maxs2=a[i]
if maxs!=maxs2:
    print(n-d1[maxs]-d2[maxs2])
else:
    mmmm=0
    for i in d2:
        if i!=maxs2 and d2[i]>mmmm:
            mmmm=d2[i]
    mmm=0
    for i in d1:
        if i!=maxs and d1[i]>mmm:
            mmm=d1[i]
    print(min(n - d1[maxs] - mmmm,n-mmm-d2[maxs2]))

Submission status: Accepted

Solution code:
import collections
def solve():
    _ = input()
    v = list(map(int, input().split()))
    if v.count(v[0]) == len(v):
        print(len(v) // 2)
        return
    even = v[0::2]
    odd = v[1::2]
    even_cnt = list(sorted(collections.Counter(even).items(), key=lambda x: x[1], reverse=True))
    odd_cnt = list(sorted(collections.Counter(odd).items(), key=lambda x: x[1], reverse=True))
    ev = even_cnt[0][0]
    od = odd_cnt[0][0]
    if even_cnt[0] == odd_cnt[0]:
        if len(even_cnt) == 1:
            ev = even_cnt[1][0]
        elif len(odd_cnt) == 1:
            od = odd_cnt[1][0]
        elif even_cnt[1][1] > odd_cnt[1][1]:
            ev = even_cnt[1][0]
        else:
            od = odd_cnt[1][0]
    print(len(even) - even.count(ev) + len(odd) - odd.count(od))

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
b=a[0::2]
c=a[1::2]
x=Counter(b)
y=Counter(c)
x1=list(x.most_common(1)[0])
y1=list(y.most_common(1)[0])

if x1[0]==y1[0]:
  if x1[1]==len(b):
    if y1[1]==len(c):
      print(len(c))
    else:
      y2=list(y.most_common(2)[1])
      print(len(c)-y2[1])
  elif y1[1]==len(c):
    x2=list(x.most_common(2)[1])
    print(len(b)-x2[1])
  
  else:
    x2=list(x.most_common(2)[1])
    y2=list(y.most_common(2)[1])   
    if x1[1]+y2[1]>=x2[1]+y1[1]:
      print(len(a)-x1[1]-y2[1])
    else:
      print(len(a)-y1[1]-x2[1])
  
else:
  xc=len(b)-x1[1]
  yc=len(c)-y1[1]
  print(xc+yc)

Submission status: Accepted

Solution code:
#coding:utf-8
import numpy as np
import collections
N = 1000000
_ = int(input())
ls = list(map(int,input().split()))
ls.append(N)
ls.append(N)
array = np.array(ls).reshape(-1,2).T
c_even = collections.Counter(array[0])
c_odd = collections.Counter(array[1])
if len(set(ls))==2:
    print (int((len(ls)-2)/2))
elif c_even.most_common()[0][0] != c_odd.most_common()[0][0]:
    print (len(ls)-c_even.most_common()[0][1]-c_odd.most_common()[0][1]-2)
else:
    print (min(len(ls)-c_even.most_common()[0][1]-c_odd.most_common()[1][1]-2,len(ls)-c_even.most_common()[1][1]-c_odd.most_common()[0][1]-2))

Submission status: Accepted

Solution code:
import sys
from collections import OrderedDict
n = int(input())
v = list(map(int, input().split()))
if len(list(set(v))) == 1:
    print(n // 2)
    sys.exit()
e = {}
for i in v[::2]:
    if i in e:
        e[i] += 1
    else:
        e[i] = 1
o = {}
for i in v[1::2]:
    if i in o:
        o[i] += 1
    else:
        o[i] = 1
e = OrderedDict(sorted(e.items(), key = lambda x:-x[1]))
o = OrderedDict(sorted(o.items(), key = lambda x:-x[1]))
ans = 0
if not list(e.keys())[0] == list(o.keys())[0]:
    ans += (n // 2 - max(list(e.values())))
    ans += (n // 2 - max(list(o.values())))
else:
    ans1 = 0
    ans2 = 0
    if not len(list(set(list(o.keys())))) == 1:
        ans1 += (n // 2 - list(e.values())[0])
        ans1 += (n // 2 - list(o.values())[1])
    else:
        ans1 = 10 ** 10
    if not len(list(set(list(e.keys())))) == 1:
        ans2 += (n // 2 - list(o.values())[0])
        ans2 += (n // 2 - list(e.values())[1])
    else:
        ans2 = 10 ** 10
    ans = min(ans1, ans2)
print(ans)

Submission status: Accepted

Solution code:
import collections

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

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

A = collections.Counter(a).most_common()
B = collections.Counter(b).most_common()

sa, na = A[0]
sb, nb = B[0]
if sa == sb:
    if na == n // 2 and nb == n // 2:
        print(na)
    else:
        if len(A) == 1:
            _, nb = B[1]
            print(n - na - nb)
        elif len(B) == 1:
            _, na = A[1]
            print(n - na - nb)
        else:
            _, nna = A[1]
            _, nnb = B[1]
            if na + nnb < nb + nna:
                print(n - nb - nna)
            else:
                print(n - na - nnb)
else:
    print(n - na - nb)

Submission status: Accepted

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

# 全部同じ数字の時
if len(set(list(A))) == 1:
    print(n // 2)
    sys.exit(0)
O = A[0::2]
E = A[1::2]
mco = Counter(O).most_common()
mce = Counter(E).most_common()

l = len(O) # == len(E)
io = 0
io_max = len(mco) - 1
ie = 0
ie_max = len(mce) - 1
while(mco[io][0] == mce[ie][0]):
    if io_max <= io + 1:
        ie += 1
        continue
    if ie_max <= ie + 1:
        io += 1
        continue
    v1 = mco[io][1] + mce[ie+1][1]
    v2 = mco[io+1][1] + mce[ie][1]
    if v1 >= v2:
        ie += 1
    else:
        io += 1

print(l*2-mco[io][1]-mce[ie][1])

Submission status: Accepted

Solution code:
n = int(input())

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

v1 = [v for i,v in enumerate(vl) if i%2 ==0]
v2 = [v for i,v in enumerate(vl) if i%2 ==1]

table1 = {}
table2 = {}

for x in v1:
    table1.setdefault(x,0)
    table1[x] += 1

for x in v2:
    table2.setdefault(x,0)
    table2[x] += 1

v1 = [(k,table1[k]) for k in table1]
v2 = [(k,table2[k]) for k in table2]

v1 = sorted(v1,key= lambda v: v[1],reverse=True)
v2 = sorted(v2,key= lambda v: v[1],reverse=True)

size= int(n/2)
ans=0
if v1[0][0] != v2[0][0]:
    ans += size - v1[0][1]
    ans += size - v2[0][1]
else:
    if len(v1) == 1 and len(v2) == 1:
        print(size)
        exit()
    elif len(v1) == 1 and len(v2) != 1:
        print(size - v2[1][1])
        exit()
    elif len(v1) != 1 and len(v2) == 1:
        print(size - v1[1][1])
        exit()   


    if v1[1][1] == v1[0][1] or v2[1][1] == v2[0][1]:
        ans += size - v1[0][1]
        ans += size - v2[0][1]
    elif v1[1][1] < v2[1][1]:
        ans += size - v1[0][1]
        ans += size - v2[1][1]
    else:
        ans += size - v1[1][1]
        ans += size - v2[0][1]

print(ans)

Submission status: Accepted

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

d = defaultdict(int)
rd = defaultdict(int)
d[0] = 0
rd[0] = 0

ll = (n) // 2
rr = (n) // 2

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

d = list(reversed(sorted(d.items(), key=lambda x: x[1])))
rd= list(reversed(sorted(rd.items(), key=lambda x: x[1])))

i, j = 0, 0

while d[i][0] == rd[j][0] and i < len(d)-1 and j < len(rd)-1:
    if (ll - d[i][1] + rr - rd[j+1][1] < ll - d[i+1][1] + rr - rd[j][1]):
        j += 1
    else:
        i += 1


print(ll - d[i][1] + rr - rd[j][1])