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 defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys



def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n = I()
num = LI()
even_cnt = Counter([num[i] for i in range(n) if i%2]).most_common()
odd_cnt = Counter([num[i] for i in range(n) if i%2==0]).most_common()

odd_cnt += [(0, 0)]
even_cnt += [(0, 0)]


if len(odd_cnt) == 1:
    print(n - odd_cnt[0][1] - even_cnt[1][1])
elif even_cnt[0][0] != odd_cnt[0][0]:
    print(n - even_cnt[0][1] - odd_cnt[0][1])
else:
    print(min(n - even_cnt[0][1] - odd_cnt[1][1], n - even_cnt[1][1] - odd_cnt[0][1]))

Submission status: Accepted

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

from collections import defaultdict

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

for i in range(N//2):
    j = 2*i
    k = 2*i + 1
    even[v[j]][0] += 1
    odd[v[k]][0] += 1

even_sorted = sorted(even, key=lambda x:x[0], reverse = True)
odd_sorted = sorted(odd, key=lambda x:x[0], reverse = True)

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    v = [int(i) for i in input().split()]
    v_even = v[::2]
    v_odd = v[1::2]
    from collections import Counter
    v_even_cnt = sorted(list(Counter(v_even).items()),key=lambda x:x[1],reverse=True)
    v_odd_cnt = sorted(list(Counter(v_odd).items()),key=lambda x:x[1],reverse=True)
    ans = 0
    if v_even_cnt[0][0] != v_odd_cnt[0][0]:
        ans = len(v_even) - v_even_cnt[0][1] + len(v_odd) - v_odd_cnt[0][1]
    else:
        if len(v_even_cnt) == 1:
            ans = len(v_even)
        else:
            ans1 = len(v_even) - v_even_cnt[0][1] + len(v_odd) - v_odd_cnt[1][1]
            ans2 = len(v_even) - v_even_cnt[1][1] + len(v_odd) - v_odd_cnt[0][1]
            ans = min(ans1,ans2)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
V = list(map(int, input().split()))
odd, even = {}, {}
for i in range(N):
    if i % 2 == 1:
        if V[i] not in even:
            even[V[i]] = 0
        even[V[i]] += 1
    else:
        if V[i] not in odd:
            odd[V[i]] = 0
        odd[V[i]] += 1

sorted_odd = sorted(odd.items(), key=lambda x: x[1], reverse=True)
sorted_even = sorted(even.items(), key=lambda x: x[1], reverse=True)
if sorted_odd[0][0] != sorted_even[0][0]:
    print(N - sorted_even[0][1] - sorted_odd[0][1])
else:
    if len(sorted_odd) == 1 or len(sorted_even) == 1:
        print(N // 2)
    else:
        print(N - max(sorted_even[0][1] + sorted_odd[1][1], sorted_even[1][1] + sorted_odd[0][1]))

Submission status: Accepted

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

n, = map(int, input().split())
data = getInputListInt()

onedic = {0:0}
twodic = {0:0}

for i in range(n):
    if i % 2 ==0:
        onedic[data[i]]=0
    else:
        twodic[data[i]]=0

for i in range(n):
    if i % 2 ==0:
        onedic[data[i]]+=1
    else:
        twodic[data[i]]+=1

onemax = max(onedic, key=onedic.get)
twomax = max(twodic, key=twodic.get)

onemax = sorted(onedic.items(), key = lambda x:x[1], reverse=True)
twomax = sorted(twodic.items(), key = lambda x:x[1], reverse=True)

ans = 0
if onemax[0][0] != twomax[0][0]:
    ans = n - onemax[0][1] - twomax[0][1]
else:
    ans = min(n - onemax[0][1] - twomax[1][1], n - onemax[1][1] - twomax[0][1])

print(ans)

Submission status: Accepted

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

from collections import Counter

v1 = []
v2 = []
for i in range(0,n-1,2):
  v1.append(V[i])
for i in range(1,n,2):
  v2.append(V[i])
  
c1 = Counter(v1).most_common() + [(0,0)]
c2 = Counter(v2).most_common() + [(0,0)]

if c1[0][0] == c2[0][0]:
  a1 = n -c1[1][1]-c2[0][1]
  a2 = n -c1[0][1]-c2[1][1]
  ans = min(a1,a2)
  
else:
  ans = n-c1[0][1]-c2[0][1]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

def abc(v):
  n = len(v)
  v_e = Counter(v[::2]).most_common(2)
  v_o = Counter(v[1::2]).most_common(2)
  if v_e[0][0] != v_o[0][0]:
    return n - v_e[0][1] - v_o[0][1]
  else:
    if len(v_o)==1 or len(v_e)==1:
      return n//2
    else:
      return n - max(v_e[0][1] + v_o[1][1], v_e[1][1] + v_o[0][1])
    
input()
print(abc(list(map(int, input().split()))))

Submission status: Accepted

Solution code:
n = int(input())
v = [int(x) for x in input().split()]
dic0 = {}
dic1 = {}
for i in range(n):
  if i % 2 == 0:
    if v[i] in dic0:
      dic0[v[i]] += 1
    else:
      dic0[v[i]] = 1
  else:
    if v[i] in dic1:
      dic1[v[i]] += 1
    else:
      dic1[v[i]] = 1
list0 = sorted(dic0.items(), key = lambda x : x[1])
list1 = sorted(dic1.items(), key = lambda x : x[1])
l0 = len(list0) - 1
l1 = len(list1) - 1

if list0[l0][0] != list1[l1][0]:
  print(n - list0[l0][1] - list1[l1][1])
else:
  if l0 == 0 and l1 == 0:
    print(n // 2)
  elif l0 == 0:
    print(n - max(list0[0][1] + list1[l1 - 1][1], list1[l1][1]))
  elif l1 == 0:
    print(n - max(list0[l0 - 1][1] + list1[0][1], list0[l0][1]))
  else:
    print(n - max(list0[l0 - 1][1] + list1[l1][1], list0[l0][1] + list1[l1 - 1][1]))

Submission status: Accepted

Solution code:
n = int(input())
v = [int(x) for x in input().split()]
v_1 = {}
v_2 = {}
k = 1
for i in v:
    if k == 1:
        if i in v_1:
            v_1[i] = v_1[i]+1
        else:
            v_1[i] = 1
    else:
        if i in v_2:
            v_2[i] = v_2[i]+1
        else:
            v_2[i] = 1
    k = -k

v_11 = sorted(v_1.items(), key=lambda x:x[1], reverse=True)
v_12 = sorted(v_2.items(), key=lambda x:x[1], reverse=True)

a1, b1 = v_11[0][1], v_12[0][1]
if len(v_11) == 1:
    a2 = -1
else:
    a2 = v_11[1][1]
if len(v_12) == 1:
    b2 = -1
else:
    b2 = v_12[1][1]
if v_11[0][0] != v_12[0][0]:
    ans = n-a1-b1
elif a2 == -1:
    if b2 == -1:
        ans = n-n//2
    else:
        ans = n-a1-b2
else:
    if b2 == -1:
        ans = n-a2-b1
    else:
        ans = min(n-a2-b1,n-a1-b2)
print(ans)

Submission status: Accepted

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

A = [[0] * (10**5+1) for i in range(2)]

for i in range(n):
    A[i%2][V[i]] += 1

C = [[0] * (10**5+1) for i in range(2)]  # A[0][:i], A[1][:i]の最大値
for i in range(1, len(C[0])):
    C[0][i] = max(C[0][i-1], A[0][i])
    C[1][i] = max(C[1][i-1], A[1][i])

# print(A[0][:150])
# print(A[1][:150])
# print(C[:150])

ans = 0
for i in range(1, len(C[0])):
    ans = max(ans, C[1][i-1] + A[0][i])
    ans = max(ans, C[0][i-1] + A[1][i])

print(n - ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

f = Counter(v[::2]).most_common(2)
s = Counter(v[1::2]).most_common(2)

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

Submission status: Accepted

Solution code:
import sys
import math
from fractions import gcd
# import queue
from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""

# 奇数側と偶数側でリストを分ける
# それぞれの最大値で置き換える
# 数値が一致してしまった時にどうするか
# 出現数順にソート、辞書を持つ
# 一致している場合とそうでない場合で分ける
# 療法試してmax取る


N =  int(sys.stdin.readline())
L =  list(map(int, sys.stdin.readline().split()))

E = L[::2]
O = L[1::2]

ce = Counter(E)
co = Counter(O)

cem = ce.most_common()
com = co.most_common()

if cem[0] != com[0]:
    print(N - cem[0][1] - com[0][1])
else:
    if len(cem) == 1 or len(com) == 1:
        print(N // 2)
    else:
        print(min(N - (cem[0][1] + com[1][1]), N - (cem[1][1] + com[0][1])))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
s = list(map(int, input().split()))
evenL = []
oddL = []
for i, v in enumerate(s):
  if i % 2 == 0:
    evenL.append(v)
  else:
    oddL.append(v)
    
evenLc = Counter(evenL)
oddLc = Counter(oddL)

evenLcs = sorted(evenLc.items(), key=lambda x: -x[1])
oddLcs = sorted(oddLc.items(), key=lambda x: -x[1])

if evenLcs[0][0] != oddLcs[0][0]:
  print(n - evenLcs[0][1] - oddLcs[0][1])
else:
  if len(evenLc) == 1 and len(oddLc) == 1:
    print(n - len(evenL))
    exit()
  if evenLcs[0][1] > oddLcs[0][1]:
    print(n - evenLcs[0][1] - oddLcs[1][1])
  elif evenLcs[0][1] < oddLcs[0][1]:
    print(n - evenLcs[1][1] - oddLcs[0][1])
  else:
    print(n - evenLcs[0][1] - max(evenLcs[1][1], oddLcs[1][1]))

Submission status: Accepted

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

    dic1 = {0:0}
    for i in range(0, n, 2):
        if v[i] in dic1:
            dic1[v[i]] += 1
        else:
            dic1[v[i]] = 1
    
    dic2 = {0:0}
    for i in range(1, n, 2):
        if v[i] in dic2:
            dic2[v[i]] += 1
        else:
            dic2[v[i]] = 1
    
    dic1 = sorted(list(dic1.items()), key=lambda x: -x[1])
    dic2 = sorted(list(dic2.items()), key=lambda x: -x[1])
    # print(dic1)
    # print(dic2)
    if dic1[0][0] != dic2[0][0]:
        print(n-dic1[0][1]-dic2[0][1])
    else:
        print(min(n-dic1[0][1]-dic2[1][1], n-dic1[1][1]-dic2[0][1]))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
vvv = na()
even = vvv[::2]
odd = vvv[1::2]
d_even = {}
d_odd = {}
for i in range(n//2):
    if even[i] not in d_even.keys():
        d_even[even[i]] = 1
    else:
        d_even[even[i]] += 1

    if odd[i] not in d_odd.keys():
        d_odd[odd[i]] = 1
    else:
        d_odd[odd[i]] += 1

e_max = max(d_even, key=d_even.get)
o_max = max(d_odd, key=d_odd.get)
e1 = d_even[e_max]
o1 = d_odd[o_max]

if e_max == o_max:
    d_even[e_max] = 0
    d_odd[o_max] = 0

    e2 = max(d_even.values())
    o2 = max(d_odd.values())

    ans = n - max(e1 + o2, e2 + o1)

else:
    ans = n - e1 - o1

print(ans)

Submission status: Accepted

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

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

b_dic = {}
c_dic = {}

for i in range(n//2):
  if b[i] in b_dic.keys():
    b_dic[b[i]] += 1
  else:
    b_dic[b[i]] = 1
    
  if c[i] in c_dic.keys():
    c_dic[c[i]] += 1
  else:
    c_dic[c[i]] = 1
    
b_max = max(b_dic, key=b_dic.get)
c_max = max(c_dic, key=c_dic.get)

if b_max == c_max:
  m_b = b_dic[b_max]
  m_c = c_dic[c_max]
  
  b_dic[b_max] = 0
  c_dic[c_max] = 0
  
  b_max2 = max(b_dic, key=b_dic.get)
  c_max2 = max(c_dic, key=c_dic.get)

  ans = n - max(m_b + c_dic[c_max2], b_dic[b_max2] + m_c)
  
else:
  ans = n - b_dic[b_max] - c_dic[c_max]
  
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections

n = int(input())

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

v_odd = v[1::2]
v_even = v[0::2]
c_odd = collections.Counter(v_odd)
c_even = collections.Counter(v_even)

if c_odd.most_common()[0][0] != c_even.most_common()[0][0]:
    print(n - c_odd.most_common()[0][1] - c_even.most_common()[0][1])
else:
    if len(c_even.most_common()) == 1:
        print(n - c_odd.most_common()[0][1])
    elif len(c_odd.most_common()) == 1:
        print(n - c_even.most_common()[0][1])
    else:
        print(min(n - c_odd.most_common()[0][1] - c_even.most_common()[1][1],
                  n - c_odd.most_common()[1][1] - c_even.most_common()[0][1]))

Submission status: Accepted

Solution code:

from collections import Counter

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

# 奇数
O = V[0::2]
#print(O)

# 偶数
E = V[1::2]
#print(E)

co = Counter(O)
ce = Counter(E)

# これをいれとかないと、最頻値が１しか無い時に２つめを取得できなくなる
co[0] = 0
ce[0] = 0

co1 = co.most_common(1)
ce1 = ce.most_common(1)

co2 = co.most_common(2)
ce2 = ce.most_common(2)

if co1[0][0] == ce1[0][0]:
    print(min(N - co1[0][1] - ce2[1][1], N - co2[1][1] - ce1[0][1]))
else:
    print(N - co1[0][1] - ce1[0][1])

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
vs = list(map(int, input().split()))
a = vs[::2]
b = vs[1::2]

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

Submission status: Accepted

Solution code:
from collections import Counter


n = int(input())
vs = list(map(int, input().split()))
a = vs[::2]
b = vs[1::2]

ca = Counter(a)
cb = Counter(b)
ca[0] = 0
cb[0] = 0
if ca.most_common(1)[0][0] != cb.most_common(1)[0][0]:
    print(n - ca.most_common(1)[0][1] - cb.most_common(1)[0][1])
else:
    print(min(
        n - ca.most_common(1)[0][1] - cb.most_common(2)[1][1],
        n - ca.most_common(2)[1][1] - cb.most_common(1)[0][1]
    ))
    # if len(set(vs)) == 1:
    #     print(n // 2)
    # else:
    #     print(min(
    #         n - ca.most_common(1)[0][1] - cb.most_common(2)[1][1],
    #         n - ca.most_common(2)[1][1] - cb.most_common(1)[0][1]
    #     ))

Submission status: Accepted

Solution code:
from collections import Counter
 
N = int(input())
V = [i for i in input().split()]
 
odd = Counter(V[::2]).most_common(2)
even = Counter(V[1::2]).most_common(2)

if odd[0][0] != even[0][0]:
    print(N - odd[0][1] - even[0][1])
elif len(odd) == 1:
    print(N // 2)
else:
    o0e1 = N - odd[0][1] - even[1][1]
    o1e0 = N - odd[1][1] - even[0][1]
    print(min(o0e1, o1e0))

Submission status: Accepted

Solution code:
import collections,sys
n=int(input())
v=list(map(int,input().split()))
a=[]
b=[]
for i in range(0,n,2):
  a.append(v[i-1])
  b.append(v[i])
c=collections.Counter(a)
d=collections.Counter(b)
C=sorted(c.items(), key=lambda x:-x[1])
D=sorted(d.items(), key=lambda x:-x[1])
A=int(C[0][0])
B=int(D[0][0])
cnt=0
if len(set(v))==1:
  print(n//2)
  sys.exit()
if A!=B:
  for i in range(n//2):
    if a[i]!=A:
      cnt+=1
  for j in range(n//2):
    if b[j]!=B:
      cnt+=1
  print(cnt)
  sys.exit()
if A==B:
  totla=0
  A_=int(C[1][0]) if len(set(a))!=1 else A
  for i in range(n//2):
    if a[i]!=A_:
      totla+=1
  for j in range(n//2):
    if b[j]!=B:
      totla+=1
  totlb=0
  B_=int(D[1][0]) if len(set(b))!=1 else B
  for i in range(n//2):
    if a[i]!=A:
      totlb+=1
  for j in range(n//2):
    if b[j]!=B_:
      totlb+=1
  print(min(totla,totlb))
  sys.exit()

Submission status: Accepted

Solution code:
n1 = int(input())

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

n3 = []
n4 = []
for i1 in range(n1):
    if i1 % 2 == 0:
        n3.append(n2[i1])
    else:
        n4.append(n2[i1])

n3.sort()
n4.sort()

t1 = 0
c1 = 0
n5 = []

for i2 in range(len(n3)):
    if n3[i2] == t1:
        c1 += 1
        if i2 == len(n3) - 1:
            n5.append([t1, c1])
    else:
        if i2 != 0:
            n5.append([t1, c1])
        t1 = n3[i2]
        c1 = 1
        if i2 == len(n3) - 1:
            n5.append([t1, c1])

t1 = 0
c1 = 0
n6 = []
for i3 in range(len(n4)):
    if n4[i3] == t1:
        c1 += 1
        if  i3 == len(n4) - 1:
            n6.append([t1, c1])
    else:
        if i3 != 0:
            n6.append([t1, c1])
        t1 = n4[i3]
        c1 = 1
        if  i3 == len(n4) - 1:
            n6.append([t1, c1])

n5.sort(key=lambda x:(x[1],x[0]), reverse=True)
n6.sort(key=lambda x:(x[1],x[0]), reverse=True)

r1 = 0

if len(n5) == 1 and len(n6) == 1 and n5[0][0] == n6[0][0]:
     r1 = n1 // 2
else:
    if n5[0][0] != n6[0][0]:
        r1 = n1 - n5[0][1] - n6[0][1]
    else:
        if len(n5) == 1:
            r1 = n1 - n5[0][1] - n6[1][1]
        elif len(n6) == 1:
            r1 = n1 - n5[1][1] - n6[0][1]
        else:
            if (n1 - n5[0][1] - n6[1][1]) >= (n1 - n5[1][1] - n6[0][1]):
                r1 = r1 = n1 - n5[1][1] - n6[0][1]
            else:
                r1 = n1 - n5[0][1] - n6[1][1]

print(r1)

Submission status: Accepted

Solution code:
from collections import defaultdict


def resolve():
    n = int(input())
    v = list(map(int, input().split()))
    d_o = defaultdict(int)
    d_e = defaultdict(int)
    for i in range(n):
        if i % 2:
            d_o[v[i]] += 1
        else:
            d_e[v[i]] += 1
    d_e[0] = 0
    d_o[0] = 0
    de = sorted(d_e.items(), key=lambda x: -x[1])
    do = sorted(d_o.items(), key=lambda x: -x[1])
    if de[0][0] != do[0][0]:
        print(n - de[0][1] - do[0][1])
    else:
        p01 = de[0][1] + do[1][1]
        p10 = de[1][1] + do[0][1]
        print(n - max(p01, p10))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
V = list(map(int, input().split()))
A = {}
B = {}
for i in range(n):
  if i % 2 == 0:#A
    A[V[i]] = A.get(V[i], 0) + 1
  else:#B
    B[V[i]] = B.get(V[i], 0) + 1

A = sorted(A.items(), key=lambda x:x[1], reverse=True)
B = sorted(B.items(), key=lambda x:x[1], reverse=True)
a1, b1 = A[0][1], B[0][1]
am, bm = A[0][0], B[0][0]
ans = n - a1 - b1
if ans == 0 and V[0]==V[1]:
  ans = n//2
elif am == bm and n > 4:
  a2,b2 = A[1][1], B[1][1]
  ans = min(n - a2 - b1, n - a1 - b2) 
print(ans)

Submission status: Accepted

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

from collections import Counter

n = I()
v = III()

if v==[v[0]]*len(v):
    print(len(v)//2)
    exit()

x = [v[i] for i in range(0,n,2)]
y = [v[i] for i in range(1,n,2)]

xc = Counter(x)
yc = Counter(y)
xval = xc.most_common(1)[0][0]
yval = yc.most_common(1)[0][0]
xnum = xc.most_common(1)[0][1]
ynum = yc.most_common(1)[0][1]

if xval!=yval:
    print(len(x)-xnum+len(y)-ynum)
else:
    if xnum==len(x):
        print(min(len(x)+len(y)-ynum, len(x)-xnum+len(y)-yc.most_common(2)[1][1]))
    elif ynum==len(y):
        print(min(len(x)-xnum+len(y), len(x)-xc.most_common(2)[1][1]+len(y)-ynum))
    else:
        print(min(len(x)-xc.most_common(2)[1][1]+len(y)-ynum, len(x)-xnum+len(y)-yc.most_common(2)[1][1]))

Submission status: Accepted

Solution code:
n=int(input())
v=[int(x) for x in input().split()]
v_e=v[::2]
v_o=v[1::2]
vote_e={}
vote_o={}
vote_e[-1]=0
vote_o[-1]=0

for i in range(n//2):
    vote_e[v_e[i]] = vote_e[v_e[i]] + 1 if v_e[i] in vote_e else 1
    vote_o[v_o[i]] = vote_o[v_o[i]] + 1 if v_o[i] in vote_o else 1

vote_e=sorted(vote_e.items(), reverse=True, key=lambda x:x[1])
vote_o=sorted(vote_o.items(), reverse=True, key=lambda x:x[1])
popular_e=vote_e[0][1]
popular_o=vote_o[0][1]
if vote_e[0][0]==vote_o[0][0]:
    if vote_e[0][1]+vote_o[1][1]>vote_o[0][1]+vote_e[1][1]:
        popular_o = vote_o[1][1]
    else:
        popular_e = vote_e[1][1]
print(n-popular_o-popular_e)

Submission status: Accepted

Solution code:
import collections

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

    even = [j for i, j in enumerate(v) if i % 2 == 0]
    odd = [j for i, j in enumerate(v) if i % 2 == 1]

    cnt_e = collections.Counter(even)
    cnt_o = collections.Counter(odd)

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

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


main()

Submission status: Accepted

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

odd = []
even = []

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

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

odd1 = oddm.most_common()[0]
even1 = evenm.most_common()[0]

if (odd1 == even1) and (oddm.most_common()[0][1] + evenm.most_common()[0][1] == n):
  ans = n//2
else:
  if odd1 != even1:
    ans = n - oddm.most_common()[0][1] - evenm.most_common()[0][1] 
  else:
    p1 = ans = n - oddm.most_common()[1][1] - evenm.most_common()[0][1] 
    p2 = ans = n - oddm.most_common()[0][1] - evenm.most_common()[1][1] 
    ans = min(p1, p2)
    
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
V = [int(el) for el in input().split(' ')]

a = Counter(V[0::2]).most_common() + [(0,0)]
b = Counter(V[1::2]).most_common() + [(0,0)]

if a[0][0]!=b[0][0]:
    print(n-a[0][1]-b[0][1])
else:
    print(n-max(a[1][1]+b[0][1],a[0][1]+b[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() + [(0,0)]
b = Counter(V[1::2]).most_common() + [(0,0)]
if a[0][0]!=b[0][0]:
    print(N-a[0][1]-b[0][1])
else:
    print(N-max(a[1][1]+b[0][1],a[0][1]+b[1][1]))

Submission status: Accepted

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

def main():
    N = int(input())
    V1 = {}
    V2 = {}
    V = list(map(int, input().split()))
    for i,v in enumerate(V):
        if i%2 == 0:
            V1[v] = V1[v] + 1 if v in V1 else 1
        else:
            V2[v] = V2[v] + 1 if v in V2 else 1

    V1 = sorted(V1.items(), key=lambda x:x[1], reverse=True)
    V2 = sorted(V2.items(), key=lambda x:x[1], reverse=True)

    if V1[0] != V2[0]:
        ans = 0
        for v in V1[1:]:
            ans += v[1]
        for v in V2[1:]:
            ans += v[1]
        print(ans)
    else:
        a1 = 0
        for v in V1[1:]:
            a1 += v[1]
        a1 += V2[0][1]
        for v in V2[2:]:
            a1 += v[1]
        a2 = 0
        a2 += V1[0][1]
        for v in V1[2:]:
            a2 += v[1]
        for v in V2[1:]:
            a2 += v[1]
        print(min(a1,a2))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc111/tasks/arc103_a
# C - /\/\/\/
import collections

n = int(input().split()[0])
v_list = list(map(int, input().split()))

# 奇数番目の数
odd_list = [str(v) for i, v in enumerate(v_list) if i % 2 == 1]
# 偶数番目の数
even_list = [str(v) for i, v in enumerate(v_list) if i % 2 == 0]

# 奇数の最頻値（No1とNo2)
odd_counter = collections.Counter(odd_list)
odd_1 = odd_counter.most_common()[0][0]

if len(odd_counter) >= 2:
    odd_2 = odd_counter.most_common()[1][0]
else:
    odd_2 = '*' # dummy

# 偶数の最頻値（No1とNo2)
even_counter = collections.Counter(even_list)
even_1 = even_counter.most_common()[0][0]

if len(even_counter) >= 2:
    even_2 = even_counter.most_common()[1][0]
else:
    even_2 = '+' # dummy

# 以前の方法だと条件分岐が複雑になるので
# とりうる操作を全部試して、回数が少ない方をとる

if even_1 == odd_1:
    # 偶数側の操作回数（最頻値で置き換え）
    even_op_count = len([v for v in even_list if v != even_1])
    # 奇数の操作回数
    odd_op_count = len([v for v in odd_list if v != odd_2])
    count_1 = even_op_count + odd_op_count

    # 偶数側の操作回数
    even_op_count = len([v for v in even_list if v != even_2])
    # 奇数側の操作回数（最頻値で置き換え）
    odd_op_count = len([v for v in odd_list if v != odd_1])
    count_2 = even_op_count + odd_op_count

    count = min(count_1, count_2)
else:
    # 偶数側も奇数側も最頻値で置き換えればよい
    even_op_count = len([v for v in even_list if v != even_1])
    odd_op_count = len([v for v in odd_list if v != odd_1])
    count = even_op_count + odd_op_count

print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

odd = defaultdict(int)
even = defaultdict(int)
al = defaultdict(int)

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

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

even_sorted = sorted(even.items(), key=lambda x: x[1], reverse=True)
odd_sorted = sorted(odd.items(), key=lambda x: x[1], reverse=True)

#print(even_sorted[0:4])
#print(odd_sorted[0:4])


if(len(even_sorted) == 1): # what if either of them had only one uniq num..
  if(odd_sorted[0][0] == even_sorted[0][0]):
    print(n - odd_sorted[1][1]  - even_sorted[0][1])
  else:
    print(n - odd_sorted[0][1] - even_sorted[0][1])
elif(len(odd_sorted) == 1):
  if(odd_sorted[0][0] == even_sorted[0][0]):
    print(n - even_sorted[1][1] - odd_sorted[0][1])
  else:
    print(n - odd_sorted[0][1] - even_sorted[0][1])


else: # both of them have more than 1 uniq num..
  if(odd_sorted[0][0] == even_sorted[0][0]):
    if(odd_sorted[0][1] == even_sorted[0][1]):
      if(odd_sorted[1][1] < even_sorted[1][1]):
        print(n - odd_sorted[0][1] - even_sorted[1][1])
      else:
        print(n - odd_sorted[1][1] - even_sorted[0][1])
    elif(odd_sorted[0][1] < even_sorted[0][1]): # odd to be changed (even stays)
      print(n - odd_sorted[1][1] - even_sorted[0][1])
    else:
      print(n - odd_sorted[0][1] - even_sorted[1][1])
  else: # both has more than 1 uniq num, and most freq values aren't the same
    print(n - odd_sorted[0][1] - even_sorted[0][1])

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    V = [int(v) for v in input().split()]
    even = dict()
    odd = dict()
    for i in range(n//2):
        even[V[2*i]] = even.get(V[2*i], 0) + 1
        odd[V[2*i+1]] = odd.get(V[2*i+1], 0) + 1
    max_count_even = max(even.values())
    max_count_odd = max(odd.values())
    max_v_even = [v for v in even if even[v] == max_count_even]
    max_v_odd = [v for v in odd if odd[v] == max_count_odd]

    if len(max_v_even) == len(max_v_odd) == 1 and max_v_even[0] == max_v_odd[0]:
        del even[max_v_even[0]]
        del odd[max_v_odd[0]]
        if even:
            max_count_even_nxt = max(even.values())
        else:
            max_count_even_nxt = 0
        if odd:
            max_count_odd_nxt = max(odd.values())
        else:
            max_count_odd_nxt = 0
        return min(n-max_count_even-max_count_odd_nxt, n-max_count_odd-max_count_even_nxt)
    else:
        return n - max_count_even - max_count_odd


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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
a = Counter(v[::2])
b = Counter(v[1::2])
count = a.most_common()
count1 = b.most_common()
if len(set(v)) == 1:
    print(n//2)
    exit()
if count[0][0] == count1[0][0]:
    print(n- count[0][1] - (max(count[1][1], count1[1][1])))

else:
    print(n - count[0][1] - count1[0][1])

Submission status: Accepted

Solution code:
from collections import Counter

n,*l = map(int, open(0).read().split())

c0 = Counter(l[::2]).most_common() + [(0, 0)]
c1 = Counter(l[1::2]).most_common() + [(0, 0)]

if c0[0][0] != c1[0][0]:
  print(n - c0[0][1] - c1[0][1])
else:
  print(n - max(c0[1][1] + c1[0][1], c0[0][1] + c1[1][1]))

Submission status: Accepted

Solution code:
n = int(input())
V = list(map(int, input().split()))
#まず偶奇分けして二つの配列にします
Vodd = []
Veven = []
for i in range(n//2):
  Vodd.append(V[2*i])
  Veven.append(V[2*i+1])
#それぞれの配列の最頻と2番目を探します
import collections
Codd = collections.Counter(Vodd)
Ceven = collections.Counter(Veven)
cmlstodd = Codd.most_common()
cmlsteven = Ceven.most_common()
#print(cmlstodd, cmlsteven)
#仕方ないのでここからは地道に場合分けします
if len(cmlstodd)==1:
  if cmlstodd[0][0] == cmlsteven[0][0]:
    if len(cmlsteven)==1:
      print(n//2)
    else:
      print(n//2 - cmlsteven[1][1])
  else:
    print(n//2 - cmlsteven[0][1])
elif len(cmlsteven)==1:
  if cmlsteven[0][0] == cmlstodd[0][0]:
    print(n//2 - cmlstodd[1][1])
  else:
    print(n//2 - cmlstodd[0][1])
else:
  if cmlsteven[0][0] == cmlstodd[0][0]:
    print(n - max(cmlsteven[0][1] + cmlstodd[1][1], cmlsteven[1][1] + cmlstodd[0][1]))
  else:
    print(n - cmlstodd[0][1] - cmlsteven[0][1])