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 math import factorial,sqrt,ceil #,gcd
# from itertools import permutations,combinations,combinations_with_replacement
from collections import deque,Counter
# from bisect import bisect_left
# from heapq import heappush,heappop
# from numba import njit
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from fractions import gcd

# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np    # numpy.lcm()
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb,perm #permはnPk
# import networkx as nx
# G = Graph()

# slist = "abcdefghijklmnopqrstuvwxyz"
N = int(input())
lisA = list(map(int,input().split()))

kv1 = Counter(lisA[0:N:2]).most_common()
kv2 = Counter(lisA[1:N:2]).most_common()

if len(kv1)==len(kv2)==1 and kv1[0]==kv2[0]:
    print(N//2)
    exit()

ans1 = N
ans2 = N
for i in range(0,N//2-1):
    try:
        if kv1[0][0]!=kv2[i][0]:
            ans1 = min(ans1,N-(kv1[0][1]+kv2[i][1]))
        if kv1[i][0]!=kv2[0][0]:
            ans2 = min(ans2,N-(kv1[i][1]+kv2[0][1]))
    except:
        pass

print(min(ans1,ans2))
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

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

from collections import Counter, defaultdict
e, o = Counter(), Counter()
for i in range(n):
    if i % 2 == 0:
        e[v[i]] += 1
    else:
        o[v[i]] += 1

e_inv = defaultdict(list)
for key in e:
    e_inv[n // 2 - e[key]].append(key)
e_inv[n // 2].append(0)

o_inv = defaultdict(list)
for key in o:
    o_inv[n // 2 - o[key]].append(key)
o_inv[n // 2].append(0)

e_inv_keys = sorted(list(e_inv.keys()))
o_inv_keys = sorted(list(o_inv.keys()))

l1, l2 = e_inv[e_inv_keys[0]], o_inv[o_inv_keys[0]]
if len(l1) == 1 and len(l2) == 1 and l1 == l2:
    ans = min(e_inv_keys[0] + o_inv_keys[1], e_inv_keys[1] + o_inv_keys[0])
else:
    ans = e_inv_keys[0] + o_inv_keys[0]

print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
V=list(map(int,input().split()))
l=N//2
V_odd=[]
V_eve=[]
for i in range(N):
  if i%2==0:
    V_odd.append(V[i])
  else:
    V_eve.append(V[i])

p_odd = list(collections.Counter(V_odd).most_common())
p_eve = list(collections.Counter(V_eve).most_common())
p_odd.append([0,0])
p_eve.append([0,0])

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

Submission status: Accepted

Solution code:
import collections
N=int(input())
V=list(map(int,input().split()))
l=N//2
V_odd=[]
V_eve=[]
for i in range(N):
  if i%2==0:
    V_odd.append(V[i])
  else:
    V_eve.append(V[i])

p_odd = list(collections.Counter(V_odd).most_common())
p_eve = list(collections.Counter(V_eve).most_common())
p_odd.append([0,0])
p_eve.append([0,0])

if p_odd[0][0]!=p_eve[0][0]:
  print(N-p_odd[0][1]-p_eve[0][1])
  exit()
else:
  print(min(N-p_odd[0][1]-p_eve[1][1],N-p_odd[1][1]-p_eve[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int,input().split()))
a = []
b = []
for i in range(n):
    if i % 2 == 0:
        a.append(v[i])
    else:
        b.append(v[i])
c1 = Counter(a).most_common()
c2 = Counter(b).most_common()
if len(set(a)) == len(set(b)) == 1:
    if a == b:
        print(len(a))
    else:
        print(0)
else:
    if c1[0][0] != c2[0][0]:
        print(n - c1[0][1] - c2[0][1])
    else:
        print(min(n-c1[0][1] -c2[1][1],n - c1[1][1] - c2[0][1]))

Submission status: Accepted

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
# from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

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

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

def MS():
    return input().split()

def RS():
    return list(input().split())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input().split()) for _ in range(n) ]

n = I()
v = RI()

ans1 = 0
ans2 = 0

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

ac = Counter(a)
bc = Counter(b)

ma = 0
mb = 0

for i in ac:
    if ma<ac[i]:
        ma = ac[i]
        no1 = i
    else:
        pass

for i in bc:
    if mb<bc[i] and i!=no1:
        mb = bc[i]
        no2 = i
    else:
        pass

ans1 += len(a) - ma
ans1 += len(b) - mb

ma = 0
mb = 0

for i in bc:
    if mb<bc[i]:
        mb = bc[i]
        no2 = i
    else:
        pass

for i in ac:
    if ma<ac[i] and i!=no2:
        ma = ac[i]
        no1 = i
    else:
        pass

ans2 += len(a) - ma
ans2 += len(b) - mb

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
n = int(input())
v_list = list(map(int,input().split()))
v_list_odd = v_list[::2]
v_list_ev = v_list[1::2]
import collections
c_odd = collections.Counter(v_list_odd)
c_even = collections.Counter(v_list_ev)

c_odd = sorted(c_odd.items(),key=lambda x:x[1],reverse = True)
c_even = sorted(c_even.items(),key=lambda x:x[1],reverse = True)
if c_odd[0][0] != c_even[0][0]:
  print(n - c_odd[0][1] - c_even[0][1])
else:
  if len(c_odd) == 1 and len(c_even) == 1:
    print(n - c_odd[0][1])
  elif len(c_odd) == 1:
    print(n - c_odd[0][1] - c_even[1][1])
  elif len(c_even) == 1:
    print(n - c_odd[1][1] - c_even[0][1])
  else:
    print(n - max(c_odd[1][1] + c_even[0][1],c_odd[0][1] + c_even[1][1]))

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
v1 = sorted(v[0::2])
v2 = sorted(v[1::2])
s1 = set(v1)
s2 = set(v2)

eve = [[0,0]]
odd = [[0,0]]
cnt_eve = 1
cnt_odd = 1

for i in range(1,n//2):
    ## 偶数項
    if v1[i] == v1[i-1]:
        cnt_eve += 1
    else:
        eve.append([cnt_eve, v1[i-1]])
        cnt_eve = 1
    ## 奇数項
    if v2[i] == v2[i-1]:
        cnt_odd += 1
    else:
        odd.append([cnt_odd, v2[i-1]])
        cnt_odd = 1
    if i == n//2 - 1:
        eve.append([cnt_eve, v1[i]])
        odd.append([cnt_odd, v2[i]])


eve.sort(key=lambda x: x[0], reverse=True)
odd.sort(key=lambda x: x[0], reverse=True)

if eve[0][1] != odd[0][1]:
    ans = n - eve[0][0] - odd[0][0]
    print(ans)
else:
    ans = n - max(eve[0][0] + odd[1][0], eve[1][0] + odd[0][0])
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
v1 = sorted(v[0::2])
v2 = sorted(v[1::2])
s1 = set(v1)
s2 = set(v2)

## 偶数項・奇数項ごとに最大数および次点の値を保持
eve = [[0,0]]
odd = [[0,0]]
cnt_eve = 1
cnt_odd = 1

for i in range(1,n//2):
    ## 偶数
    if v1[i] == v1[i-1]:
        cnt_eve += 1
    else:
        eve.append([cnt_eve, v1[i-1]])
        cnt_eve = 1
    if v2[i] == v2[i-1]:
        cnt_odd += 1
    else:
        odd.append([cnt_odd, v2[i-1]])
        cnt_odd = 1
    if i == n//2 - 1:
        eve.append([cnt_eve, v1[i]])
        odd.append([cnt_odd, v2[i]])


eve.sort(key=lambda x: x[0], reverse=True)
odd.sort(key=lambda x: x[0], reverse=True)

if eve[0][1] != odd[0][1]:
    ans = n - eve[0][0] - odd[0][0]
    print(ans)
else:
    ans = n - max(eve[0][0] + odd[1][0], eve[1][0] + odd[0][0])
    print(ans)

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


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

# v1 : 0,2,4... 番目の要素からなるリスト
# v2 : 1,3,5... 番目の要素からなるリスト
v1 = V[0::2]
v2 = V[1::2]

# それぞれ, 要素の出現回数を集計する.
# 一番出現回数が多い数字 以外 を書き換えるのが良さそう
from collections import Counter
c1 = Counter(v1).most_common()
c2 = Counter(v2).most_common()

# 数字が1つしかない場合に"2番目"をダミーで追加
c1.append((0,0))
c2.append((0,0))

# 残したい数字が v1,v2 で一致した場合, どちらかは次点を採用せざるを得ない
if c1[0][0]==c2[0][0]:
    print(min(N-(c1[0][1]+c2[1][1]), N-(c1[1][1]+c2[0][1])))
else:
    print(N- (c1[0][1]+c2[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter

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

V_even = V[: : 2]
V_odd = V[1: : 2]

D_even = Counter(V_even)
D_odd = Counter(V_odd)

ans = 0
even, odd = D_even.most_common(2), D_odd.most_common(2)
if even[0][0] != odd[0][0]:
    ans = n - even[0][1] - odd[0][1]
else:
    if len(D_even) == len(D_odd) == 1:
        ans = n // 2
    else:
        ans = min(
            n - even[1][1] - odd[0][1],
            n - even[0][1] - odd[1][1]
        )

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
odd_common = Counter(a[::2]).most_common()
even_common = Counter(a[1::2]).most_common()
odd_common.append(('', 0))
even_common.append(('', 0))
if odd_common[0][0] != even_common[0][0]:
    print(n - odd_common[0][1] - even_common[0][1])
else:
    b = max(odd_common[0][1] + even_common[1][1], odd_common[1][1] + even_common[0][1])
    print(n - b)

Submission status: Accepted

Solution code:
def count(l):
    odd = {}
    even = {}
    for idx, d in enumerate(l):
        if idx%2 == 0:
            res = even
        else:
            res = odd
        if d in res:
            res[d] += 1
        else:
            res[d] = 1
    return odd, even
n = int(input())
data = list(map(int, input().split()))
odd, even = count(data)
odd = sorted(odd.items(), key=lambda x:-x[1])
even = sorted(even.items(), key=lambda x:-x[1])
if len(odd) == 1 and len(even) == 1:
    if odd[0][0] != even[0][0]:
        print(0)
    else:
        print(min(odd[0][1], even[0][1]))
else:
    size = n//2
    if odd[0][0] != even[0][0]:
        ans = 2*size - odd[0][1] - even[0][1]
        print(ans)
    else:
        ans1 = 2*size - odd[0][1] - even[1][1]
        ans2 = 2*size - odd[1][1] - even[0][1]
        print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = list(map(int,input().split()))
odd = []
even = []
for i in range(N):
    if i % 2 == 1:
        odd.append(A[i])
    else:
        even.append(A[i])
Dic_odd = collections.Counter(odd)
Dic_eve = collections.Counter(even)
M_odd = Dic_odd.most_common()
M_eve = Dic_eve.most_common()
#print(M_odd, M_eve)
if M_odd[0][0] != M_eve[0][0]:
    ans = len(odd) - M_odd[0][1] + len(even) - M_eve[0][1]
else:
    if len(M_odd) == 1 and len(M_eve) == 1:
        ans = min(len(odd), len(even))
    elif len(M_odd) == 1:
        ans = min(len(even) - M_eve[1][1], len(even) - M_eve[0][1] + len(odd))
    elif len(M_eve) == 1:
        ans = min(len(odd) - M_odd[1][1], len(odd) - M_odd[0][1] + len(even))
    else:
        ans = len(odd) + len(even) - max(M_odd[0][1] + M_eve[1][1], M_odd[1][1] + M_eve[0][1])
print(ans)

Submission status: Accepted

Solution code:
import itertools
from collections import Counter
N  = int(input())
V = [int(x) for x in input().split()]
ev_cnt = Counter(V[::2])
od_cnt = Counter(V[1::2])
# 1種のみ
ev_cnt[-1] = 0
od_cnt[-2] = 0
# 最頻値の上位２種のみ使用
ev = ev_cnt.most_common(2)
od = od_cnt.most_common(2)
ans = N
for (k1,v1), (k2,v2) in itertools.product(ev,od):
    if k1==k2:
        continue
    x = N - v1 - v2
    if ans > x:
        ans = x
print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
v=list(map(int,input().split()))
v1=v[0::2]
v2=v[1::2]
c1=collections.Counter(v1).most_common()
c2=collections.Counter(v2).most_common()
if c1[0][0] != c2[0][0]:
	if len(c1) == 1 and len(c2) == 1:
		print(0)
		exit()
	elif len(c1) == 1:
		print(n//2-c2[0][1])
		exit()
	elif len(c2) == 1:
		print(n//2-c1[0][1])
		exit()
	else:
		print(n-c1[0][1]-c2[0][1])
		exit()
else:
	#最頻値が同じ
	if len(c1) == 1 and len(c2) == 1:
		print(n//2)
		exit()
	elif len(c1) == 1:
		print(n//2-c2[1][1])
		exit()
	elif len(c2) == 1:
		print(n//2-c1[1][1])
		exit()
	else:
		print(min(n-c1[1][1]-c2[0][1],n-c1[0][1]-c2[1][1]))
		exit()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
even = {}
odd = {}

for i in range(n):
  if i % 2 == 0:
    if a[i] in even:
      even[a[i]] += 1
    else:
      even[a[i]] = 1
  else:
    if a[i] in odd:
      odd[a[i]] += 1
    else:
      odd[a[i]] = 1
      
#print(even)
#print(odd)
emode = max(even, key=even.get)
omode = max(odd, key=odd.get)
emax = max(even.values())
omax = max(odd.values())

if emode != omode:
  print(n-emax-omax)
else:
  even[emode]=0
  odd[omode]=0
  emax2 = max(even.values())
  omax2 = max(odd.values())
  print(min(n-emax-omax2, n-emax2-omax))

Submission status: Accepted

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

d1 = [0]*(10**5+1)
d2 = [0]*(10**5+1)
for i in range(n):
    if i%2: d2[v[i]] += 1
    else: d1[v[i]] += 1
        
max_d1 = [0, 0]
max_d2 = [0, 0]
submax_d1 = [0, 0]
submax_d2 = [0, 0]
for i in range(10**5+1):
    if max_d1[1] < d1[i]:
        submax_d1 = deepcopy(max_d1)
        max_d1[0] = i
        max_d1[1] = d1[i]
    elif submax_d1[1] < d1[i]:
        submax_d1[0] = i
        submax_d1[1] = d1[i]
    if max_d2[1] < d2[i]:
        submax_d2 = deepcopy(max_d2)
        max_d2[0] = i
        max_d2[1] = d2[i]
    elif submax_d2[1] < d2[i]:
        submax_d2[0] = i
        submax_d2[1] = d2[i]
if max_d1[0] == max_d2[0]:
    if max_d1[1] + submax_d2[1] > max_d2[1]+submax_d1[1]:
        print(n-max_d1[1]-submax_d2[1])
    else:
        print(n-max_d2[1]-submax_d1[1])
else:
    print(n-max_d1[1]-max_d2[1])

Submission status: Accepted

Solution code:
# C - /\/\/\/

from collections import defaultdict
import sys
readline = sys.stdin.readline

n = int(readline())
v = list(int(x) for x in readline().split())

d0 = defaultdict(int)
d1 = defaultdict(int)
d0[0] = 0
d1[0] = 0
for i in range(n):
    if i%2:
        d1[v[i]] += 1
    else:
        d0[v[i]] += 1

d0 = list(d0.items())
d1 = list(d1.items())
d0.sort(key=lambda x: x[1], reverse=True)
d1.sort(key=lambda x: x[1], reverse=True)

if d0[0][0] != d1[0][0]:
    ans = n - d0[0][1] - d1[0][1]
else:
    ans = n - max(d0[0][1] + d1[1][1], d0[1][1] + d1[0][1])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


N = int(input())
V = list(map(int, input().split()))
V1 = []
V0 = []
for i, v in enumerate(V):
    if i % 2 == 0:
        V0.append(v)
    else:
        V1.append(v)

V1C = Counter(V1)
V0C = Counter(V0)
ans = 0

if V1C.most_common()[0][0] != V0C.most_common()[0][0]:
    ans = V1C.most_common()[0][1] + V0C.most_common()[0][1]
elif len(V1C) != 1 and len(V0C) != 1:
    ans = max(V1C.most_common()[1][1] + V0C.most_common()[0][1], V1C.most_common()[0][1] + V0C.most_common()[1][1])
elif len(V1C) != 1:
    ans = V1C.most_common()[1][1] + V0C.most_common()[0][1]
elif len(V0C) != 1:
    ans = V1C.most_common()[0][1] + V0C.most_common()[1][1]
else:
    ans = N // 2

print(N - ans)

Submission status: Accepted

Solution code:
n,*v=map(int,open(0).read().split())
a,b={},{}
for i,j in zip(v[::2],v[1::2]):	
  a[i]=a.get(i,0)+1
  b[j]=b.get(j,0)+1
A=list(a.items())
B=list(b.items())
A.sort(key=lambda x: x[1])
B.sort(key=lambda x: x[1])
c,d=A[-1][1],B[-1][1]
if len(set(v))==1:
    print(n//2)
    exit()
if A[-1][0]==B[-1][0]:
    if c==d:
        c=max(A[-2][1],B[-2][1])
    elif c>d:
        d=B[-2][1]
    else:c=A[-2][1]    
print(n-c-d)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import Counter 
def main():
    n = int(readline())
    a = [int(i) for i in readline().split()]
    odd = Counter(a[::2]).most_common(2)
    even = Counter(a[1::2]).most_common(2)
    odd.append((0, 0))
    even.append((0, 0))
    if odd[0][0] == even[0][0]:
        print(min(n-odd[1][1]-even[0][1], n-odd[0][1]-even[1][1]))
        return
    print(n-odd[0][1]-even[0][1])
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import Counter 
from collections import defaultdict
def main():
    n = int(readline())
    a = [int(i) for i in readline().split()]
    odd = Counter(a[::2]).most_common(2)
    even = Counter(a[1::2]).most_common(2)
    odd.append((-1, 0))
    even.append((-1, 0))
    if odd[0][0] == even[0][0]:
        print(min(n-odd[1][1]-even[0][1], n-odd[0][1]-even[1][1])) 
    else:
        print(n-odd[0][1]-even[0][1])
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
from collections import Counter
v_odd=Counter([v[i] for i in range(n) if i%2==0])
v_eve=Counter([v[i] for i in range(n) if i%2==1])
v_eve[-1]=0
v_odd[-1]=0

eve=[[k,v] for k,v in v_eve.items()]
odd=[[k,v] for k,v in v_odd.items()]
eve.sort(key=lambda x:x[1])
odd.sort(key=lambda x:x[1])

if eve[-1][0]!=odd[-1][0]:
  print(n-eve[-1][1]-odd[-1][1])
else:
  ans0=n-eve[-2][1]-odd[-1][1]
  ans1=n-eve[-1][1]-odd[-2][1]
  print(min(ans0,ans1))

Submission status: Accepted

Solution code:
from collections import Counter

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

odd = sorted(Counter(v[0:n:2]).items(), reverse=True, key=lambda x: x[1])
even = sorted(Counter(v[1:n+1:2]).items(), reverse=True, key=lambda x: x[1])

if odd[0][0] != even[0][0]:
    res = n - odd[0][1] - even[0][1]
elif len(odd) == 1:
    res = odd[0][1]
else:
    if odd[0][1] == even[0][1]:
        if odd[1][1] >= even[1][1]:
            res = n - odd[1][1] - even[0][1]
        else:
            res = n - odd[0][1] - even[1][1]
    elif odd[0][1] > even[0][1]:
        res = n - odd[0][1] - even[1][1]
    else:
        res = n - odd[1][1] - even[0][1]

print(res)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
data_1 = defaultdict(int)
data_2 = defaultdict(int)
v = list(map(int, input().split()))
if len(set(v)) == 1:
    print(n // 2)
    exit()
for i in range(n):
    if i % 2 == 0:
        data_1[v[i]] += 1
    else:
        data_2[v[i]] += 1
data_1 = sorted(data_1.items(), key = lambda x: x[1],reverse = True)
data_2 = sorted(data_2.items(), key = lambda x: x[1],reverse = True)

ans = n - data_1[0][1] - data_2[0][1]
if data_1[0][0] == data_2[0][0]:
    ans = n - max(data_1[0][1] + data_2[1][1], data_1[1][1] + data_2[0][1])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
L = list(map(int, input().split()))
L1 = L[::2]
L2 = L[1::2]
c1 = Counter(L1).most_common()
c2 = Counter(L2).most_common()
if len(set(L1)) == len(set(L2)) == 1:
    if L1[0] == L2[0]:
        print(len(L1))
    else:
        print(0)
else:
    if c1[0][0] != c2[0][0]:
        print(n - c1[0][1] - c2[0][1])
    else:
        print(min(n - c1[0][1] - c2[1][1], n - c1[1][1] - c2[0][1]))

Submission status: Accepted

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

c1 = Counter(v1)
c2 = Counter(v2)

c1 = sorted(c1.items(), key=lambda x:(x[1], -x[0]), reverse=True)
c2 = sorted(c2.items(), key=lambda x:(x[1], -x[0]), reverse=True)
ans = 0
if c1[0][0] != c2[0][0]:
    if len(c1) != 1:
        for k, v in c1[1:]:
            ans += v
    if len(c2) != 1:
        for k, v in c2[1:]:
            ans += v
    print(ans)
else:
    if len(c1) == 1 and len(c2) == 1:
        ans += n // 2
    elif len(c1) == 1 and len(c2) > 1:
        ans += len(v2) - c2[1][1]
    elif len(c1) > 1 and len(c2) == 1:
        ans += len(v1) - c1[1][1]
    else:
        if c1[1][1] < c2[1][1]:
            ans += len(v1) - c1[0][1]
            ans += len(v2) - c2[1][1]
        else:
            ans += len(v1) - c1[1][1]
            ans += len(v2) - c2[0][1]
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ue = [0]*10**6
sita = [0]*10**6
for i in range(n):
  if i % 2 == 0:
    ue[a[i]] += 1
  else:
    sita[a[i]] += 1

m_ue = max(ue)
m_ue_idx = ue.index(m_ue)
m_sita = max(sita)
m_sita_idx = sita.index(m_sita)
if m_ue_idx != m_sita_idx:
  print(n - m_ue - m_sita)
else:
  s_ue = sorted(ue, reverse=True)
  s_sita = sorted(sita, reverse=True)
  print(min(n-s_ue[0]-s_sita[1], n-s_ue[1]-s_sita[0]))

Submission status: Accepted

Solution code:
from operator import itemgetter
n = int(input())
even = {}
odd = {}
V = list(map(int,input().split()))
for i in range(n):
    if i % 2 == 0:
        if V[i] in even:
            even[V[i]] += 1
        else:
            even[V[i]] = 1
    else:
        if V[i] in odd:
            odd[V[i]] += 1
        else:
            odd[V[i]] = 1
E = sorted(even.items(),key=itemgetter(1))
O = sorted(odd.items(),key=itemgetter(1))
a1=E.pop()
b1=O.pop()
if a1[0] != b1[0]:
    print(n-a1[1]-b1[1])
else:
    ans=a1[1]
    if E:
        a2=E.pop()
        ans=max(ans,a2[1]+b1[1])
    if O:
        b2=O.pop()
        ans = max(ans,a1[1]+b2[1])
    print(n-ans)

Submission status: Accepted

Solution code:
import math
import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix
input = stdin.readline
setrecursionlimit(10**7)

def main():
    do = defaultdict(int)
    de = defaultdict(int)
    do[-1] = 0
    de[-1] = 0
    n = int(input())
    v = list(map(int,input().split()))
    for i,vi in enumerate(v):
        if i%2 == 0:
            de[vi] += 1
        else:
            do[vi] += 1
    dem = 0
    demk = 0
    dom = 0
    domk = 0
    for dek,dev in de.items():
        if dem < dev:
            demk = dek
            dem = dev
    for dok,dov in do.items():
        if dom < dov:
            domk = dok
            dom = dov

    if demk == domk:
        if list(do.values()).count(dom) > 1 or list(de.values()).count(dem) > 1:
            print(n-dem-dom)
        else:
            de[demk] = 0
            do[domk] = 0
            dxm = dem
            dem = max(de.values())
            dom = max(do.values())
            if dem > dom:
                print(n-dxm-dem)
            else:
                print(n-dxm-dom)
    else:
        print(n-dem-dom)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

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

c1 = Counter(v1)
c2 = Counter(v2)

c1 = c1.most_common()
c2 = c2.most_common()

if c1[0][0] != c2[0][0]:
    ans = n - c1[0][1] - c2[0][1]
else:
    if len(c2) > 1:
        choice_1 = c1[0][1] + c2[1][1]
    else:
        choice_1 = c1[0][1]
    if len(c1) > 1:
        choice_2 = c2[0][1] + c1[1][1]
    else:
        choice_2 = c2[0][1]
    ans = n - (max(choice_1, choice_2))

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
def input(): return sys.stdin.readline().strip()


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

    d_odd = defaultdict(int)
    d_even = defaultdict(int)
    one_kind = True
    for i, v in enumerate(V):
        if v != V[0]: one_kind = False
        if i % 2 == 0: d_even[v] += 1
        if i % 2 != 0: d_odd[v] += 1
    if one_kind:
        print(n // 2)
        return
    
    """
    修正した結果すべての数字が一致する場合を省かないといけないのがコーナーケース
    """
    
    even_list = list(d_even.values())
    even_list.sort()
    even_max = even_list.pop()
    even_pre_max = even_list.pop() if len(even_list) > 0 else -10**10
    odd_list = list(d_odd.values())
    odd_list.sort()
    odd_max = odd_list.pop()
    odd_pre_max = odd_list.pop() if len(odd_list) > 0 else -10**10

    even_max_key = [k for k, v in d_even.items() if v == even_max]
    odd_max_key = [k for k, v in d_odd.items() if v == odd_max]
   

    if len(even_max_key) == 1 and len(odd_max_key) == 1 and even_max_key[0] == odd_max_key[0]:
        print(min(n - even_max - odd_pre_max, n - even_pre_max - odd_max))
    else:
        print(n - even_max - odd_max)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

cnt_even = Counter(v[0::2])
cnt_odd = Counter(v[1::2])

# [(要素, 出現回数), (要素, 出現回数), ....]
common_even = cnt_even.most_common()
common_odd = cnt_odd.most_common()
common_even.append((0, 0))
common_odd.append((0, 0))

# 偶数番目に一番多く現れる数と奇数番目に一番多く現れる数が異なる
if common_even[0][0] != common_odd[0][0]:
    ans = n - common_even[0][1] - common_odd[0][1]
else:
    ans = min(n - common_even[1][1] - common_odd[0][1], \
              n - common_even[0][1] - common_odd[1][1])

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
n = int(input())
v = [int(x) for x in input().split()]

cnt_even = Counter(v[0::2])
cnt_odd = Counter(v[1::2])

# [(要素, 出現回数), (要素, 出現回数), ....]
common_even = cnt_even.most_common()
common_odd = cnt_odd.most_common()
if len(common_even) == 1:   # 1種類の数しかない
    common_even.append((0, 0))
if len(common_odd) == 1:
    common_odd.append((0, 0))

# 偶数番目に一番多く現れる数と奇数番目に一番多く現れる数が異なる
if common_even[0][0] != common_odd[0][0]:
    ans = n - common_even[0][1] - common_odd[0][1]
else:
    ans = min(n - common_even[1][1] - common_odd[0][1], \
              n - common_even[0][1] - common_odd[1][1])

print(ans)

Submission status: Accepted

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

even_ocr = defaultdict(int) # v[0], v[2], ..., で各数が何回現れるか
odd_ocr = defaultdict(int)  # v[1], v[3], ..., で各数が何回現れるか

for i in range(n // 2):
    even_ocr[v[2 * i]] += 1
    odd_ocr[v[2 * i + 1]] += 1

even_max_num = 0;  even_max_ocr = 0;
odd_max_num = 0;  odd_max_ocr = 0;
for (num, ocr) in even_ocr.items():
    if ocr > even_max_ocr:
        even_max_ocr = ocr
        even_max_num = num

for (num, ocr) in odd_ocr.items():
    if ocr > odd_max_ocr:
        odd_max_ocr = ocr
        odd_max_num = num

if even_max_num != odd_max_num:
    ans = n - even_max_ocr - odd_max_ocr
else:
# 偶数番目に一番多く現れる数と奇数番目に一番多く現れる数が同じ
    even_next_num = 0;  even_next_ocr = 0;
    odd_next_num = 0;  odd_next_ocr = 0;
    for (num, ocr) in even_ocr.items():
        if num != even_max_num and ocr > even_next_ocr:
            even_next_ocr = ocr
            even_next_num = num

    for (num, ocr) in odd_ocr.items():
        if num != odd_max_num and ocr > odd_next_ocr:
            odd_next_ocr = ocr
            odd_next_num = num

    if even_next_ocr + odd_max_ocr > even_max_ocr + odd_next_ocr:
        # 偶数番目は二番目に多い数、奇数番目は一番多い数に揃える
        ans = n - even_next_ocr - odd_max_ocr
    else:
        ans = n - even_max_ocr - odd_next_ocr

print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
v=list(map(int,input().split()))
c1=collections.Counter(v[::2])
c2=collections.Counter(v[1::2])
c1_v1=c1.most_common()[0][0]
c2_v1=c2.most_common()[0][0]
c1_n1=c1.most_common()[0][1]
c2_n1=c2.most_common()[0][1]

if c1_v1!=c2_v1:
    print(n-c1_n1-c2_n1)
else:
    if c1_n1==n//2:
        if c2_n1==n//2:
            print(n//2)
        else:
            c2_n2=c2.most_common(2)[1][1]
            print(n-c1_n1-c2_n2)
    else:
        c1_n2=c1.most_common(2)[1][1]
        if c2_n1==n//2:
            print(n-c1_n2-c2_n1)
        else:
            c2_n2=c2.most_common(2)[1][1]
            print(min(n-c1_n2-c2_n1,n-c1_n1-c2_n2))

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
a=Counter(v[::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])))