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()]

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:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict, Counter


def solve():
    n = int(input())
    v = [int(i) for i in input().split()]
    d1 = Counter(v[::2])
    d2 = Counter(v[1::2])
    rd1 = defaultdict(list)
    rd2 = defaultdict(list)
    for k, v in d1.items():
        rd1[v].append(k)
    for k, v in d2.items():
        rd2[v].append(k)
    a1 = [i for i in sorted(rd1.keys(), reverse=True)]
    a2 = [i for i in sorted(rd2.keys(), reverse=True)]
    ans, half = n, (n // 2)
    la1, la2 = len(a1) - 1, len(a2) - 1
    for h in range(3):
        for i in range(3):
            ai1, ai2 = min(h, la1), min(i, la2)
            aa1, aa2 = rd1[a1[ai1]], rd2[a2[ai2]]
            s1, s2 = set(aa1), set(aa2)
            if len(s1) > 1 or len(s2) > 1 or (s1 - s2):
                ans = min(
                    ans, 2 * half - a1[ai1] - a2[ai2])
            else:
                ans = min(ans,
                          2 * half - a1[ai1] - a2[ai2] + min(a1[ai1], a2[ai2]))
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict, Counter


def solve():
    n = int(input())
    v = [int(i) for i in input().split()]
    d1 = Counter(v[::2])
    d2 = Counter(v[1::2])
    rd1 = defaultdict(list)
    rd2 = defaultdict(list)
    for k, v in d1.items():
        rd1[v].append(k)
    for k, v in d2.items():
        rd2[v].append(k)
    a1 = [i for i in sorted(rd1.keys(), reverse=True)]
    a2 = [i for i in sorted(rd2.keys(), reverse=True)]
    ans, half = n, (n // 2)
    la1 = len(a1) - 1
    la2 = len(a2) - 1
    for h in range(3):
        for i in range(3):
            ai1, ai2 = min(h, la1), min(i, la2)
            aa1, aa2 = rd1[a1[ai1]], rd2[a2[ai2]]
            s1, s2 = set(aa1), set(aa2)
            if len(s1) > 1 or len(s2) > 1 or (s1 - s2):
                ans = min(
                    ans, 2 * half - a1[ai1] - a2[ai2])
            else:
                ans = min(ans,
                          2 * half - a1[ai1] - a2[ai2] + min(a1[ai1], a2[ai2]))
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict, Counter


def solve():
    n = int(input())
    v = [int(i) for i in input().split()]
    d1 = Counter(v[::2])
    d2 = Counter(v[1::2])
    rd1 = defaultdict(list)
    rd2 = defaultdict(list)
    for k, v in d1.items():
        rd1[v].append(k)
    for k, v in d2.items():
        rd2[v].append(k)
    a1 = [i for i in sorted(rd1.keys(), reverse=True)]
    a2 = [i for i in sorted(rd2.keys(), reverse=True)]
    ans = n
    la1 = len(a1) - 1
    la2 = len(a2) - 1
    for h in range(3):
        for i in range(3):
            aa1 = rd1[a1[min(h, la1)]]
            aa2 = rd2[a2[min(i, la2)]]
            s1 = set(aa1)
            s2 = set(aa2)
            if len(s1) > 1 or len(s2) > 1 or (s1 - s2):
                ans = min(
                    ans, (n // 2) - a1[min(h, la1)] + (n // 2) - a2[min(i, la2)])
            else:
                ans = min(ans, (n // 2) - a1[min(h, la1)] + (n // 2) - a2[min(i, la2)]
                          + min(a1[min(h, la1)], a2[min(i, la2)]))
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict


def solve():
    n = int(input())
    v = [int(i) for i in input().split()]
    d1 = defaultdict(int)
    d2 = defaultdict(int)
    for i in range(n):
        if i % 2 == 0:
            d1[v[i]] += 1
        else:
            d2[v[i]] += 1
    rd1 = defaultdict(list)
    rd2 = defaultdict(list)
    for k, v in d1.items():
        rd1[v].append(k)
    for k, v in d2.items():
        rd2[v].append(k)
    a1 = [i for i in sorted(rd1.keys(), reverse=True)]
    a2 = [i for i in sorted(rd2.keys(), reverse=True)]
    ans = n
    la1 = len(a1) - 1
    la2 = len(a2) - 1
    for h in range(3):
        for i in range(3):
            aa1 = rd1[a1[min(h, la1)]]
            aa2 = rd2[a2[min(i, la2)]]
            s1 = set(aa1)
            s2 = set(aa2)
            if len(s1) > 1 or len(s2) > 1 or (s1 - s2) or (s2 - s1):
                ans = min(
                    ans, (n // 2) - a1[min(h, la1)] + (n // 2) - a2[min(i, la2)])
            else:
                ans = min(ans, (n // 2) - a1[min(h, la1)] + (n // 2) - a2[min(i, la2)]
                          + min(a1[min(h, la1)], a2[min(i, la2)]))
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import math
import collections

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

even_list = nums[0::2]
odd_list = nums[1::2]

even_mod = collections.Counter(even_list).most_common(2)
odd_mod = collections.Counter(odd_list).most_common(2)

if even_mod[0][0] == odd_mod[0][0]:
    if len(even_mod) == 1:
        print(int(n / 2))
    else:
        print(n - even_mod[0][1] - max(even_mod[1][1], odd_mod[1][1]))
elif even_mod[0][0] != odd_mod[0][0]:
    for i in range(0, len(nums)):
        if i % 2 == 0:
            if nums[i] != even_mod[0][0]:
                ans1 += 1
        else:
            if nums[i] != odd_mod[0][0]:
                ans1 += 1
    print(ans1)
else:
    for i in range(0, len(nums)):
        if i % 2 == 0:
            if nums[i] != even_mod[0][0]:
                ans1 += 1
        else:
            if nums[i] != odd_mod[1][1]:
                ans1 += 1

    for i in range(0, len(nums)):
        if i % 2 == 0:
            if nums[i] != even_mod[1][1]:
                ans2 += 1
        else:
            if nums[i] != odd_mod[0][0]:
                ans2 += 1
    if ans1 < ans2:
        print(ans1)
    else:
        print(ans2)

Submission status: Accepted

Solution code:
MAX_V = 10 ** 5


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

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

    no, ne = [i for i in range(MAX_V+1)], [i for i in range(MAX_V+1)]
    no.sort(key=lambda val: odd[val], reverse=True)
    ne.sort(key=lambda val: even[val], reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

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

print(n - keep)

Submission status: Accepted

Solution code:
#abc111c
from collections import Counter

n=int(raw_input())
v=map(int,raw_input().split())
a,b=[],[]
for i in xrange(0,n,2):
 a.append(v[i])
 b.append(v[i+1])
acnt=Counter(a)
bcnt=Counter(b)
x=acnt.most_common()[0][0]
y=bcnt.most_common()[0][0]
if x!=y:
 res=n-acnt.most_common()[0][1]-bcnt.most_common()[0][1]
else:
 if acnt.most_common()[0][1]==n/2:
  res=n-bcnt.most_common()[0][1]
 elif bcnt.most_common()[0][1]==n/2:
  res=n-acnt.most_common()[0][1]
 else:
  res=min(n-acnt.most_common()[0][1]-bcnt.most_common()[1][1],n-bcnt.most_common()[0][1]-acnt.most_common()[1][1])
print res

Submission status: Accepted

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

cnt_arr = [dict() for _ in range(2)]

for i in range(n):
    cnt_arr[i % 2][v[i]] = cnt_arr[i % 2].get(v[i], 0) + 1

v_even = sorted(list(cnt_arr[0].items()), key=lambda x: x[1], reverse=True)[:2]  # [value, 出現数] * 2（上位2）
v_odd = sorted(list(cnt_arr[1].items()), key=lambda x: x[1], reverse=True)[:2]  # [value, 出現数] * 2（上位2）

keep = 0
flg = False
for even in v_even:  # [0]: 数値, [1]: 出現数
    for odd in v_odd:  # [0]: 数値, [1]: 出現数
        if even[0] != odd[0]:
            flg = True
            keep = max(keep, even[1] + odd[1])

ans = n - keep if flg else n // 2
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

# import ipdb; ipdb.set_trace()

if np.unique(v).size == 1:
    print(int(n/2))
else:
    # 偶数要素と奇数要素に分ける
    vo = v[::2]
    ve = v[1::2]
    # それぞれについて最大数の要素を見つける
    vof = np.bincount(vo).argmax()
    vef = np.bincount(ve).argmax()

    if vof == vef:
        if sum(vo == vof) > sum(ve == vef):
            vef = np.bincount(ve[ve!=vef]).argmax()
        elif sum(ve == vef) > sum(vo == vof):
            vof = np.bincount(vo[vo!=vof]).argmax()
        else :
            vof2 = np.bincount(vo[vo!=vof]).argmax()
            vef2 = np.bincount(ve[ve!=vef]).argmax()
            if (sum(vo == vof) + sum(ve == vef2)) > (sum(ve == vef) + sum(vo == vof2)):
                vef = vef2
            else:
                vof = vof2
    
    # それぞれについて最大数の数では無い要素数を数える
    vonf = sum(vo != vof)
    venf = sum(ve != vef)
    # 足しあげる
    print(vonf+venf)

Submission status: Accepted

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

from collections import Counter
import operator
def f():
    a = Counter(A[::2]).most_common(2)
    b = Counter(A[1::2]).most_common(2)
    ac, bc = a[0][1], 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:
# -*- coding: utf-8 -*-

from collections import Counter


n = int(input())
v = list(map(int, input().split()))
odd = Counter(v[0::2]).most_common(2) + [(0, 0)]
even = Counter(v[1::2]).most_common(2) + [(0, 0)]

n_half = int(n / 2)

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

print(ans)

Submission status: Accepted

Solution code:
import sys

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

odddict = {}
evendict = {}
for i,vi in enumerate(v):
    if i % 2 == 0: 
        if vi in evendict.keys():
            evendict[vi] += 1
        else:
            evendict[vi] = 1
    else:
        if vi in odddict.keys():
            odddict[vi] += 1
        else:
            odddict[vi] = 1
            
odd = sorted(odddict.items(), key=lambda x:x[1], reverse=True)
even = sorted(evendict.items(), key=lambda x:x[1], reverse=True)

if odd == even and len(odd)==1:
    print(n // 2)
    sys.exit()

i, j = 0,0
while odd[i] == even[j]:
    o_dif = odd[i+1][1] - odd[i][1]
    e_dif = even[j+1][1] - even[j][1]
    if o_dif >= e_dif:
        i += 1
    else:
        j += 1
  
print(n - odd[i][1] - even[j][1])

Submission status: Accepted

Solution code:
import itertools

n = int(input())
v = [int(w) for w in input().split()]
assert len(v) == n

def elem_count(ls):
    '''Return the list of (num of elem, elem).'''
    ls.sort()
    ls = itertools.groupby(ls)
    cs = [(len(list(s)), e) for e, s in ls]
    cs.sort(reverse=True)
    return cs

e = [v[i] for i in range(0, n, 2)]
o = [v[i] for i in range(1, n, 2)]

e = elem_count(e)
o = elem_count(o)

candidate = []
if e[0][1] != o[0][1]:
    candidate.append(e[0][0] + o[0][0])
if len(e) > 1:
    candidate.append(e[1][0] + o[0][0])
if len(o) > 1:
    candidate.append(e[0][0] + o[1][0])
if len(e) == len(o) == 1:
    candidate.append(e[0][0])

print(n - max(candidate))

Submission status: Accepted

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

a = []
b = []

a_dic = {}
b_dic = {}

for i in range(n):
    if i % 2 == 0:
        if v[i] in a_dic.keys():
            a_dic[v[i]] += 1
        else:
            a_dic[v[i]] = 1
    else:
        if v[i] in b_dic.keys():
            b_dic[v[i]] += 1
        else:
            b_dic[v[i]] = 1

max_a = 0
max_b = 0

max_a_ind = 0
max_b_ind = 0
 
for i, val in a_dic.items():
    if val > max_a:
        max_a = val
        max_a_ind = i

for i, val in b_dic.items():
    if val > max_b:
        max_b = val
        max_b_ind = i

n_2 = int(n / 2)

ans = (n_2 - max_a) + (n_2 - max_b)

if (len(a_dic) == 1 and len(b_dic) == 1) and a_dic.keys() ==  b_dic.keys():
    print(n_2)

elif max_a_ind == max_b_ind:
    list_a = sorted(list(a_dic.values()), reverse=True)
    list_b = sorted(list(b_dic.values()), reverse=True)
    if list_a[0] > list_b[0]:
        ans = n_2 - list_a[0] + n_2 - list_b[1]
    elif list_a[0] < list_b[0]:
        ans = n_2 - list_a[1] + n_2 - list_b[0]
    else:
        ans = n_2 - list_a[0] + n_2 - max(list_a[1], list_b[1])
    print(ans)

else:
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = [int(i) for i in input().split()]
v1 = Counter(v[::2])
v2 = Counter(v[1::2])
v1mc = v1.most_common()
v2mc = v2.most_common()
if(v1mc[0][0] == v2mc[0][0]):
    if(len(v1mc) == 1 and len(v2mc) == 1):
        print(int(n/2))
    elif(len(v1mc) == 1):
        print(n - (v1mc[0][1]+v2mc[1][1]))
    elif(len(v2mc) == 1):
        print(n - (v2mc[0][1]+v1mc[1][1]))
    else:
        print(n - max(v1mc[0][1]+v2mc[1][1], v2mc[0][1]+v1mc[1][1]))
else:
    print(n - (v1mc[0][1]+v2mc[0][1]))

Submission status: Accepted

Solution code:
import sys
from collections import Counter
Input = sys.stdin.readline

n = int(Input())
v = list(map(int, Input().split()))
mi = float("inf")
count1 = 0
count2 = 0

v1 = v[0::2]
v1_count = Counter(v1)
v1_count = v1_count.most_common()
count1 += len(v1) - v1_count[0][1]

v2 = v[1::2]
v2_count = Counter(v2)
v2_count = v2_count.most_common()
count2 += len(v2) - v2_count[0][1]

if v1_count[0][0] != v2_count[0][0]:
    mi = count1 + count2
else:
    lv2 = len(v2)
    for k, i in v2_count[1:]:
        result = count1 + lv2 - i
        if mi > result:
            mi = result
    else:
        if mi > count1 + lv2:
            mi = count1 + lv2

    lv1 = len(v1)
    for k, i in v1_count[1:]:
        result = count2 + lv1 - i
        if mi > result:
            mi = result
    else:
        if mi > count2 + lv1:
            mi = count2 + lv1

print(mi)

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int,input().split()))
A = []
B = []
Q = [0,0]
R = [0,0]
S = [0,0]
T = [0,0]
J = 1
K = 1
for i in range(n):
  if (i%2 == 0):
    A.append(L[i])
  else:
    B.append(L[i])
A.sort()
B.sort()
A.append(-1)
B.append(-1)
for j in range(1,len(A)):
  if (A[j-1] == A[j]):
    J += 1
  else:
    if (J > Q[0]):
      Q[1] = Q[0]
      del Q[0]
      Q.insert(0,J)
      R[1] = R[0]
      del R[0]
      R.insert(0,A[j-1])
      j += J
      J = 1
    elif (Q[0] >= J > Q[1]):
      del Q[1]
      Q.append(J)
      del R[1]
      R.append(A[j-1])
      j += J
      J = 1
    else:
      j += J
      J = 1
for k in range(1,len(B)):
  if (B[k-1] == B[k]):
    K += 1
  else:
    if (K > S[0]):
      S[1] = S[0]
      del S[0]
      S.insert(0,K)
      T[1] = T[0]
      del T[0]
      T.insert(0,B[k-1])
      k += K
      K = 1
    elif (S[0] >= K > S[1]):
      del S[1]
      S.append(K)
      del T[1]
      T.append(B[k-1])
      k += K
      K = 1
    else:
      k += K
      K = 1
if (R[0] != T[0]):
  print(n-Q[0]-S[0])
else:
  print(n-max(S[0]+Q[1],S[1]+Q[0]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
e = []
o = []
for i in range(n):
    if i % 2 == 0:
        e.append(v[i])
    else:
        o.append(v[i])
e_counter = Counter(e)
o_counter = Counter(o)
e_most = e_counter.most_common()
o_most = o_counter.most_common()
E1 = e_most[0][0]
E2 = E1
if len(e_most) > 1:
    E2 = e_most[1][0]
O1 = o_most[0][0]
O2 = O1
if len(o_most) > 1:
    O2 = o_most[1][0]
e_cnt = n // 2 - e.count(E1)
o_cnt = n // 2 - o.count(O1)
if e_cnt == 0 and o_cnt == 0:
    if e[0] == o[0]:
        print(n // 2)
    else:
        print(0)
else:
    if E1 != O1:
        print(e_cnt + o_cnt)
    else:
        if E1 == O2:
            e_cnt = n // 2 - e.count(E2)
            print(e_cnt + o_cnt)
        elif E2 == O1:
            o_cnt = n // 2 - o.count(O2)
            print(e_cnt + o_cnt)
        else:
            cnt1 = n // 2 - e.count(E2) + o_cnt
            cnt2 = e_cnt + n // 2 - o.count(O2)
            print(min(cnt1, cnt2))

Submission status: Accepted

Solution code:
# N個のスペース区切りの数値を取得
N = int(input())
A = [int(N) for N in input().split()]


# 奇数番目と偶数番目に出てくる数字の頻度順にそれぞれソートして、
odd = []
even = []

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

import collections
c_odd = collections.Counter(odd)
c_even = collections.Counter(even)
#print(c_odd.most_common()[0][0], c_even.most_common()[0][0])



# 最頻な数字が一致してない場合はそれぞれの最頻値以外の合計値、
if c_odd.most_common()[0][0] != c_even.most_common()[0][0] :
    #print(len(set(odd))-1 + len(set(even))-1)
    print(N - c_odd.most_common()[0][1] - c_even.most_common()[0][1])
    exit()


# 一致してる場合は片方が最頻を諦めた場合の合計値を両パターン求めて小さい方
if c_odd.most_common()[0][0] == c_even.most_common()[0][0] :

    # 偶数と奇数の数列が完全一致のパターンでないかチェック
    if len(c_odd.most_common()) == 1 and len(c_even.most_common()) == 1:
        print(N//2)

    # oddが1種類しかない場合
    elif len(c_odd.most_common()) == 1:
        print(N//2 - c_even.most_common()[1][1])
    # evenが1種類しかない場合
    elif len(c_even.most_common()) == 1:
        print(N//2 - c_odd.most_common()[1][1])
    #両方が2種類以上で構成されている場合
    else:
        ans = N - max(c_odd.most_common()[0][1]+c_even.most_common()[1][1], c_odd.most_common()[1][1]+c_even.most_common()[0][1] )
        print(ans)

'''
        # 最頻を諦める ＝ 2番めに頻出な数値を採用
        temp_odd_retire = N//2 - c_odd.most_common()[1][1] #個数
        temp_even_retire = N//2 - c_even.most_common()[1][1] #個数
        #print('temps are ', temp_odd_retire, temp_even_retire)

        # oddを諦めたほうが良い場合
        if temp_odd_retire < temp_even_retire:
            print(temp_odd_retire + N//2 - c_even.most_common()[0][1])
        # even()
        else:
            print(temp_even_retire + N//2 - c_odd.most_common()[0][1])
        exit()
'''

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
#全体の最頻値が奇数のみ or 偶数のみの最頻値とは限らない！
def get_sorted_num(v):
	num_v = {}
	for v_ in v:
		if v_ not in num_v:
			num_v[v_]  = 1
		else:
			num_v[v_] += 1
	sorted_num_v = sorted(num_v.items(), key=lambda x:x[1])
	return sorted_num_v

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

if len(get_sorted_num(v)) == 1:
	print n / 2
	sys.exit()

sorted_num_odd = get_sorted_num(v[0::2])
sorted_num_even = get_sorted_num(v[1::2])

odd_1st_k, odd_1st_v = sorted_num_odd[-1]
even_1st_k, even_1st_v = sorted_num_even[-1]

	
if odd_1st_k == even_1st_k:
	if len(sorted_num_odd) == 1:
		print len(v[0::2]) - odd_1st_v + len(v[1::2]) - even_2nd_v
	elif len(sorted_num_even) == 1:
		print len(v[0::2]) - odd_2nd_v + len(v[1::2]) - even_1st_v
	else:
		odd_2nd_k, odd_2nd_v = sorted_num_odd[-2]
		even_2nd_k, even_2nd_v = sorted_num_even[-2]
		if odd_2nd_v > even_2nd_v:
			print len(v[0::2]) - odd_2nd_v + len(v[1::2]) - even_1st_v
		else:
			print len(v[0::2]) - odd_1st_v + len(v[1::2]) - even_2nd_v
else:
	print len(v[0::2]) - odd_1st_v + len(v[1::2]) - even_1st_v

Submission status: Accepted

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

eo = [{}, {}]
# 0: even 1: odd
for i in range(len(V_list)):
    v = V_list[i]

    tmp = (i + 1) % 2
    eo[tmp][v] = eo[tmp].get(v, 0) + 1


eomax = [max(eo[i].values()) for i in range(2)]
maxkv = [[(k, v) for k, v in eo[i].items() if v == eomax[i]] for i in range(2)]

for ke, ve in maxkv[0]:
    flag = False
    for ko, vo in maxkv[1]:
        if(ke != ko):
            ans = n - ve - vo
            flag = True
            break
    if(flag is True):
        break
else:
    sec = [0, 0]
    for i in range(2):
        for v in eo[i].values():
            if(v >= eomax[i]):
                continue
            sec[i] = max(sec[i], v)
    ans = min(n - eomax[0] - sec[1], n - eomax[1] - sec[0])
    # print(ans)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

def fParse(iN,aV):
    oV0 = Counter(aV[0::2]).most_common(2)
    oV1 = Counter(aV[1::2]).most_common(2)
    oV0 += [(0,0)]
    oV1 += [(0,0)]

    if oV0[0][0] != oV1[0][0] :
        return iN - oV0[0][1] - oV1[0][1]
    else:
        return iN - max(oV0[0][1],oV1[0][1]) - max(oV0[1][1],oV1[1][1])

if __name__ == "__main__":
    iN = int(input())
    aV = [int(_) for _ in input().split()]
    print(fParse(iN,aV))

Submission status: Accepted

Solution code:
from collections import Counter

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

oV0 = Counter(aV[0::2]).most_common(2)
oV1 = Counter(aV[1::2]).most_common(2)
oV0 += [(0,0)]
oV1 += [(0,0)]

if oV0[0][0] != oV1[0][0] :
    print(iN - oV0[0][1] - oV1[0][1])
else:
    print(iN - max(oV0[0][1],oV1[0][1]) - max(oV0[1][1],oV1[1][1]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int,input().split()))
o= v[0::2]
e =v[1::2]
o_mode = Counter(o).most_common(2)
e_mode = Counter(e).most_common(2)
if o_mode[0][0] == e_mode[0][0]:
  if len(o_mode) == 1:
    print(int(n/2)) 
  else:
    print(n-max(o_mode[1][1],e_mode[1][1])-o_mode[0][1])
else:
  print(n-o_mode[0][1]-e_mode[0][1])

Submission status: Accepted

Solution code:
from collections import Counter

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

oV0 = Counter(aV[0::2]).most_common()
oV1 = Counter(aV[1::2]).most_common()
oV0 += [(0,0)]
oV1 += [(0,0)]

if oV0[0][0] != oV1[0][0] :
    print(iN - oV0[0][1] - oV1[0][1])
else:
    print(iN - max(oV0[0][1],oV1[0][1]) - max(oV0[1][1],oV1[1][1]))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())

index = 0
even_numbers = []
odd_numbers = []

for i in input().split():
    if index % 2 == 0:
        even_numbers.append(int(i))
    else:
        odd_numbers.append(int(i))
    index += 1

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

if even[0][0] == odd[0][0]:
    if len(even) == 1:
        print(N//2)
    else:
        print(N - even[0][1] - max(even[1][1], odd[1][1]))
else:
    print(N - even[0][1] - odd[0][1])

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
a = [0 for i in range(10**6)]
b = [0 for i in range(10**6)]
a_i = 0
b_i = 0
i = 0
for e in l:
    if i%2 == 0:
        a[e]+=1
        if(a[a_i]<=a[e]):
            a_i=e
    else:
        b[e]+=1
        if(b[b_i]<=b[e]):
            b_i=e
    i+=1
a.sort()
a.reverse()
b.sort()
b.reverse()
if a_i != b_i:
    print(n-a[0]-b[0])
else:
    print(n-max(a[0]+b[1],a[1]+b[0]))

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import collections

n = int(input())
v = [int(i) for i in input().split()]
INF = 10 ** 6

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

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

c1 = collections.Counter(v1)
c1[INF] = 0
c2 = collections.Counter(v2)
c2[INF] = 0

sc1 = sorted(c1.items(), key=lambda x: x[1], reverse=True)
a1, b1 = sc1[0][0], sc1[1][0]
sc2 = sorted(c2.items(), key=lambda x: x[1], reverse=True)
a2, b2 = sc2[0][0], sc2[1][0]

total = sum(c1.values()) + sum(c2.values())
if a1 == a2:
    ans = min(total - c1[a1] - c2[b2], total - c1[b1] - c2[a2])
else:
    ans = total - c1[a1] - c2[a2]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


def solve(V):
    n = len(V)
    odd_max = Counter(V[1::2]).most_common(2)
    even_max = Counter(V[::2]).most_common(2)

    if odd_max[0][0] != even_max[0][0]:
        return n - odd_max[0][1] - even_max[0][1]
    elif  len(odd_max) == 1 and len(even_max) == 1:
        return n // 2
    elif len(odd_max) != 1 and len(even_max) == 1:
        return n - odd_max[1][1] - even_max[0][1]
    elif len(odd_max) == 1 and len(even_max) != 1:
        return n - odd_max[0][1] - even_max[1][1]
    else:
        return n - max(even_max[0][1] + odd_max[1][1],
                       even_max[1][1] + odd_max[0][1])

if __name__ == "__main__":
    n = int(input())
    l = input()
    print(solve(l.split(" ")))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = [int(_) for _ in input().split()]
v1 = []
v2 = []
for i in range(n):
    if i % 2 == 0:
        v1.append(v[i])
    else:
        v2.append(v[i])
v_count = Counter(v)
v1_count = Counter(v1)
v2_count = Counter(v2)
# print("v1_count", v1_count)
# print("v2_count", v2_count)
v1_comm = v1_count.most_common(1)
v2_comm = v2_count.most_common(1)
sols = []
# print("v1_comm", v1_comm)
# print("v2_comm", v2_comm)
if len(v_count) == 1:
    print(n//2)
else:
    key1 = v1_comm[0][1]
    # print("v1_comm[0][0]", v1_comm[0][0])
    # print("v2_comm[0][0]", v2_comm[0][0])
    if v1_comm[0][0] == v2_comm[0][0]:
        # print("hoge")
        v2_comm = v2_count.most_common(2)
        key2 = v2_comm[1][1]
    else:
        key2 = v2_comm[0][1]
    sols.append(n-key1-key2)
    key1 = v2_comm[0][1]
    if v1_comm[0][0] == v2_comm[0][0]:
        v1_comm = v1_count.most_common(2)
        key2 = v1_comm[1][1]
    else:
        key2 = v1_comm[0][1]
    sols.append(n-key1-key2)
    print(min(sols))

Submission status: Accepted

Solution code:
from collections import Counter

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

"""
まずは偶数番目、奇数番目をカウントする
次に数を調べる
2個目から先を足す
"""

#forループを回さなくても解ける方法があるよ。
#listの要素を取るときに[::2]とすると、２個おきにとってきてくれる
odd = Counter(v[1::2]).most_common()
even = Counter(v[0::2]).most_common()

#most_comon()は、（要素、出現回数）のリストを返してくれる

if odd[0][0] != even[0][0]:
	answer = n - odd[0][1] - even[0][1]

else:
	if len(odd) == 1 and len(even) >1:
		answer = n - even[0][1]
	elif len(even) == 1 and len(odd) >1:
		answer = n - odd[0][1]
	elif len(even) == 1 and len(odd) ==1:
		answer = n - even[0][1]
	else:		
		if odd[0][1] > even[0][1]:
			answer = n - odd[0][1] - even[1][1]
		elif odd[0][1] < even[0][1]:
			answer = n - even[0][1] - odd[1][1]
		else:
			if odd[1][1] > even[1][1]:
				answer = n - even[0][1] - odd[1][1]
			elif odd[1][1] < even[1][1]:
				answer = n - odd[0][1] - even[1][1]
			else:
				answer = n - odd[0][1] - even[1][1]
		
print(answer)

"""
半分waになってしまった...なにがいけなかったんだろう
"""

Submission status: Accepted

Solution code:
import collections

n = int(input())
v = list(map(int,input().split(" ")))
e,o = [],[]
e = v[::2]
o = v[1::2]

ec = collections.Counter(e).most_common(2)
oc = collections.Counter(o).most_common(2)

if ec[0][0] != oc[0][0]:
    print(n - ec[0][1] - oc[0][1])
else:
    if len(ec) == 1:
        print(n // 2)
    else:
        print(n - max(ec[0][1] + oc[1][1], ec[1][1] + oc[0][1]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter
from operator import itemgetter
def solve():
    input()
    V = list(map(int, input().split()))
    l = sorted(list(Counter(V[::2]).items())+[(0,0)], key=itemgetter(1), reverse=True)
    u = sorted(list(Counter(V[1::2]).items())+[(0,0)], key=itemgetter(1), reverse=True)
    res = sum(map(itemgetter(1), l+u)) - (max(l[0][1]+u[1][1], l[1][1]+u[0][1]) if l[0][0]==u[0][0] else (l[0][1]+u[0][1]))
    return str(res)

if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
from collections import Counter


def solve(V):
    n = len(V)
    ac = Counter(V[::2]).most_common(2)
    bc = Counter(V[1::2]).most_common(2)

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


if __name__ == "__main__":
    n = int(input())
    l = input()
    print(solve(l.split(" ")))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = list(map(int,input().split()))
oddA = A[0::2]
evenA = A[1::2]

oc = Counter(oddA)
omode = oc.most_common()
ec = Counter(evenA)
emode = ec.most_common()
if omode[0][0] != emode[0][0]:
    print(n-omode[0][1]-emode[0][1])
else:
    if len(omode) == 1:
        omode.append((0, 0))
    if len(emode) == 1:
        emode.append((0, 0))
    a = n-omode[0][1]-emode[1][1]
    b = n-omode[1][1]-omode[0][1]
    print(min(a, b))

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]))