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:
def c_zigzag(N, V):
    from collections import Counter
    count_seq_even = Counter(V[::2])  # 数列の(0-indexedで)偶数番目の要素の出現頻度
    count_seq_odd = Counter(V[1::2])  # 出現頻度(奇数番目)

    count_seq_even_s = sorted(count_seq_even.items(), key=lambda x: x[1], reverse=True)
    count_seq_odd_s = sorted(count_seq_odd.items(), key=lambda x: x[1], reverse=True)

    # 偶数番目からなる数列と奇数番目からなる数列のそれぞれについて、
    # 基本的には最も出現回数の多い要素に書き換えればいい。
    # ただし、それらの要素が一致する場合は書き換えられないので、
    # 2番目に出現回数の多い要素も(あれば)調べておく。(それ以下は見る必要はない)
    seq_even_select = [count_seq_even_s[k][0] for k in range(min(2, len(count_seq_even)))]
    seq_odd_select = [count_seq_odd_s[k][0] for k in range(min(2, len(count_seq_odd)))]

    ans = float('inf')
    # 両方の数列についてそれぞれ1種の要素しかなく、かつそれらが一致する
    # (全要素が等しい値)場合は、奇数番目か偶数番目かどちらかの要素を
    # すべて書き換えなければならない
    if len(seq_even_select) == 1 and len(seq_odd_select) == 1 and seq_even_select[0] == seq_odd_select[0]:
        return N // 2

    for e in seq_even_select:
        for o in seq_odd_select:
            if e == o:
                continue
            ans = min(ans, N - count_seq_even[e] - count_seq_odd[o])
    return ans

N = int(input())
V = [int(i) for i in input().split()]
print(c_zigzag(N,V))

Submission status: Accepted

Solution code:
import math

class xx:
    val = 0
    num = 0

    def __init__(self,v,n):
        self.val,self.num = v,n

while True:
    try:
        n = eval(input().strip())

        arr = list(map(int,input().strip().split()))

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

        for i in range(1,n,2):
            if arr[i] in two.keys():
                two[arr[i]] = two[arr[i]] + 1
            else:
                two[arr[i]] = 1

        # print(one)
        # print(two)
        one = sorted(one.items(),key=lambda x:x[1],reverse=True)
        two = sorted(two.items(), key=lambda x: x[1], reverse=True)
        # print(one)
        # print(two)

        if one[0][0] != two[0][0]:
            sum = n // 2 - one[0][1]
            sum += n // 2 - two[0][1]
            print(sum)
        elif one[0][0] == two[0][0] and one[0][1] == two[0][1] and one[0][1] == n // 2:
            print(n // 2)
        elif one[0][0] == two[0][0]:
            len1 = len(one)
            len2 = len(two)

            if len1 >= 2 and len2 >= 2:
                sum = n // 2 - one[0][1]
                sum += n // 2 - two[1][1]

                sum2 = n // 2 - one[1][1]
                sum2 += n // 2 - two[0][1]

                print(min(sum,sum2))
            elif len1 == 1 and len2 >= 2:
                sum = n // 2 - one[0][1]
                sum += n // 2 - two[1][1]
                print(sum)
            elif len1 >= 2 and len2 == 1:
                sum = n // 2 - one[1][1]
                sum += n // 2 - two[0][1]
                print(sum)
    except:
        break

Submission status: Accepted

Solution code:
from collections import Counter


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

"""数字が1種類の場合は半分を書き換える必要が確定"""
if len(set(v)) == 1:
    print(len(v) // 2)
    exit()

"""一番多く出現する2つの数字を探す"""
max_hit = 0
for odd_val, odd_hit in Counter(v[1::2]).most_common(2):
    for even_val, even_hit in Counter(v[::2]).most_common(2):
        if odd_val == even_val:
            continue  # 奇数と偶数は同じ数字じゃだめ
        if odd_hit + even_hit > max_hit:
            max_hit = odd_hit + even_hit

"""一致していない要素が書き換え必要回数"""
print(len(v) - max_hit)
exit()

Submission status: Accepted

Solution code:
from collections import Counter as c
n=int(input())
*l,=map(int,input().split())

ca=sorted(c(l[1::2]).items(),key=lambda x:x[1])[::-1]
cb=sorted(c(l[0::2]).items(),key=lambda x:x[1])[::-1]

v1=sum([i[1] for i in ca])
v2=sum([i[1] for i in cb])

a1=ca[0][0]
for i in cb:
  if a1==i[0]:continue
  b=i[1]
  break
else:
  b=0
ans1=v2-b+v1-ca[0][1]

a1=cb[0][0]
for i in ca:
  if a1==i[0]:continue
  b=i[1]
  break
else:
  b=0
ans2=v1-b+v2-cb[0][1]

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
import collections
 
n = int(input())
v = [int(i) for i in input().split()]
 
v1 = [v[i] for i in range(0,n,2)]
v2 = [v[i] for i in range(1,n,2)]
 
count1 = collections.Counter(v1).most_common(2)
count2 = collections.Counter(v2).most_common(2)

if count1[0][0] != count2[0][0]:
    res = len(v1) - count1[0][1] + len(v2) - count2[0][1]
    print(res)
else:
    if len(count1) == 1:
        print(len(v1))
    else:
        res1 = len(v1) - count1[0][1] + len(v2) - count2[1][1]
        res2 = len(v1) - count1[1][1] + len(v2) - count2[0][1]
        print(min(res1,res2))

Submission status: Accepted

Solution code:
import operator

n=int(input().strip())
lst=input().split(' ')
map(int, lst)

d1 = {}
d2 = {}

for i in range(len(lst)):
    if i % 2 == 0:
        if lst[i] in d1:
            d1[lst[i]] += 1
        else:
            d1[lst[i]] = 1
    else:
        if lst[i] in d2:
            d2[lst[i]] += 1
        else:
            d2[lst[i]] = 1

sd1 = sorted(d1.values(), reverse=True)
sd2 = sorted(d2.values(), reverse=True)
key1 = max(d1.items(), key=operator.itemgetter(1))[0]
key2 = max(d2.items(), key=operator.itemgetter(1))[0]

if(key1 == key2):

    v11 = sd1[0]
    if(len(d1) == 1):
        v12 = 0
    else:
        v12 = sd1[1]

    v21 = sd2[0]
    if(len(d2) == 1):
        v22 = 0
    else:
        v22 = sd2[1]

    print(min(n-v11-v22, n-v12-v21))

else:
    print(n-sd1[0]-sd2[0])

Submission status: Accepted

Solution code:
from collections import Counter

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

"""数字が1種類の場合は半分を書き換える必要が確定"""
if len(set(v)) == 1:
    print(len(v) // 2)
    exit()

"""要素出現数を集計"""
odd_count = Counter(v[1::2])
even_count = Counter(v[::2])

"""一番多く出現する2つの数字を探す"""
max_hit = 0
odd = None
even = None
for odd_val, odd_hit in odd_count.most_common(2):
    for even_val, even_hit in even_count.most_common(2):
        if odd_val == even_val:
            continue  # 奇数と偶数は同じ数字じゃだめ
        if odd_hit + even_hit > max_hit:
            max_hit = odd_hit + even_hit
            odd = odd_val
            even = even_val

"""一致していない要素が書き換え必要回数"""
print(len(v) - max_hit)

Submission status: Accepted

Solution code:
from collections import Counter
    
n = int(input())
v = list(map(int, input().split()))
odd = v[::2]
even = v[1::2]
    
c_even = Counter(even).most_common(2)
c_odd = Counter(odd).most_common(2)

    
if(c_even[0][0] != c_odd[0][0]):
    print(n - c_even[0][1] - c_odd[0][1])
else:
    if(len(c_even) == 1):
        print(n//2)
    else:
        second = max([c_even[1][1], c_odd[1][1]])
        print(n - c_even[0][1] - second)

Submission status: Accepted

Solution code:
#input phase
N = int(input())
V = list(map(int,input().split()))
#make dictionary
D1 = {}
D2 = {}
#append element to dictionary
for i in range(N):
    if i % 2 == 0 :
        if V[i] in D1 :
            D1[V[i]] += 1
        else:
            D1[V[i]] = 1
    else:
        if V[i] in D2 :
            D2[V[i]] += 1
        else:
            D2[V[i]] = 1
#define max-value of each dictionary
maxD1 = max(D1.values())
maxD2 = max(D2.values())
# I haven't appreciated dictionary method well...
if max(D1, key=D1.get) != max(D2, key=D2.get):
    print(N-maxD1-maxD2)
else:
    D1[max(D1, key=D1.get)] = 0
    D2[max(D2, key=D2.get)] = 0
    maxD12 = max(D1.values())
    maxD22 = max(D2.values())
    if maxD12 >= maxD22 :
        if len(D1) > 1 :
            print(N-maxD12-maxD2)
        else :
            print(N-maxD2)
    else :
        if len(D2) > 1 :
            print(N-maxD1-maxD22)
        else :
            print(N-maxD2)

Submission status: Accepted

Solution code:
from collections import Counter

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

"""数字が1種類の場合は半分を書き換える必要が確定"""
if len(set(v)) == 1:
    print(len(v) // 2)
    exit()

"""要素出現数を集計"""
odd_count = Counter(v[1::2])
even_count = Counter(v[::2])

"""一番多く出現する2つの数字を探す"""
max_hit = 0
odd = None
even = None
for odd_val, odd_hit in odd_count.most_common(5):
    # if max_hit > odd_hit:
    #     break  # もう最大値は更新されないので打ち止め
    for even_val, even_hit in even_count.most_common(5):
        # if max_hit > even_hit:
        #     break  # もう最大値は更新されないので打ち止め
        if odd_val == even_val:
            continue  # 数字は2種類ないとダメ
        if odd_hit + even_hit > max_hit:
            max_hit = odd_hit + even_hit
            odd = odd_val
            even = even_val

"""一致していない要素が書き換え必要回数"""
print(len(v) - max_hit)

Submission status: Accepted

Solution code:
from collections import Counter
    
n = int(input())
v = list(map(int, input().split()))
odd = v[::2]
even = v[1::2]
    
c_even = Counter(even).most_common(2)
c_odd = Counter(odd).most_common(2)

    
if(c_even[0][0] != c_odd[0][0]):
    print(n - c_even[0][1] - c_odd[0][1])
else:
    if(len(c_even) == 1):
        print(n//2)
    else:
        # second = max[c_even[1][1], c_odd[1][1]]
        # print(n - c_even[0][1] - second)
        print(n - max(c_odd[1][1], c_even[1][1]) - c_even[0][1])

Submission status: Accepted

Solution code:
from collections import Counter
    
n = int(input())
v = list(map(int, input().split()))
odd = v[::2]
even = v[1::2]
    
c_even = Counter(even).most_common(2)
c_odd = Counter(odd).most_common(2)

    
if(c_even[0][0] != c_odd[0][0]):
    print(n - c_even[0][1] - c_odd[0][1])
else:
    if(len(c_even) == 1):
        print(n//2)
    else:
        # second = max[c_even[1][1], c_odd[1][1]]
        # print(n - c_even[0][1] - second)
        print(n - max(c_even[0][1] + c_odd[1][1], c_even[1][1] + c_odd[0][1]))

Submission status: Accepted

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

def main():
    n = int(input())
    v_list = list(map(int, input().split()))
    v_list_even = v_list[::2]
    v_list_odd = v_list[1::2]
    counter_even = Counter(v_list_even)
    counter_odd = Counter(v_list_odd)
    if counter_even.most_common()[0][0] == counter_odd.most_common()[0][0]:
        even_first = counter_even.most_common()[0][1]
        odd_first = counter_odd.most_common()[0][1]
        if len(counter_even.most_common()) == 1:
            even_second = 0
        else:
            even_second = counter_even.most_common()[1][1]
        if len(counter_odd.most_common()) == 1:
            odd_second = 0
        else:
            odd_second = counter_odd.most_common()[1][1]
        ans = min(len(v_list_even) - even_first + len(v_list_odd) - odd_second, len(v_list_even) - even_second + len(v_list_odd) - odd_first)
    else:
        ans = len(v_list_even) - counter_even.most_common()[0][1] + len(v_list_odd) - counter_odd.most_common()[0][1]
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

from collections import Counter
import operator
def f():
    evn = Counter(A[::2])
    odd = Counter(A[1::2])

    a = sorted(odd.most_common(2), key=operator.itemgetter(1, 0), reverse=True)
    b = sorted(evn.most_common(2), key=operator.itemgetter(1, 0), reverse=True)

    ac = a[0][1]
    bc = b[0][1]
    ret = n - (ac + bc)
    if a[0][0] == b[0][0]:
        bc_2 = b[1][1] if len(b) != 1 else 0
        ac_2 = a[1][1] if len(a) != 1 else 0
        ret = n - max(ac + bc_2, ac_2 + bc)
    return ret
print(f())

Submission status: Accepted

Solution code:
import collections

n = int(input())
v = [int(i) for i in input().split()]
odd = collections.Counter(v[1::2])
even = collections.Counter(v[0::2])
# print(odd)

max_odd = max(odd, key=odd.get)
# print(max_odd)
max_even = max(even, key=even.get)
# print(max_even)

max_odd_v = odd[max_odd]
max_even_v = even[max_even]

if max_odd == max_even:
    if len(odd) > 1:
        max_2nd_odd = odd.most_common(2)[1][0]
        max_2nd_odd_v = odd[max_2nd_odd]
    else:
        max_2nd_odd_v = 0
    if len(even) > 1:
        max_2nd_even = even.most_common(2)[1][0]
        max_2nd_even_v = even[max_2nd_even]
    else:
        max_2nd_even_v = 0
    if (max_odd_v + max_2nd_even_v) < (max_even_v + max_2nd_odd_v):
        print(n - (max_even_v + max_2nd_odd_v))
    else:
        print(n - (max_odd_v + max_2nd_even_v))
else:
    print(n - (max_odd_v + max_even_v))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = [int(i) for i in input().split()]
e = []
o = []
for i, a in enumerate(v):
    if i % 2:
        o.append(a)
    else:
        e.append(a)
oc = Counter(o)
ec = Counter(e)
h = n//2
if oc.most_common(1)[0][0] != ec.most_common(1)[0][0]:
    a = h - oc.most_common(1)[0][1]
    b = h - ec.most_common(1)[0][1]
    print(a+b)
else:
    if len(oc) == 1:
        print(h)
        exit()
    E1 = E2 = 0
    for i, (k, v) in enumerate(ec.most_common(2)):
        if i == 0:
            E2 = E1 = k
        else:
            E2 = k
    O1 = O2 = 0
    for i, (k, v) in enumerate(oc.most_common(2)):
        if i == 0:
            O2 = O1 = k
        else:
            O2 = k
    print(min(h-ec[E1] + h - oc[O2], h - ec[E2] + h - oc[O1]))

Submission status: Accepted

Solution code:
iN = int(input())
aV = [int(_) for _ in input().split()]

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

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

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

if iMax1aV0 != iMax1aV1:
    print(iN - aV0[iMax1aV0] - aV1[iMax1aV1])
else:
    print(iN - max(aV0[iMax1aV0],aV1[iMax1aV1]) - max(aV0[aV0dec[1]],aV1[aV1dec[1]]))

Submission status: Accepted

Solution code:
import collections

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

Submission status: Accepted

Solution code:
n = int(input())
lst = list(map(int, input().split()))
ma = {}
mb = {}
for i in range(0, n, 2):
    lst[i] in ma
    if lst[i] in ma:
        ma[lst[i]] += 1
    else:
        ma[lst[i]] = 1
    lst[i+1] in mb
    if lst[i+1] in mb:
        mb[lst[i+1]] += 1
    else:
        mb[lst[i+1]] = 1
if max(ma, key=ma.get) != max(mb, key=mb.get):
    d = n - max(ma.values()) - max(mb.values())
else:
    ma2 = max(ma.values())
    mb2 = max(mb.values())
    ma[max(ma, key=ma.get)] = 0
    mb[max(mb, key=mb.get)] = 0
    d1 = n - ma2 - max(mb.values())
    d2 = n - max(ma.values()) - mb2
    d = min(d1, d2)
print(d)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
gu = v[::2]
ki = v[1::2]
ans = 0
k,g = False,False

###全部同じなら全長の半分,
# 最頻値が同じなら2番目を比べる
if v.count(v[0]) == n:
    ans += n // 2
else:
    kic = Counter(ki)
    guc = Counter(gu)
    k1 = kic.most_common(2)
    g1 = guc.most_common(2)
    if k1[0][0] == g1[0][0]:
        if k1[1][1] < g1[1][1]:
            g = True
        else:
            k = True

    kic = list(kic.values())
    guc = list(guc.values())
    kic.sort(reverse = True)
    guc.sort(reverse = True)
    if len(kic) != 1:
        if k:
            ans += kic[0]
            ans += sum(kic[2::])
        else:
            ans += sum(kic[1::]) 
    if len(guc) != 1:
        if g:
            ans += guc[0]
            ans += sum(guc[2::])
        else:
            ans += sum(guc[1::])

print(ans)

Submission status: Accepted

Solution code:
import collections

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

def calc(vs):
    r = dict(collections.Counter(vs))
    r[0] = 0
    ri = r.items()
    return sorted(ri, key=lambda x:-x[1])

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 = min(N - ve[0][1] - vo[1][1], N - ve[1][1] - vo[0][1])

print(result)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
x = [int(i) for i in input().split()]
a,b = [],[]
for i in range(n):
	if i%2: a.append(x[i])
	else: b.append(x[i])
ca,cb = Counter(a).most_common(),Counter(b).most_common()
if ca[0][0]!=cb[0][0]: print(n-ca[0][1]-cb[0][1])
elif len(ca)!=1 and len(cb)!=1: print(min(n-ca[0][1]-cb[1][1],n-ca[1][1]-cb[0][1]))
elif len(ca)==len(cb): print(n//2)
elif len(ca)==1: print(n//2-cb[1][1])
else: print(n//2-ca[1][1])

Submission status: Accepted

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

"""
9
1 1 1 1 2 2 2 4 
1 1 1 1 3 3 4 4
"""

#V = [1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 2, 3, 2, 4, 4, 4]

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

f1 = Counter(v1).most_common(2)
f2 = Counter(v2).most_common(2)

ans = 0

if len(set(v1)) == len(set(v2)) == 1:
    if v1 == v2:
        ans = min(len(v1), len(v2))
else:
    if f1[0][0] != f2[0][0]:
        ans = (len(v1) - f1[0][1]) + (len(v2) - f2[0][1])
    else:
        if f1[1][1] < f2[1][1]:
            ans = (len(v1) - f1[0][1]) + (len(v2) - f2[1][1])
        else:
            ans = (len(v1) - f1[1][1]) + (len(v2) - f2[0][1])
        
print(ans)

Submission status: Accepted

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

from collections import Counter
import operator
def f():
    evn = Counter(A[::2])
    odd = Counter(A[1::2])

    a = sorted(odd.most_common(2), key=operator.itemgetter(1, 0), reverse=True)
    b = sorted(evn.most_common(2), key=operator.itemgetter(1, 0), reverse=True)

    ac = (n // 2) - a[0][1]
    bc = (n // 2) - b[0][1]
    ret = ac + bc
    if a[0][0] == b[0][0]:
        bc_2 = n // 2 - (b[1][1] if len(b) != 1 else 0)
        ac_2 = n // 2 - (a[1][1] if len(a) != 1 else 0)
        ret = min(ac + bc_2, ac_2 + bc)
    return ret
print(f())

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = [int(i) for i in input().split()]
v_0 = v[0::2]
v_1 = v[1::2]
ans = 0

c0 = Counter(v_0).most_common(2)
c1 = Counter(v_1).most_common(2)

# 要素が1つしか出てこない場合を判定
if len(set(v_0)) == len(set(v_1)) == 1:
    if v_0 == v_1:
        ans = min(len(v_0), len(v_1))

else:
    # mostが被っていないか判定
    if c0[0][0] != c1[0][0]:
        ans = (len(v_0) - c0[0][1]) + (len(v_1) - c1[0][1])
    else:
        w1 = (len(v_0) - c0[0][1]) + (len(v_1) - c1[1][1])
        w2 = (len(v_0) - c0[1][1]) + (len(v_1) - c1[0][1])
        ans = min(w1, w2)

print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())

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

array_0 = collections.Counter(array[::2]).most_common()
array_1 = collections.Counter(array[1::2]).most_common()


if array_0[0][0] == array_1[0][0]:
    if len(array_0) * len(array_1) == 1:
        print(n // 2)
        exit()
    elif array_0[0][1] > array_1[0][1]:
        print(len(array) - array_1[1][1] - array_0[0][1])
        exit()
    elif array_0[0][1] < array_1[0][1]:
        print(len(array) - array_1[0][1] - array_0[1][1])
        exit()
    else:
        print(min(len(array) - array_1[0][1] - array_0[1][1], len(array) - array_1[1][1] - array_0[0][1]))
        exit()
print(len(array) - array_1[0][1] - array_0[0][1])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
gu = v[::2]
ki = v[1::2]
ans = 0
k,g = False,False

if v.count(v[0]) == n:#全部同じ
    ans += n // 2
else:
    kic = Counter(ki)
    guc = Counter(gu)
    k1 = kic.most_common(2)
    g1 = guc.most_common(2)
    if k1[0][0] == g1[0][0]:
        if k1[1][1] < g1[1][1]:
            k = True
        else:
            g = True

    kic = list(kic.values())
    guc = list(guc.values())
    kic.sort(reverse = True)
    guc.sort(reverse = True)
    if len(kic) != 1:
        if g:
            ans += kic[0]
            ans += sum(kic[2::])
        else:
            ans += sum(kic[1::]) 
    if len(guc) != 1:
        if k:
            ans += guc[0]
            ans += sum(guc[2::])
        else:
            ans += sum(guc[1::])

print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())

al = [int(l) for l in input().split()]

a = []
b = []
for i, x in enumerate(al):
    if i%2 == 0:
        a.append(x)
    else:
        b.append(x)

ac = collections.Counter(a).most_common()
bc = collections.Counter(b).most_common()

if ac[0][0] != bc[0][0]:
    print(n-ac[0][1]-bc[0][1])
else:
    if len(ac) == 1 and len(bc) == 1:
        print(n-max(ac[0][1], bc[0][1]))
    elif len(ac) == 1:
        print(n-max(ac[0][1], ac[1][1]+bc[0][1]))
    elif len(ac) == 1:
        print(n-max(bc[0][1], ac[0][1]+bc[1][1]))
    else:
        print(n-max(ac[1][1]+bc[0][1], ac[0][1]+bc[1][1]))

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

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

odds_counter = defaultdict(int)
evens_counter = defaultdict(int)

for o in V[0::2]:
    odds_counter[o] += 1
for e in V[1::2]:
    evens_counter[e] += 1

odds = sorted(odds_counter.items(), key=lambda x: x[1], reverse=True)
evens = sorted(evens_counter.items(), key=lambda x: x[1], reverse=True)

if odds[0][0] != evens[0][0]:
    ans = n - odds[0][1] - evens[0][1]
else:
    if len(odds) == 1 and len(evens) == 1:
        ans = odds[0][1]
    elif len(odds) == 1 and len(evens) != 1:
        ans = n - odds[0][1] - evens[1][1]
    elif len(odds) != 1 and len(evens) == 1:
        ans = n - odds[1][1] - evens[0][1]
    else:
        ans = min(n - odds[0][1] - evens[1][1], n - odds[1][1] - evens[0][1])
print(ans)

Submission status: Accepted

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

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

cv1 = collections.Counter(v[::2])
cv2 = collections.Counter(v[1::2])
num1 = cv1.most_common()[0][0]
num2 = cv2.most_common()[0][0]
cnt1 = cv1.most_common()[0][1]
cnt2 = cv2.most_common()[0][1]
if num1==num2:
  cnt3 = cv1.most_common()[1][1]
  cnt4 = cv2.most_common()[1][1]
  a,b = cnt1+cnt4,cnt2+cnt3
  print(min(n-a,n-b))
  exit()
print(n-(cnt1+cnt2))

Submission status: Accepted

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

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

f1 = Counter(v1).most_common(2)
f2 = Counter(v2).most_common(2)

ans = 0

if len(set(v1)) == len(set(v2)) == 1:
    if v1 == v2:
        ans = min(len(v1), len(v2))
else:
    if f1[0][0] != f2[0][0]:
        ans = (len(v1) - f1[0][1]) + (len(v2) - f2[0][1])
    else:
        ans1 = (len(v1) - f1[0][1]) + (len(v2) - f2[1][1])
        ans2 = (len(v1) - f1[1][1]) + (len(v2) - f2[0][1])
        ans = min(ans1, ans2)

print(ans)

Submission status: Accepted

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

a = [ v[i] for i in range(0,n,2)]
b = [ v[i] for i in range(1,n,2)]
A = [0] * 100010
B = [0] * 100010

for i in range(int(n/2)):
    A[a[i]] += 1
    B[b[i]] += 1

if A.index(max(A)) == B.index(max(B)):
    BB = sorted(B)
    cc1 = len(b) - BB[-2] + len(a) - max(A)
    AA = sorted(A)
    cc2 = len(a) - AA[-2] + len(b) - max(B)
    if cc1 > cc2:
        print(cc2)
    else:
        print(cc1)
else:
    print(n - max(A) - max(B))

Submission status: Accepted

Solution code:
# coding: utf-8
import collections
n=int(input())
all_str=list(map(int,input().split()))
i=0
j=0
o_str=[]   # 0, ,2,
e_str=[]   #  ,1, ,3,
while(2*i<n):
  o_str.append(all_str[2*i])
  i+=1
while(2*j+1<n):
  e_str.append(all_str[2*j+1])
  j+=1

o_c=collections.Counter(o_str)
e_c=collections.Counter(e_str)
o_mode=o_c.most_common()[0][0]
e_mode=e_c.most_common()[0][0]
o_mode_n=o_str.count(o_mode)
e_mode_n=e_str.count(e_mode)
if(o_mode==e_mode):
  if(len(o_c)==1 and len(e_c)==1):
    if(o_mode_n>e_mode_n):
      e_mode_n=0
      
    else:
      
      o_mode_n=0
      
  else:
    if(len(o_c)==1):
      o_c_sec=0
      e_c_sec=e_c.most_common()[1][1]
    elif(len(e_c)==1):
      e_c_sec=0
      o_c_sec=o_c.most_common()[1][1]
    else:
      o_c_sec=o_c.most_common()[1][1]
      e_c_sec=e_c.most_common()[1][1]
    if(o_c_sec>e_c_sec):
      o_mode_n=o_c.most_common()[1][1]
    else:
      e_mode_n=e_c.most_common()[1][1]
alt=len(all_str)-o_mode_n-e_mode_n
print(alt)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
l = list(range(n))
odd = [v[i] for i in l if i%2 == 0]
even = [v[i] for i in l if i%2 == 1]

import collections
oddm = collections.Counter(odd).most_common()
evenm = collections.Counter(even).most_common()

if oddm[0][0] != evenm[0][0]:
    a = (n/2)-oddm[0][1]+(n/2)-evenm[0][1]
else:
    if len(oddm) == 1 and len(evenm) == 1:
        a = n/2
    elif len(oddm) == 1:
        a = (n/2)-evenm[1][1]
    elif len(evenm) == 1:
        a = (n/2)-oddm[1][1]
    else:
        a = n-max(evenm[1][1]+oddm[0][1],evenm[0][1]+oddm[1][1])

print(int(a))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(aa) for aa in input().split()]

def add_entry(hist, aa):
    if aa in hist:
        hist[aa] += 1
    else:
        hist[aa] = 1


def make_hist(a):
    even_hist = {}
    odd_hist = {}

    for i in range(len(a)//2):
        add_entry(even_hist, a[2*i])
        add_entry(odd_hist, a[2*i+1])

    return even_hist, odd_hist


def get_sorted_hist(hist):
    return sorted(hist.items(), key=lambda x: -x[1])

eh, oh = make_hist(a)
seh = get_sorted_hist(eh)
soh = get_sorted_hist(oh)


if seh[0][0] != soh[0][0]:
    print(len(a) - seh[0][1] - soh[0][1])
else:
    if len(seh) == 1 and len(soh) == 1:
        print(len(a)//2)
    elif len(seh) == 1:
        print(len(a) - seh[0][1] - soh[1][1])
    elif len(soh) == 1:
        print(len(a) - soh[0][1] - seh[1][1])
    else:
        print(min(len(a) - seh[0][1] - soh[1][1], len(a) - soh[0][1] - seh[1][1]))

Submission status: Accepted

Solution code:
import collections

n = input()
nums = list(map(int, input().split()))
odd = nums[0::2]
even = nums[1::2]
odd_c = collections.Counter(odd)
even_c = collections.Counter(even)


if odd_c.most_common(1)[0][0] == even_c.most_common(1)[0][0]:
    if len(odd_c) >= 3 and len(even_c) >= 3:
        odd_first_count = len(odd) - odd_c.most_common(1)[0][1]
        even_first_count = len(even) - even_c.most_common(1)[0][1]
        odd_second_count = len(odd) - odd_c.most_common(2)[1][1]
        even_second_count = len(even) - even_c.most_common(2)[1][1]
        if odd_first_count + even_second_count < odd_second_count + even_first_count:
            print(odd_first_count + even_second_count)
            exit()
        else:
            print(odd_second_count + even_first_count)
            exit()
    odd_next_most = 0
    even_next_most = 0
    if len(odd_c) >= 2:
        odd_next_most = odd_c.most_common(2)[1][1]
    if len(even_c) >= 2:
        even_next_most = even_c.most_common(2)[1][1]
    if odd_next_most > even_next_most:
        print(len(odd) - odd_next_most + len(even) - even_c.most_common(1)[0][1])
    else:
        print(len(odd) - odd_c.most_common(1)[0][1] + len(even) - even_next_most)
else:
    odd_change = len(odd) - odd_c.most_common(1)[0][1]
    even_change = len(even) - even_c.most_common(1)[0][1]
    print(odd_change + even_change)

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = input().split()
v_int = [int(i) for i in v]
v_odd = []
v_even = []
for i in range(int(n/2)):
  v_even.append(v_int[i*2])
  v_odd.append(v_int[i*2+1])
c_odd = collections.Counter(v_odd)
c_even = collections.Counter(v_even)
max_odd = 0
second_odd = 0
max_key_odd = 0
for key in c_odd:
  if max_odd < c_odd[key]:
    second_odd = max_odd
    max_odd = c_odd[key]
    max_key_odd = key
  elif second_odd < c_odd[key]:
    second_odd = c_odd[key]
    
max_even = 0
second_even = 0
max_key_even = 0
for key in c_even:
  if max_even < c_even[key]:
    second_even = max_even
    max_even = c_even[key]
    max_key_even = key
  elif second_even < c_even[key]:
    second_even = c_even[key]
    
if max_key_even != max_key_odd:
  print(int(n)- max_even - max_odd)
else:
  if max_even > max_odd:
    if max_even == second_even:
      print(int(n) - max_even - max_odd)
    else:
      print(int(n) - max_even - second_odd)
  elif max_even < max_odd:
    if max_odd == second_odd:
      print(int(n) - max_odd - max_even)
    else:
      print(int(n) - max_odd - second_even)
  else : 
    if second_odd > second_even:
      print(int(n) - max_even - second_odd)
    else :
      print(int(n) - max_odd - second_even)