Problem:


Score : 300 points

Problem Statement

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

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

Exactly two different numbers appear in the sequence.

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

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

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

Sample Input 1

4
3 1 3 2

Sample Output 1

1

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

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

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

Sample Input 3

4
1 1 1 1

Sample Output 3

2

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



Sample input:
4
3 1 3 2


Sample output:
1

Submission status: Accepted

Solution code:
N = int(input())
a = [[],[]]
[a[i%2].append(t) for i, t in enumerate(map(int, input().split()))]

a_cnt = [{},{}]
for i in range(2):
    for j in range(len(a[i])):
        if a[i][j] in a_cnt[i]:
            a_cnt[i][a[i][j]] += 1
        else:
            a_cnt[i][a[i][j]] = 1

for i in range(2):
    # print(list(a_cnt[i].items()))
    a_cnt[i] = list(sorted(list(a_cnt[i].items()),\
        key=(lambda x:x[1]), reverse=True))[:2]

# print(a_cnt)
if a_cnt[0][0][0] != a_cnt[1][0][0]:
    print(N -a_cnt[0][0][1] -a_cnt[1][0][1])
else:
    if len(a_cnt[0])==1 and len(a_cnt[1])==1:
        print(N//2)
    else:
        ans1 = N -a_cnt[0][0][1] -a_cnt[1][1][1]
        ans2 = N -a_cnt[0][1][1] -a_cnt[1][0][1]
        print(min(ans1, ans2))

Submission status: Accepted

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

# 始点から5(6-1)番目の要素を2個ごとに取り出す
# a[:6:2] #[0, 2, 4]
# 7番目から終点までの要素を2個ごとに取り出す
# a[7::2] #[7, 9]
odd=vlis[0::2]
even=vlis[1::2]



import collections
odd_max=collections.Counter(odd).most_common(2)
even_max=collections.Counter(even).most_common(2)

# 一番多い数の数字が偶奇で同じ場合
if odd_max[0][0]==even_max[0][0]:
    # 全てが同じ数字の場合
    if len(odd_max)==1 and len(even_max)==1:
        print(int(len(vlis)/2))
    # 偶数列が全部同じ値だった時は奇数列は２番目多い数の数字に合わせる
    elif len(even_max)==1:
        print(len(vlis)-even_max[0][1]-odd_max[1][1])
    # 奇数列が全部同じ値だった時は偶数列は２番目多い数の数字に合わせる
    elif len(odd_max)==1:
        print(len(vlis)-odd_max[0][1]-even_max[1][1])
    else:

        if odd_max[0][1]+even_max[1][1]>=odd_max[1][1]+even_max[0][1]:
            print(len(vlis)-odd_max[0][1]-even_max[1][1])
        else:
            print(len(vlis)-odd_max[1][1]-even_max[0][1])
else:
    print(len(vlis)-odd_max[0][1]-even_max[0][1])

Submission status: Accepted

Solution code:
n=int(input())
vlis=[int(i) for i in input().split()]
odd=vlis[0::2]
even=vlis[1::2]
import collections 
odd_=collections.Counter(odd).most_common(2)
even_=collections.Counter(even).most_common(2)
 
if odd_[0][0]==even_[0][0]:
    if len(odd_)==1 and len(even_)==1:
        print(int(len(vlis)/2))
    elif len(even_)==1:
        print(len(vlis)-even_[0][1]-odd_[1][1])
    elif len(odd_)==1:
        print(len(vlis)-odd_[0][1]-even_[1][1])
    else:
        if odd_[0][1]+even_[1][1]>=odd_[1][1]+even_[0][1]:
            print(len(vlis)-odd_[0][1]-even_[1][1])
        else:
            print(len(vlis)-odd_[1][1]-even_[0][1])
else:
    print(len(vlis)-odd_[0][1]-even_[0][1])

Submission status: Accepted

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

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

even = {}
odd = {}
# 奇数番目と偶数番目に分けて集計
for i in range(N):
    if i % 2 == 0:
        even[vN[i]] = even.get(vN[i], 0) + 1
    else:
        odd[vN[i]] = odd.get(vN[i], 0) + 1

# 集計数の多い順ソート
even_sorted = sorted(even.items(), key=lambda x: x[1], reverse=True)
odd_sorted = sorted(odd.items(), key=lambda x: x[1], reverse=True)

# 1番多い数字が同じだった場合の別処理
if even_sorted[0][0] == odd_sorted[0][0]:
    # 偶奇それぞれ数字が1種類しかない場合は、この後の比較処理のためにダミーの0を入れておく
    if len(even_sorted) == 1:
        even_sorted.append((0, 0))
    if len(odd_sorted) == 1:
        odd_sorted.append((0, 0))
    # 偶奇それぞれを2番目にしてみて、数の大きい方
    print(N - max(even_sorted[1][1] + odd_sorted[0][1], even_sorted[0][1] + odd_sorted[1][1]))
else:
    print(N - even_sorted[0][1] - odd_sorted[0][1])

Submission status: Accepted

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

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

even = {}
odd = {}
# 奇数番目と偶数番目に分けて集計
for i in range(N):
    if i % 2 == 0:
        even[vN[i]] = even.get(vN[i], 0) + 1
    else:
        odd[vN[i]] = odd.get(vN[i], 0) + 1

# 集計数の多い順ソート
even_sorted = sorted(even.items(), key=lambda x: x[1], reverse=True)
odd_sorted = sorted(odd.items(), key=lambda x: x[1], reverse=True)

# 1番多い数字が同じだった場合の別処理
if even_sorted[0][0] == odd_sorted[0][0]:
    # 偶奇それぞれ数字が1種類しかない場合の別処理
    if len(even_sorted) == 1 and len(odd_sorted) > 1:
        print(N - even_sorted[0][1] - odd_sorted[1][1])
    elif len(even_sorted) > 1 and len(odd_sorted) == 1:
        print(N - even_sorted[1][1] - odd_sorted[0][1])  
    elif len(even_sorted) == 1 and len(odd_sorted) == 1:
        print(N // 2)
    else:
        # 偶奇それぞれを2番目にしてみて、数の大きい方
        print(N - max(even_sorted[1][1] + odd_sorted[0][1], even_sorted[0][1] + odd_sorted[1][1]))
else:
    print(N - even_sorted[0][1] - odd_sorted[0][1])

Submission status: Accepted

Solution code:
def solve(n, a):
    D = [{0: 0}, {0:0}]
    for i in range(n):
        if not a[i] in D[i%2]:
            D[i%2][a[i]] = 0
        D[i%2][a[i]] += 1
    e = sorted(D[0].items(), key=lambda _:-_[1])
    o = sorted(D[1].items(), key=lambda _:-_[1])
    k_e0, v_e0 = e[0]
    k_o0, v_o0 = o[0]
    if k_e0 != k_o0:
        return n - v_e0 - v_o0
    else:
        _, v_e1 = e[1]
        _, v_o1 = o[1]
        return min(n - v_e0 - v_o1, n - v_e1 - v_o0)

assert solve(4, [3,1,3,2]) == 1
assert solve(6, [105,119,105,119,105,119]) == 0
assert solve(4, [1,1,1,1]) == 2
n = input()
a = map(int, raw_input().split())
print solve(n, a)

Submission status: Accepted

Solution code:
from collections import Counter

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

odd = [a[i] for i in range(0,len(a),2)]
even = [a[i] for i in range(1,len(a),2)]

a_odd_most = Counter(odd).most_common()[:2]
a_even_most = Counter(even).most_common()[:2]

if a_odd_most[0][0] != a_even_most[0][0]:
    print(len(a) - a_odd_most[0][1] - a_even_most[0][1])
else:
    if len(a_odd_most) == 1 or len(a_even_most) == 1:
        a_odd_most.append((0,0))
        a_even_most.append((0,0))

    print(len(a) - max(a_odd_most[0][1]+a_even_most[1][1], 
                  a_odd_most[1][1]+a_even_most[0][1]))

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
from collections import Counter


input = sys.stdin.readline


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


def solve(N, V):
    v_even = [V[i] for i in range(0, N, 2)]
    v_odd = [V[i] for i in range(1, N, 2)]

    # 出現回数の多い上位2つの要素と出現回数を取得
    v_even_most = Counter(v_even).most_common()[:2]
    v_odd_most = Counter(v_odd).most_common()[:2]

    # 最頻値が被っていなければそのまま採用
    if v_even_most[0][0] != v_odd_most[0][0]:
        return N - v_even_most[0][1] - v_odd_most[0][1]
    else:
        # 数字が1種類しか登場しない場合は、0を追加
        # list index out of range対策
        if len(v_even_most) == 1 or len(v_odd_most) == 1:
            v_even_most.append((0, 0))
            v_odd_most.append((0, 0))

        # 書き換え回数が少ない方を採用
        return N - max(v_even_most[0][1] + v_odd_most[1][1],
                       v_even_most[1][1] + v_odd_most[0][1])


N = int(input())
V = inpl()
print(solve(N, V))

Submission status: Accepted

Solution code:
n = int(input())
a = input().split()
dic1 = {}
firitem1 = "0"
firnum1 = 0
secitem1 = "0"
secnum1 = 0
dic2 = {}
firitem2 = "0"
firnum2 = 0
secitem2 = "0"
secnum2 = 0

for i in range(len(a)):
  if i%2 == 0:
    if a[i] in dic1:
      dic1[a[i]] += 1
    else:
      dic1[a[i]] = 1
    
    if dic1[a[i]] > firnum1:
      if firitem1 == a[i]:
        firnum1 = dic1[a[i]]
      else:
        secitem1 = firitem1
        secnum1 = firnum1
        firitem1 = a[i]
        firnum1 = dic1[a[i]]
        
    elif dic1[a[i]] > secnum1:
      secitem1 = a[i]
      secnum1 = dic1[a[i]]
  else:
    if a[i] in dic2:
      dic2[a[i]] += 1
    else:
      dic2[a[i]] = 1
    
    if dic2[a[i]] > firnum2:
      if firitem2 == a[i]:
        firnum2 = dic2[a[i]]
      else:
        secitem2 = firitem2
        secnum2 = firnum2
        firitem2 = a[i]
        firnum2 = dic2[a[i]]
    elif dic2[a[i]] > secnum2:
      secitem2 = a[i]
      secnum2 = dic2[a[i]]

if firitem1 != firitem2:
  print(int(n/2-firnum1 + n/2-firnum2))
else:

  print(int(min(n/2-firnum1 + n/2-secnum2, n/2-secnum1 + n/2-firnum2)))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = [int(c) for c in input().split()]
vo = v[::2]
ve = v[1::2]
co = Counter(vo).most_common()
ce = Counter(ve).most_common()
co.append((-1, 0))
ce.append((-2, 0))
if co[0][0] != ce[0][0]:
	print(n - co[0][1] - ce[0][1])
else:
	res1 = n - co[0][1] - ce[1][1]
	res2 = n - co[1][1] - ce[0][1]
	print(min(res1, res2))

Submission status: Accepted

Solution code:
#!/bin/env python

def count_nums(v):
    counter = {}
    for x in v:
        if x not in counter:
            counter[x] = 0
        counter[x] += 1
    retval = counter.items()
    retval.sort(lambda a,b:b[1] - a[1])
    # i : index
    # retval[i][0] : number
    # retval[i][1] : count
    # retval is sorted by count
    return retval

def main():
    n = int(raw_input().strip())
    v = raw_input().strip().split(" ")
    v1 = [int(v[x]) for x in xrange(0, n, 2)]
    v2 = [int(v[x]) for x in xrange(1, n, 2)]

    cv1 = count_nums(v1)
    cv2 = count_nums(v2)

    if cv1[0][0] != cv2[0][0]:
        print n - (cv1[0][1] + cv2[0][1])
    elif len(cv1) == 1 and len(cv2) == 1:
        print len(v2)
    elif len(cv1) == 1:
        print len(v2) - cv2[1][1]
    elif len(cv2) == 1:
        print len(v1) - cv1[1][1]
    else:
        print n - max(cv1[0][1]+cv2[1][1], cv1[1][1]+cv2[0][1])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

# print(v1)
# print(v2)

d1, d2 = dict(), dict()


for num in v1:
    d1[num] = d1.get(num, 0) + 1

for num in v2:
    d2[num] = d2.get(num, 0) + 1

# for key, val in d1.items():
#    print("{} -> {}".format(key, val))

v1tmp1 = [(key, val) for key, val in d1.items()]
V1 = sorted(v1tmp1, key=lambda x: x[1], reverse=True)

v2tmp1 = [(key, val) for key, val in d2.items()]
V2 = sorted(v2tmp1, key=lambda x: x[1], reverse=True)

# print(tmp2)

ret = 0

if (V1[0][0] != V2[0][0]):
    ret = n - V1[0][1] - V2[0][1]
else:
    t1 = n - V1[0][1] - (V2[1][1] if len(V2) >= 2 else 0)
    t2 = n - (V1[1][1] if len(V1) > 2 else 0) - V2[0][1]
    ret = min(t1, t2)

print(ret)

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
vodd=[v[i] for i in range(n) if i%2==1]
veven=[v[i] for i in range(n) if i%2==0]
vodd.sort()
veven.sort()
def matome(l):
    result=[]
    a=l[0]
    b=0
    for i in range(len(l)):
        if l[i]==a:
            b+=1
        else:
            result.append([a,b])
            b=1
            a=l[i]
    result.append([a,b])
    return result
odd=matome(vodd)
odd.sort(key=lambda x:-x[1])
even=matome(veven)
even.sort(key=lambda x:-x[1])

if len(odd)==1 and len(even)==1:
    if odd[0][0]==even[0][0]:
        print(n//2)
    else:
        print(0)
elif len(odd)==1:
    if odd[0][0]==even[0][0]:
        print(n-odd[0][1]-even[1][1])
    else:
        print(n-odd[0][1]-even[0][1])
elif len(even)==1:
    if odd[0][0]==even[0][0]:
        print(n-odd[1][1]-even[0][1])
    else:
        print(n-odd[0][1]-even[0][1])
else:


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

    else:
        print(n-odd[0][1]-even[0][1])

Submission status: Accepted

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

for i in range(0, n, 2):
    if v[i] in d1.keys():
        d1[v[i]] += 1
    else:
        d1[v[i]] = 1
for i in range(1, n, 2):
    if v[i] in d2.keys():
        d2[v[i]] += 1
    else:
        d2[v[i]] = 1


def keys_values(d):
    fk, fv, sk, sv = 0, 0, 0, 0
    for k in d:
        if d[k] > fv:
            sk, sv = fk, fv
            fk, fv = k, d[k]
        elif d[k] > sv:
            sk, sv = k, d[k]
    return fk, fv, sk, sv


ak, av, bk, bv = keys_values(d1)
ck, cv, dk, dv = keys_values(d2)
if ak != ck:
    print(n-av-cv)
else:
    print(min((n-av-dv), n-bv-cv))

Submission status: Accepted

Solution code:
n=int(input())
vlis=[int(i) for i in input().split()]
odd=vlis[0::2]
even=vlis[1::2]
import collections 
odd_=collections.Counter(odd).most_common(2)
even_=collections.Counter(even).most_common(2)

if odd_[0][0]==even_[0][0]:
    if len(odd_)==1 and len(even_)==1:
        print(int(len(vlis)/2))
    elif len(even_)==1:
        print(len(vlis)-even_[0][1]-odd_[1][1])
    elif len(odd_)==1:
        print(len(vlis)-odd_[0][1]-even_[1][1])
    else:
        if odd_[0][1]+even_[1][1]>=odd_[1][1]+even_[0][1]:
            print(len(vlis)-odd_[0][1]-even_[1][1])
        else:
            print(len(vlis)-odd_[1][1]-even_[0][1])
else:
    print(len(vlis)-odd_[0][1]-even_[0][1])

Submission status: Accepted

Solution code:
import collections
n=int(input())
m=n//2
v1=list(map(int,input().split()))
v2=[]
for j in range(m):
    v2.append(v1.pop(j))
a1=sorted((collections.Counter(v1)).items(),key=lambda x:x[1],reverse=True)
a2=sorted((collections.Counter(v2)).items(),key=lambda x:x[1],reverse=True)
if a1[0][1]<a2[0][1]:a1,a2=a2[:],a1[:]
if a1[0][0]!=a2[0][0]:
    print(n-a1[0][1]-a2[0][1])
elif len(a2)==1:
    print(m)
elif a1[1][1]<a2[1][1]:
    print(n-a1[0][1]-a2[1][1])
else:
    print(n-a1[0][1]-a1[1][1])

Submission status: Accepted

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


def count_rewrite_times(n, v, even, odd):
    ans = 0

    for x in range(n):
        if x % 2 == 0:
            if v[x] != even:
                ans += 1
        else:
            if v[x] != odd:
                ans += 1

    return ans


def main():
    from collections import Counter

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

    if len(set(v)) == 1:
        print(n // 2)
        exit()
    else:
        even = Counter(v[::2]).most_common(1)[0][0]
        odd = Counter(v[1::2]).most_common(1)[0][0]

        if even == odd:
            even2 = Counter(v[::2]).most_common(2)[1][0]
            odd2 = Counter(v[1::2]).most_common(2)[1][0]

            print(min(count_rewrite_times(n, v, even, odd2),
                      count_rewrite_times(n, v, even2, odd)))
            exit()

        print(count_rewrite_times(n, v, even, odd))


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    from collections import Counter

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

    if len(set(v)) == 1:
        print(n // 2)
        exit()
    else:
        even = Counter(v[::2]).most_common(1)[0][0]
        odd = Counter(v[1::2]).most_common(1)[0][0]
        ans1 = 0

        if even == odd:
            even2 = Counter(v[::2]).most_common(2)[1][0]
            odd2 = Counter(v[1::2]).most_common(2)[1][0]
            ans2 = 0
            ans3 = 0

            for x in range(n):
                if x % 2 == 0:
                    if v[x] != even:
                        ans2 += 1
                else:
                    if v[x] != odd2:
                        ans2 += 1

            for y in range(n):
                if y % 2 == 0:
                    if v[y] != even2:
                        ans3 += 1
                else:
                    if v[y] != odd:
                        ans3 += 1

            print(min(ans2, ans3))
            exit()

        for i in range(n):
            if i % 2 == 0:
                if v[i] != even:
                    ans1 += 1
            else:
                if v[i] != odd:
                    ans1 += 1

        print(ans1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
v = list(map(int,input().split()))
e, o =[], []
for i in range(n//2):
    e.append(v[2*i])
    o.append(v[2*i+1])
e.sort()
o.sort()
e.append(-1)
o.append(-1)
# print(e,o)

e1, e2, o1, o2 = 0,0,0,0
E1, O1 = e[0], o[0]
c = 1
for i in range(0,n//2):
    if e[i]==e[i+1]:
        c+= 1
        continue
    if c>e1:
        e2 = e1
        e1 = c
        E1 = e[i]
    elif c>e2:
        e2 = c
    c = 1

c=1
for i in range(0,n//2):
    if o[i]==o[i+1]:
        c+= 1
        continue
    if c>o1:
        o2 = o1
        o1 = c
        O1 = o[i]
    elif c>o2:
        o2 = c
    c = 1
    
# print(e1,e2,E1,o1,o2,O1)

if E1==O1:
    print(min(n-e1-o2,n-e2-o1))
else:
    print(n-e1-o1)

Submission status: Accepted

Solution code:

n = int(input())
V = [int(x) for x in input().split(" ")]

# print(n)
# print(V)

dict0 = {}
dict1 = {}

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

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

# 多い順にソート(タプルに変えた後、dictの値部分(x[1])でソート)
d0 = sorted(dict0.items(), key=lambda x: x[1], reverse=True)
d1 = sorted(dict1.items(), key=lambda x: x[1], reverse=True)

# print(d0)
# print(d1)


n = int(n/2)
if (d0[0][0] != d1[0][0]):
    ret = (n - d0[0][1]) + (n - d1[0][1])
else:
    d1_1_1 = 0
    if len(d1) > 1:
        d1_1_1 = d1[1][1]
    r1 = (n - d0[0][1]) + (n - d1_1_1)

    d0_1_1 = 0
    if len(d0) > 1:
        d0_1_1 = d0[1][1]
    r2 = (n - d0_1_1) + (n - d1[0][1])
    ret = min(r1, r2)

print(ret)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

x = Counter(v[::2]).most_common()
y = Counter(v[1::2]).most_common()

if x[0][0] == y[0][0]:
    if len(x) == 1:
        d = x[0][1] + y[1][1] 
    elif len(y) == 1:
        d = x[1][1] + y[0][1]
    else:
        d = max(x[0][1] + y[1][1], x[1][1] + y[0][1]) 
else:
    d = x[0][1] + y[0][1]

print(n - d)

Submission status: Accepted

Solution code:
import collections

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

c1 = collections.Counter(v[0::2]).most_common()
c2 = collections.Counter(v[1::2]).most_common()
c1.append([0,0])
c2.append([0,0])
if(c1[0][0]!=c2[0][0]):
    print(n-c1[0][1]-c2[0][1])
elif(c1[1][1]>c2[1][1]):
    print(n-c1[1][1]-c2[0][1])
else:
    print(n-c1[0][1]-c2[1][1])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

even = Counter(even).most_common(2)
odd = Counter(odd).most_common(2)

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

print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = list(map(int,input().split()))
v0 = []
v1 = []
for i,ev in enumerate(v):
    if i%2 == 0:
        v0.append(ev)
    else:
        v1.append(ev)

mcv0 = Counter(v0).most_common(2)
mcv1 = Counter(v1).most_common(2)
cmcv0 = mcv0[0][1]
cmcv1 = mcv1[0][1]
ans = n - cmcv0 - cmcv1
if mcv0[0][0] == mcv1[0][0]:
    if (len(mcv0) == 1) and (len(mcv1) == 1):
        ans = int(n/2)
    else:
        ans = min(n - mcv0[1][1] - cmcv1, n - cmcv1 - mcv1[1][1])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
list = list(map(int, input().split()))
list_odd = list[::2]
list_even = list[1::2]


def cnt_element(x):
    dict = {}
    for i in x:
        if i not in dict:
            dict[i] = 1
        else:
            dict[i] += 1
    return [(v, k) for k, v in dict.items()]


cnt_odd = sorted(cnt_element(list_odd), reverse=True)
cnt_even = sorted(cnt_element(list_even), reverse=True)

if cnt_odd[0][1] == cnt_even[0][1]:
    if len(cnt_odd) == 1 or len(cnt_even) == 1:
        ans = int(n / 2)
    else:
        if cnt_odd[1][0] > cnt_even[1][0]:
            ans = n - cnt_odd[1][0] - cnt_even[0][0]
        else:
            ans = n - cnt_odd[0][0] - cnt_even[1][0]
else:
    ans = n - cnt_odd[0][0] - cnt_even[0][0]

print(ans)

Submission status: Accepted

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

list_odd = list[::2]
list_even = list[1::2]

def cnt_element(x):
    dict = {}
    for i in x:
        if i not in dict:
            dict[i] = 1
        else:
            dict[i] += 1
    return [(v,k) for k,v in dict.items()]

cnt_odd = sorted(cnt_element(list_odd),reverse=True)
cnt_even = sorted(cnt_element(list_even),reverse=True)

#最頻値が同じの場合
if cnt_odd[0][1] == cnt_even[0][1]:
    #リスト内の値が１つの場合
    if len(cnt_odd) ==1 or len(cnt_even) == 1:
        ans =  int(n / 2)
    #リストが２つ以上の場合
    else:
        #第２最頻値の出現数が奇数の方が多い
        if cnt_odd[1][0] > cnt_even[1][0]:
            ans = n - cnt_odd[1][0] - cnt_even[0][0]
        #第二２最頻値の出現数が偶数の方が多い
        elif cnt_odd[1][0] <= cnt_even[1][0]:
            ans = n - cnt_odd[0][0] - cnt_even[1][0]
#最頻値が異なる場合
else:
    ans = n - cnt_odd[0][0] - cnt_even[0][0]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

ki = v[::2]
gu = v[1::2]

ki = Counter(ki)
gu = Counter(gu)

ki = ki.most_common()
gu = gu.most_common()

if ki[0][0] == gu[0][0] :
    if len(ki) == 1 and len(gu) == 1 :
        print(int(n/2))
    else :
        if ki[1][1] > gu[1][1] :
            print(n-ki[1][1]-gu[0][1])
        else :
            print(n-ki[0][1]- gu[1][1])

else :
    print(n-ki[0][1]-gu[0][1])

Submission status: Accepted

Solution code:
from collections import Counter

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

ki = v[::2]
gu = v[1::2]

ki = Counter(ki)
gu = Counter(gu)

ki = ki.most_common()
gu = gu.most_common()

ans = 0

#最頻値が同じ
if ki[0][0] == gu[0][0]:
    #リストの値が１つのみ
    if len(ki) == 1 and len(gu) == 1:
        ans = int(n / 2)
    else:
        if ki[1][1] > gu[1][1]:
            ans = n - ki[1][1] - gu[0][1]
        else:
            ans = n - ki[0][1] - gu[1][1]
else:
    ans = n - ki[0][1] - gu[0][1]

print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(raw_input())
A = map(int, raw_input().split(" "))

c1 = collections.Counter(A[::2]).most_common(2)
c2 = collections.Counter(A[1::2]).most_common(2)

res = n
for i, x in c1:
    for j, y in c2:
        if i != j:
            res = min(res, n - x - y)
        res = min(res, n - y)
        res = min(res, n - x)
print res

Submission status: Accepted

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

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

even = [] #偶数番目のリスト
odd = [] #奇数番目のリスト

#偶数番目、奇数番目のリストを作成
for i in range(n):
    if i % 2 == 0:
        even.append(v[i])
    else:
        odd.append(v[i])

 #頻出順に並び替え
even_most = Counter(even).most_common()
odd_most = Counter(odd).most_common()

#頻出する数字が同じ場合
if even_most[0][0] == odd_most[0][0]:
    if len(even_most) == 1:
        if len(odd_most) == 1:
            ans = n / 2
        else:
            ans = n - even_most[0][1] - odd_most[1][1]
    elif len(odd_most) == 1:
            ans = n - even_most[1][1] - odd_most[0][1]
    else:
        ans = n - max(even_most[0][1] + odd_most[1][1], even_most[1][1] + odd_most[0][1])

#それ以外の場合（頻出する数字が同じでない場合）
else:
    ans = n - even_most[0][1] - odd_most[0][1]

print(int(ans))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = list(map(int, input().split()))
for_set_v = v # set用
set_v = set(v)
even = []
odd = []
count = 0 # 置き換えた回数
count_list = []
for num in range(n):
    if num % 2 == 0:
        even.append(v[num])
    else:
        odd.append(v[num])

even_most = Counter(even).most_common()
odd_most = Counter(odd).most_common()
#print(even_most)
#print(odd_most)
if len(set(v)) >= 2:
    if even_most[0][0] != odd_most[0][0]:
        if len(set(even)) != 1:
            count += (n/2) - int(even_most[0][1])
        if len(set(odd)) != 1:
            count += (n/2) - int(odd_most[0][1])
    else:
        try:
            count_list.append(n-int(even_most[0][1])-int(odd_most[1][1]))
            count_list.append(n-int(even_most[1][1])-int(odd_most[0][1]))
            #print(count_list)
            count += int(min(count_list))
        except:
            if len(even_most) == 1:
                count += (n/2)-int(odd_most[1][1])
            elif len(odd_most) == 1:
                count += (n/2)-int(even_most[1][1])
else:
    count += (n/2)
print(int(count))

Submission status: Accepted

Solution code:
def getmaxs(l):
    max1 = max2 = poz1 = poz2 = 0
    for i in range(0, len(l)):
        if l[i] > max1:
            max2 = max1
            poz2 = poz1
            max1 = l[i]
            poz1 = i
        elif l[i] > max2:
            max2 = l[i]
            poz2 = i
    return max1, max2, poz1, poz2


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

n = int(input())
l = list(map(int, input().split()))
for i in range(0, n):
    x = l[i]
    if i % 2 == 0:
        f1[x] = f1[x] + 1
    else:
        f2[x] = f2[x] + 1

m1, m2, p1, p2 = getmaxs(f1)
n1, n2, q1, q2 = getmaxs(f2)
if p1 != q1:
    print(n-n1-m1)
else:
    print(min(n-n1-m2, n-n2-m1))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
arr = [i for i in input().split()]
c = [Counter([i for (a,i) in enumerate(arr) if a%2==j]) for j in range(2)]
if c[0].most_common()[0][0]!=c[1].most_common()[0][0] :
    print(n-c[0].most_common()[0][1]-c[1].most_common()[0][1])
else :
    if(len(c[0].most_common())==1):
        co0 = 0
    else:
        co0 = c[0].most_common()[1][1]
    if(len(c[0].most_common())==1):
        co1 = 0;
    else :
        co1 = c[1].most_common()[1][1]
    print(min(n-c[0].most_common()[0][1]-co1,n-co0-c[1].most_common()[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())

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

odd = arr[0::2]
even = arr[1::2]

odd_counter = Counter(odd)
even_counter = Counter(even)

odd_mode = odd_counter.most_common()[0][0]
even_mode = even_counter.most_common()[0][0]

odd_diff = (N / 2) - odd_counter.most_common()[0][1]
even_diff = (N / 2) - even_counter.most_common()[0][1]

if odd_mode != even_mode:
	print(int(odd_diff + even_diff))
elif odd_diff == 0:
	print(int(N / 2))
else:
	odd_diff2 = (N / 2) - odd_counter.most_common()[1][1]
	even_diff2 = (N / 2) - even_counter.most_common()[1][1]
	if odd_diff2 > even_diff2:
		print(int(odd_diff + even_diff2))
	else:
		print(int(odd_diff2 + even_diff))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
k = n//2
a = list(map(int, input().split()))
p = []
q = []
for i in range(k):
    p.append(a[2*i])
    q.append(a[2*i+1])
s = Counter(p).most_common()
t = Counter(q).most_common()
u, v = len(s), len(t)
ans = 0
if s[0][0] == t[0][0]:
    if u == 1 and v == 1:
        ans = k
    elif u == 1:
        ans = k - t[1][1]
    elif v == 1:
        ans = k - s[1][1]
    else:
        comp = max(s[1][1], t[1][1])
        ans = n - s[0][1] - comp
else:
    ans = n - s[0][1] - t[0][1]
print(ans)

Submission status: Accepted

Solution code:
_ = input()
N = list(map(int, input().split()))
Tong1 = [0 for i in range(int(1e5 + 7))]
Tong2 = [0 for i in range(int(1e5 + 7))]
for i in range(0, len(N), 2):
    Tong1[N[i]] += 1
for i in range(1, len(N), 2):
    Tong2[N[i]] += 1

Num_pos1 = Tong1.index(max(Tong1))
Num_pos2 = Tong2.index(max(Tong2))
if(Num_pos1 == Num_pos2):
    print(len(N) - max(Tong1) - max(sorted(Tong1, reverse=True)[1], sorted(Tong2, reverse=True)[1]))
else:
    print(len(N) - max(Tong1) - max(Tong2))

Submission status: Accepted

Solution code:
import collections
 
def answer(list,n):
    Even = collections.Counter(list[::2]).most_common(2)
    Odd = collections.Counter(list[1::2]).most_common(2)
 
    if Even[0][0] != Odd[0][0]:
        return n - Even[0][1] - Odd[0][1]
    else:
        if len(Even) == 1 or len(Odd) == 1:
            return n // 2
        else:
            return n - max(Even[0][1]+Odd[1][1],Even[1][1]+Odd[0][1])
 
N = int(input())
list = input().split(" ")
print(answer(list,N))

Submission status: Accepted

Solution code:
n = input()
l = input().split()
w0,w1 = dict(),dict()
flag = 0
for i in l:
  if flag % 2 == 0:
    if i not in w0.keys():w0[i] = 1
    else:w0[i] += 1
  else:
    if i not in w1.keys():w1[i] = 1
    else:w1[i] += 1
  flag += 1
sumv = sum(w0.values()) + sum(w1.values())
def get_val(w0):
  l = list()
  w0_maxval = max(w0.values())
  w0_maxkey = [i for i in w0.keys() if w0[i] == w0_maxval][0]
  l.append({w0_maxkey:w0_maxval})
  del w0[w0_maxkey]
  if len(w0) > 0:
    w0_secval = max(w0.values())
    w0_seckey = [i for i in w0.keys() if w0[i] == w0_secval][0]
  else:
    w0_secval = 0
    w0_seckey = ""
  l.append({w0_seckey:w0_secval})
  return l
w0r = get_val(w0)
w1r = get_val(w1)
max = 0 
for i in w0r:
  for j in w1r:
    if list(i.keys())[0] != list(j.keys())[0]:
      if max < list(i.values())[0]  +  list(j.values())[0] : max = list(i.values())[0]  +  list(j.values())[0] 
print(sumv - max)

Submission status: Accepted

Solution code:
import collections

def answer(list,n):
    Even = collections.Counter(list[::2]).most_common(2)
    Odd = collections.Counter(list[1::2]).most_common(2)

    if Even[0][0] != Odd[0][0]:
        return n - Even[0][1] - Odd[0][1]
    else:
        if len(Even) == 1 or len(Odd) == 1:
            return n // 2
        else:
            return n - max(Even[0][1]+Odd[1][1],Even[1][1]+Odd[0][1])

N = int(input())
list = input().split(" ")
print(answer(list,N))