Problem:


Score : 300 points

Problem Statement

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

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

Exactly two different numbers appear in the sequence.

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

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

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

Sample Input 1

4
3 1 3 2

Sample Output 1

1

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

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

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

Sample Input 3

4
1 1 1 1

Sample Output 3

2

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



Sample input:
4
3 1 3 2


Sample output:
1

Submission status: Accepted

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

for i,key in enumerate(v):
    if i%2 == 0:
        if key not in dict1:
            dict1[key] = 1
        else:
            dict1[key] += 1
    else:
        if key not in dict2:
            dict2[key] = 1
        else:
            dict2[key] += 1

max1 = 0
max2 = 0
if len(dict1) == 1 and len(dict2) == 1:
    if v[0] == v[1]:
        ans = n//2
    else:
        ans = 0
elif len(dict1) == 1:
    dict2[v[0]] = 0
    for key in dict2:
        max2 = max(max2,dict2[key])
    else:
        ans = n//2 - max2
elif len(dict2) == 1:
        dict2[v[1]] = 0
        for key in dict1:
            max1 = max(max1,dict1[key])
        else:
            ans = n//2 - max1
else:
    second1 = 0
    second2 = 0
    maxkey1 = 0
    maxkey2 = 0
    secondkey1 = 0
    secondkey2 = 0
    for key in dict1:
        if dict1[key] > max1:
            max1,second1, = dict1[key],max1
            maxkey1,secondkey1 = key,maxkey1
        elif dict1[key] > second1:
            second1 = dict1[key]
            secondkey1 = key
    for key in dict2:
        if dict2[key] > max2:
            max2,second2, = dict2[key],max2
            maxkey2,secondkey2 = key,maxkey2
        elif dict2[key] > second2:
            second2 = dict2[key]
            secondkey2 = key
    if maxkey1 == maxkey2:
        if dict1[secondkey1] < dict2[secondkey2]:
            ans = n - dict1[maxkey1] - dict2[secondkey2]
        else:
            ans = n - dict2[maxkey2] - dict1[secondkey1]
    else:
        ans = n - dict1[maxkey1] - dict2[maxkey2]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
w=[]
for i in range(n//2):
    w.append(v.pop(i))

import collections
cv = collections.Counter(v)
cw = collections.Counter(w)
scv = sorted(cv.items(), key=lambda x:-x[1])
scw = sorted(cw.items(), key=lambda x:-x[1])

v1=scv[0][0]
w1=scw[0][0]

if v1==w1:
    if len(scv)==1:
        v2=0
    else:
        v2=scv[1][1]
    if len(scw)==1:
        w2=0
    else:
        w2=scw[1][1]
        
    print(min(n-scv[0][1]-w2, n-scw[0][1]-v2))
        
else:
    print(n-scv[0][1]-scw[0][1])

Submission status: Accepted

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

gu=[]
ki=[]

from collections import Counter

for i in range(n):
  if i%2:
    ki.append(v[i])
  else:
    gu.append(v[i])

g=Counter(gu)
k=Counter(ki)

#gとkが同じ場合は、「gとkの次の候補のうち、最適な方に」ずらしてく処理が必要
#両方とも一種類しかない場合はn//2が答え
x=g.most_common()[0][1]
y=k.most_common()[0][1]
a=x+y
if g.most_common()[0][0]==k.most_common()[0][0]:
  if len(Counter(gu))!=1:
    if g.most_common()[1][1]>k.most_common()[1][1]:
      a=g.most_common()[1][1]+y
    else:
      a=x+k.most_common()[1][1]
  else:
    a=n//2
ans=n-a
print(ans)

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]
v1c = Counter(v1).most_common()
v2c = Counter(v2).most_common()
v1c.append((0, 0))
v2c.append((0, 0))

if v1c[0][0]==v2c[0][0]:
    print(min(N-v1c[0][1]-v2c[1][1], N-v1c[1][1]-v2c[0][1]))
else:
    print(N-v1c[0][1]-v2c[0][1])

Submission status: Accepted

Solution code:
N = int(input())
vlist = list(map(int, input().split()))
odd, even = vlist[1::2], vlist[::2]
from collections import Counter
odd_c, even_c = Counter(odd).most_common(2), Counter(even).most_common(2)
# print(odd_c, even_c)
if odd_c[0][0] != even_c[0][0]:
    print(N - odd_c[0][1] - even_c[0][1])
else:
    if len(odd_c) >= 2 and len(even_c) >= 2:
        if odd_c[0][1] > even_c[0][1]:
            print(N - odd_c[0][1] - even_c[1][1])
        elif odd_c[0][1] < even_c[0][1]:
            print(N - odd_c[1][1] - even_c[0][1])
        else:
            print(N - odd_c[0][1] - max(odd_c[1][1], even_c[1][1]))
    else:
        if len(odd_c) == 1 and len(even_c) == 1:
            if odd_c[0][0] == even_c[0][0]:
                print(N - max(odd_c[0][1], even_c[0][1]))
        elif len(odd_c) == 1:
            if odd_c[0][0] != even_c[0][0]:
                print(N - odd_c[0][1] - even_c[0][1])
            else:
                if odd_c[0][1] > even_c[0][1]:
                    print(N - odd_c[0][1] - even_c[1][1])
                else:
                    print(N - even_c[0][1])
        elif len(even_c) == 1:
            if odd_c[0][0] != even_c[0][0]:
                print(N - odd_c[0][1] - even_c[0][1])
            else:
                if odd_c[0][1] < even_c[0][1]:
                    print(N - odd_c[1][1] - even_c[0][1])
                else:
                    print(N - odd_c[0][1])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import Counter

def solve(n: int, v: "List[int]"):
    v1 = v[0::2]
    v2 = v[1::2]

    counter1 = Counter(v1).most_common(2)
    counter2 = Counter(v2).most_common(2)

    key1,value1 = counter1[0]
    key2,value2 = counter2[0]

    if key1 == key2:
        answer1 = 0
        ##1をかき変える
        if len(counter1)>=2:
            answer1 = len(v1)-counter1[1][1] +len(v2)-value2
        else:
            answer1 = len(v1) +len(v2)-value2

        answer2 = 0
        if len(counter1)>=2:
            answer2 = len(v1)-value1 +len(v2)-counter2[1][1]
        else:
            answer2 = len(v1)-value1 +len(v2)
        print(min(answer1,answer2))   
    else:
        print(len(v1)-value1+len(v2)-value2)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    v = [int(next(tokens)) for _ in range(n)]  # type: "List[int]"
    solve(n, v)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
DO, DE = {}, {}
L, M = [], []
for i in range(N//2):
  if A[2*i] in DO:
    DO[A[2*i]] += 1
  else:
    DO[A[2*i]] = 1
  if A[2*i+1] in DE:
    DE[A[2*i+1]] += 1
  else:
    DE[A[2*i+1]] = 1
for i in DO:
  L.append([DO[i], i])
for j in DE:
  M.append([DE[j], j])
l, m = len(L)-1, len(M)-1
L.sort()
M.sort()
if L[l][1] != M[m][1]:
  S = N - L[l][0] - M[m][0]
else:
  if l == 0 and m == 0:
    S = N // 2
  else:
    if L[l][0] - L[l-1][0] >= M[m][0] - M[m-1][0]:
      S = N - L[l][0] - M[m-1][0]
    else:
      S = N - L[l-1][0] - M[m][0]
print(S)

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
d1={}
d2={}
for i in range(n):
  if i%2==0:
    if v[i] not in d1:
      d1[v[i]]=1
    else:
      d1[v[i]]+=1
  else:
    if v[i] not in d2:
      d2[v[i]]=1
    else:
      d2[v[i]]+=1
nd1=sorted(d1.items(),key=lambda x:x[1],reverse=True)
nd2=sorted(d2.items(),key=lambda x:x[1],reverse=True)
nd1.append((0,0))
nd2.append((0,0))
if nd1[0][0]==nd2[0][0]:
  print(n-max(nd1[0][1]+nd2[1][1],nd1[1][1]+nd2[0][1]))
else:
  print(n-nd1[0][1]-nd2[0][1])

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])
if len(set(v)) == 1:
    print(n // 2)
elif a.most_common()[0][0] != b.most_common()[0][0]:
    print(n - a.most_common()[0][1] - b.most_common()[0][1])
elif len(a) < 2 and len(b) > 1:
    print(n // 2 - a.most_common()[1][1])
elif len(a) > 1 and len(b) < 2:
    print(n // 2 - b.most_common()[1][1])
else:
    print(
        min(n - a.most_common()[0][1] - b.most_common()[1][1],
            n - a.most_common()[1][1] - b.most_common()[0][1]))

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]
v1c = Counter(v1).most_common()
v2c = Counter(v2).most_common()
v1c.append((0,0))
v2c.append((0,0))
if v1c[0][0]==v2c[0][0]:
    print(min(N-v1c[0][1]-v2c[1][1],N-v1c[1][1]-v2c[0][1]))
else:
    print(N-v1c[0][1]-v2c[0][1])

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]

v1c=Counter(v1).most_common()
v2c=Counter(v2).most_common()
v1c.append((0,0))
v2c.append((0,0))
#print(v1c[0])
if v1c[0][0]==v2c[0][0]:
    print(min(n-v1c[0][1]-v2c[1][1],n-v1c[1][1]-v2c[0][1]))
else:
    print(n-v1c[0][1]-v2c[0][1])

Submission status: Accepted

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

v1c=Counter(v1).most_common()
v2c=Counter(v2).most_common()
v1c.append((0,0))
v2c.append((0,0))
#print(v1c[0])
if v1c[0][0]==v2c[0][0]:
    print(min(n-v1c[0][1]-v2c[1][1],n-v1c[1][1]-v2c[0][1]))
else:
    print(n-v1c[0][1]-v2c[0][1])

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    n = I()
    v = LI()
    even_count = collections.defaultdict(int)
    odd_count = collections.defaultdict(int)
    for i in range(n):
        if i % 2 == 1:
            odd_count[v[i]] += 1
        else:
            even_count[v[i]] += 1
    odd_array = []
    even_array = []
    for k, v in odd_count.items():
        odd_array.append((k, v))
    for k, v in even_count.items():
        even_array.append((k, v))

    odd_array = sorted(odd_array, key=lambda x:x[1])[::-1]
    even_array = sorted(even_array, key=lambda x:x[1])[::-1]

    ans = inf
    arrLen = n // 2
    for ok, ov in odd_array[:2]:
        for ek, ev in even_array[:2]:
            if ok == ek:
                continue
            ans = min(ans, (arrLen - ov) + (arrLen - ev))
    if ans == inf:
        ans = arrLen
    print(ans)
main()

Submission status: Accepted

Solution code:
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 == 1]
v1.sort()
v2.sort()
num1 = 0
num2 = 0
num11 = 0
num22 = 0
check1 = 1
check2 = 1
p = 0
for i in range(len(v1)-1):
    if v1[i] != v1[i+1]:
        if check1 > num1:
            num1 = check1
            p = v1[i]
        check1 = 1
    else:
        check1 += 1
        if check1 > num1:
            num1 = check1
            p = v1[i]

for i in range(len(v2)-1):
    if v2[i] != v2[i+1]:
        if check2 > num2 and v2[i] != p:
            num2 = check2
        check2 = 1
    else:
        check2 += 1
    if check2 > num2 and v2[i] != p:
        num2 = check2

check1 = check2 = 1
for i in range(len(v2)-1):
    if v2[i] != v2[i+1]:
        if check2 > num22:
            num22 = check2
            p = v2[i]
        check2 = 1
    else:
        check2 += 1
    if check2 > num22:
        num22 = check2
        p = v2[i]
        
for i in range(len(v1)-1):
    if v1[i] != v1[i+1]:
        if check1 > num11 and v1[i] != p:
            num11 = check1
        check1 = 1
    else:
        check1 += 1
        if check1 > num11 and v1[i] != p:
            num11 = check1

print(min(n-num1-num2, n-num11-num22))

Submission status: Accepted

Solution code:
import collections

n = int(input())
V = list(map(int, input().split()))
kisuu = []
gusuu = []
for idx, v in enumerate(V):
    if idx % 2 == 0:
        gusuu.append(v)
    else:
        kisuu.append(v)
gusuu_c = collections.Counter(gusuu)
gusuu_c_sorted = sorted(gusuu_c.items(), key=lambda x: x[1], reverse=True)
kisuu_c = collections.Counter(kisuu)
kisuu_c_sorted = sorted(kisuu_c.items(), key=lambda x: x[1], reverse=True)

if gusuu_c_sorted[0][0] != kisuu_c_sorted[0][0]:
    print(
        len(kisuu) - kisuu_c_sorted[0][1] + len(gusuu) - gusuu_c_sorted[0][1])
else:
    if len(gusuu_c_sorted) == 1 and len(kisuu_c_sorted) == 1:
        print(min(len(gusuu), len(kisuu)))
    elif len(gusuu_c_sorted) == 1:
        print(len(kisuu) - kisuu_c_sorted[1][1])
    elif len(kisuu_c_sorted) == 1:
        print(len(gusuu) - gusuu_c_sorted[1][1])
    else:
        print(min(len(kisuu) - kisuu_c_sorted[1][1] + len(gusuu) - gusuu_c_sorted[0][1],
                  len(kisuu) - kisuu_c_sorted[0][1] + len(gusuu) - gusuu_c_sorted[1][1]))

Submission status: Accepted

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


even = [0] * (10 ** 5)
odd = [0] * (10 ** 5)

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

for i in range(1, n, 2):
    odd[v[i] - 1] += 1

even_max = max(even)
odd_max = max(odd)

even_max_i = even.index(even_max)
odd_max_i = odd.index(odd_max)

if even_max_i == odd_max_i:
    even[even_max_i] = 0
    odd[odd_max_i] = 0

    even_max_2 = max(even)
    odd_max_2 = max(odd)

    ans = n - max(even_max + odd_max_2, even_max_2 + odd_max)
else:
    ans = n - (even_max + odd_max)

print(ans)

Submission status: Accepted

Solution code:
import itertools
from collections import Counter
N = int(input())
V = list(map(int,input().split()))
even_counter = Counter(V[::2])
odd_counter = Counter(V[1::2])
even_counter[-1] = 0
odd_counter[-1] = 0
even = even_counter.most_common(2)
odd = odd_counter.most_common(2)
ans = N
for (k1,v1),(k2,v2) in itertools.product(even,odd):
  if k1 == k2:
    continue
  ans = min(ans,N - v1 - v2)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
odd = [0] * (10 ** 5 + 1)
even = [0] * (10 ** 5 + 1)
odd_cnt = []
even_cnt = []
ans = 0
for i in range(n):
    if i % 2 == 0:
        even[v[i]] += 1
    else:
        odd[v[i]] += 1
for i in range(10 ** 5 + 1):
    odd_cnt.append((odd[i], i))
    even_cnt.append((even[i], i))
even_cnt.sort(reverse=True)
odd_cnt.sort(reverse=True)
if even_cnt[0][1] == odd_cnt[0][1]:
    ans = n - max(even_cnt[0][0] + odd_cnt[1][0],
                  even_cnt[1][0] + odd_cnt[0][0])
else:
    ans = n - (even_cnt[0][0] + odd_cnt[0][0])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
L = list(map(int, input().split()))
odd = [v for i, v in enumerate(L) if i % 2 == 0]
even = [v for i, v in enumerate(L) if i % 2 == 1]

if len(set(L)) == 1:
    print(N//2)
    exit()
if N == 2:
    print(0)
    exit()


c_odd = sorted(dict(Counter(odd)).items(), key=lambda x: -x[1])
c_even = sorted(dict(Counter(even)).items(), key=lambda x: -x[1])

odd_1 = c_odd[0]
even_1 = c_even[0]

if odd_1[0] != even_1[0]:
    print(N - odd_1[1] - even_1[1])
    exit()

even_2 = c_even[1]
odd_2 = c_odd[1]

ans = N
ans = min(ans, N - odd_1[1] - even_2[1])
ans = min(ans, N - odd_2[1] - even_1[1])
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

if len(set(L)) == 1:
    print(N//2)
    exit()
if N == 2:
    print(0)
    exit()


d_even = defaultdict(int)
d_odd = defaultdict(int)

for i in range(N):
    v = L[i]
    if i % 2 == 0:
        d_even[v] += 1
    else:
        d_odd[v] += 1

l_even = sorted(d_even.items(), key=lambda x: -x[1])
l_odd = sorted(d_odd.items(), key=lambda x: -x[1])

if l_even[0][0] != l_odd[0][0]:
    print(N - l_odd[0][1] - l_even[0][1])
    exit()

ans = N
ans = min(ans, N - l_odd[0][1] - l_even[1][1])
ans = min(ans, N - l_odd[1][1] - l_even[0][1])
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

if len(set(L)) == 1:
    print(N//2)
    exit()
if N == 2:
    print(0)
    exit()


d_even = defaultdict(int)
d_odd = defaultdict(int)

for i in range(N):
    v = L[i]
    if i % 2 == 0:
        d_even[v] += 1
    else:
        d_odd[v] += 1

l_even = sorted(d_even.items(), key=lambda x: -x[1])
l_odd = sorted(d_odd.items(), key=lambda x: -x[1])

if l_even[0][0] != l_odd[0][0]:
    print(N - l_even[0][1] - l_odd[0][1])
else:
    # どちらも試してコストの小さい方を採用する
    ans1 = N - l_even[0][1] - l_odd[1][1]
    ans2 = N - l_even[1][1] - l_odd[0][1]
    print(min(ans1, ans2))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
V = list(map(int,input().split()))
if len(set(V)) == 1:
  print(n//2)
else:
  odd = []
  even = []
  for i in range(len(V)):
    if i % 2 == 0:
      even.append(V[i])
    else:
      odd.append(V[i])
  max_even,max_e_count = Counter(even).most_common(1)[0]
  max_odd,max_o_count = Counter(odd).most_common(1)[0]
  if max_even == max_odd:
    sec_even,sec_e_count = Counter(even).most_common(2)[1]
    sec_odd,sec_o_count = Counter(odd).most_common(2)[1]
    even_rank2 = len(odd) - max_o_count + len(even) - sec_e_count
    odd_rank2 = len(odd) - sec_o_count + len(even) - max_e_count
    print(min(even_rank2,odd_rank2))
  else:
    print(len(odd) - max_o_count + len(even) - max_e_count)

Submission status: Accepted

Solution code:
import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    v = list(map(int, readline().split()))
    ve = collections.Counter(v[0::2]).most_common()
    vo = collections.Counter(v[1::2]).most_common()
    if ve[0][0] != vo[0][0]:
        print((n // 2 - ve[0][1]) + (n // 2 - vo[0][1]))
    else:
        if len(ve) == len(vo) == 1:
            print(n // 2)
        elif len(ve) == 1:
            print((n // 2 - ve[0][1]) + (n // 2 - vo[1][1]))
        elif len(vo) == 1:
            print((n // 2 - ve[1][1]) + (n // 2 - vo[0][1]))
        else:
            print(min((n // 2 - ve[0][1]) + (n // 2 - vo[1][1]), (n // 2 - ve[1][1]) + (n // 2 - vo[0][1])))



if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int,input().split()))
a = []
b = []
for i in range(0,n,2):
    a.append(v[i])
    b.append(v[i+1])
a = Counter(a)
b = Counter(b)
p = a.most_common()[0][1]
q = b.most_common()[0][1]

if a.most_common()[0][0] == b.most_common()[0][0]:
    if p == n//2:
        if q == n//2:
            print(n-p-q+n//2)
        else:
            print(n-q-b.most_common()[1][1])
    else:
        if q != n//2:
            
            print(min(n-p-b.most_common()[1][1],n-q-a.most_common()[1][1]))
        else:
            print(n-q-a.most_common()[1][1])
            
    
        
            
        

else:
    print(n-p-q)

Submission status: Accepted

Solution code:
from collections import Counter

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

even = Counter(lst[0::2]).most_common(2) + [(0, 0)]
odd = Counter(lst[1::2]).most_common(2) + [(0, 0)]

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

Submission status: Accepted

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

a=Counter(v[::2]).most_common(2)
b=Counter(v[1::2]).most_common(2)

a.append([0,0])
b.append([0,0])

if a[0][0]!=b[0][0]:
      print(n-(a[0][1]+b[0][1]))
else:
      print(min(n-(a[1][1]+b[0][1]),n-(a[0][1]+b[1][1])))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

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

if n==2:
    ret = 0
    if v[0]==v[1]:
        ret = 1
    print(ret)
    sys.exit()

max_v = max(v)+1
n_odd = v[0::2]
n_even = v[1::2]
n_half = n//2

odd_bucket = [0] * max_v
even_bucket = [0] * max_v

for i in n_odd:
    odd_bucket[i]+=1
for i in n_even:
    even_bucket[i]+=1

#最大と2番目のindexを取得
odd_max1 = [0,0]#index,value
odd_max2 = [0,0]#index,value
for i,v in enumerate(odd_bucket):
    if v >= odd_max1[1]:
        odd_max2[0],odd_max2[1] = odd_max1[0],odd_max1[1]
        odd_max1 = [i,v]
    elif v > odd_max2[1]:
        odd_max2 = [i,v]

#最大と2番目のindexを取得
even_max1 = [0,0]#index,value
even_max2 = [0,0]#index,value
odd_val = 0
even_val = 0

for i,v in enumerate(even_bucket):
    if v >= even_max1[1]:
        even_max2[0],even_max2[1] = even_max1[0],even_max1[1]
        even_max1 = [i,v]
    elif v > even_max2[1]:
        even_max2 = [i,v]
if odd_max1[0]==even_max1[0] and odd_max1[1]!=odd_max2[1] and even_max1[1]!=even_max2[1]:
    if odd_max1[1] - odd_max2[1] >= even_max1[1] - even_max2[1]:
        odd_val = odd_max1[1]
        even_val = even_max2[1]
    else:
        odd_val = odd_max2[1]
        even_val = even_max1[1]
elif odd_max1[0] != even_max1[0]:
    odd_val = odd_max1[1]
    even_val = even_max1[1]
else:
    if odd_max1[1]==odd_max2[1]:
        odd_val = odd_max2[1]
        even_val = even_max1[1]
    else:
        odd_val = odd_max1[1]
        even_val = even_max2[1]
print(n-odd_val-even_val)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N = INT()
A = MAP()

c = Counter(A)
v, c = zip(*c.most_common(2))
if max(c) == 1:
	print(N-2)
	sys.exit()
if len(v) < 2:
	print(N//2)
	sys.exit()
a = v[0]
b = v[1]

can_a = [a if i%2 else b for i in range(N)]
can_b = [b if i%2 else a for i in range(N)]
#print(can_a, can_b)

cand = [can_a, can_b]
ans = float('INF')
for c in cand:
	cnt = 0
	for i in range(N):
		if c[i] != A[i]: cnt+=1
	ans = min(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
v=list(input().split())
v1=v[0:n:2]
v2=v[1:n:2]
import collections
c1=collections.Counter(v1)
a1=c1.most_common()[0][0]
a2=(c1.most_common()[1][0] if len(c1)>=2 else 0)
c2=collections.Counter(v2)
b1=c2.most_common()[0][0]
b2=(c2.most_common()[1][0] if len(c2)>=2 else 0)
if a1!=b1:
    cou=0
    for i in range(n//2):
        if v1[i]!=a1:
            cou+=1
    for i in range(n//2):
        if v2[i]!=b1:
            cou+=1
    print(cou)
else:
    cou1=0
    for i in range(n//2):
        if v1[i]!=a2:
            cou1+=1
    for i in range(n//2):
        if v2[i]!=b1:
            cou1+=1
    cou2=0
    for i in range(n//2):
        if v1[i]!=a1:
            cou2+=1
    for i in range(n//2):
        if v2[i]!=b2:
            cou2+=1
    print(min(cou1,cou2))

Submission status: Accepted

Solution code:
from collections import Counter

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

v_odd=v[::2]
v_even=v[1::2]
c_odd=Counter(v_odd)
cm_odd=c_odd.most_common()
c_even=Counter(v_even)
cm_even=c_even.most_common()

if cm_odd[0][0]!=cm_even[0][0]:
    ans=(len(v_odd)-cm_odd[0][1]+len(v_even)-cm_even[0][1])
elif len(cm_odd)==1 and len(cm_even)==1:
    ans=n//2
elif len(cm_odd)==1:
    ans=len(v_even)-cm_even[1][1]
elif len(cm_even)==1:
    ans=len(v_odd)-cm_odd[1][1]
else:
    ans=min((len(v_odd)-cm_odd[1][1])+(len(v_even)-cm_even[0][1]),(len(v_odd)-cm_odd[0][1])+(len(v_even)-cm_even[1][1]))
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)
c2=collections.Counter(v2)
arr1=c1.most_common()[1:]
arr2=c2.most_common()[1:]
arr21=c1.most_common()[:1]+c1.most_common()[2:]
arr22=c2.most_common()[:1]+c2.most_common()[2:]
arr1n=sum([a[1] for a in arr1])
arr2n=sum([a[1] for a in arr2])
arr21n=sum([a[1] for a in arr21])
arr22n=sum([a[1] for a in arr22])
if c1.most_common()[0][0]==c2.most_common()[0][0]:
  if arr21n<=arr22n:
    print(arr21n+arr2n)
  else:
    print(arr22n+arr1n)
else:
  print(arr1n+arr2n)

Submission status: Accepted

Solution code:
from collections import Counter

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

even = list(Counter(v[0::2]).items())
odd = list(Counter(v[1::2]).items())

even.append([0,0])
odd.append([0,0])
even.sort(key=lambda x: -x[1])
odd.sort(key=lambda x: -x[1])

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

Submission status: Accepted

Solution code:
n = int(input())
v = [int(i) for i in input().split()]
if [v[0]] * n == v:
    print(n // 2)
    exit()
even_n = {}
odd_n = {}
for i in range(0, n):
    if (i + 1) % 2 == 0:
        if str(v[i]) in even_n:
            even_n[str(v[i])] += 1
        else:
            even_n[str(v[i])] = 1
    if (i + 1) % 2 == 1:
        if str(v[i]) in odd_n:
            odd_n[str(v[i])] += 1
        else:
            odd_n[str(v[i])] = 1
even_n = sorted(even_n.items(), key=lambda x: x[1], reverse=True)
odd_n = sorted(odd_n.items(), key=lambda x: x[1], reverse=True)
unchanged = 0
if even_n[0][0] != odd_n[0][0]:
    unchanged = even_n[0][1] + odd_n[0][1]
else:
    unchanged = max(even_n[0][1] + odd_n[1][1], even_n[1][1] + odd_n[0][1])
print(n - unchanged)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a1=a[::2]
a2=a[1::2]
if a1==a2 and len(set(a1))==1:
    print(n//2)
    exit()
import collections as c
c1=c.Counter(a1)
c2=c.Counter(a2)
c1=c1.most_common()
c2=c2.most_common()
ans=0
if c1[0][0]==c2[0][0]:
    ans=min((n//2-c1[0][1])+(n//2-c2[1][1]),(n//2-c1[1][1])+(n//2-c2[0][1]))
else:
    ans=(n//2-c1[0][1])+(n//2-c2[0][1])
print(ans)

Submission status: Accepted

Solution code:
#C
N = int(input())
V = list(map(int,input().split()))

M = 10**5
C = [0 for i in range(M)]
D = [0 for i in range(M)]
for i in range(N):
    if i%2 == 0:
        C[V[i]-1]+=1
    else:
        D[V[i]-1]+=1
        
c,d = (N+1)//2,N//2

MC = max(C)
MD = max(D)

ans = 0
if C.index(MC) == D.index(MD):
    C[C.index(MC)]-=MC
    D[D.index(MD)]-=MC
    
    MC2 = max(C)
    MD2 = max(D)
    if c-MC + d-MD2 >= c-MC2 + d-MD:
        ans = c-MC2 + d-MD
    else:
        ans = c-MC + d-MD2
    
else:
    ans = c-MC + d-MD

print(ans)

Submission status: Accepted

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

from collections import Counter
l1 = Counter(l[0::2])
mostl1 = l1.most_common()[0]
#semimostl1 = l1.most_common()[1]

l2 = Counter(l[1::2])
mostl2 = l2.most_common()[0]
#semimostl2 = l2.most_common()[1]

if mostl1[0] != mostl2[0]:
    print(n-mostl1[1]-mostl2[1])
elif len(l1.keys()) == 1 and len(l2.keys()) == 1:
    print(int(n/2))

else:
    semimostl1 = l1.most_common()[1]
    semimostl2 = l2.most_common()[1]
    print(min(n - mostl1[1] - semimostl2[1], n - semimostl1[1] - mostl2[1]))


#elif print(min(n-mostl1[1]-mostl2[1],n-mostl1[1]-mostl2[1]))

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
a=[]
b=[]
A=[]
B=[]
for i in range(n):
    if i%2==0:
        a.append(v[i])
        A.append([0,0])
    else:
        b.append(v[i])
        B.append([0,0])
a.sort()
b.sort()

A[0][1]=a[0]
B[0][1]=b[0]

x=a[0]
y=0
for j in range(len(a)):
  if a[j]==x:
    A[y][0]+=1
  else:
    x=a[j]
    y+=1
    A[y][1]=x
    A[y][0]+=1
    
w=b[0]  
z=0
for k in range(len(b)):
  if b[k]==w:
    B[z][0]+=1
   
  else:
    w=b[k]
    z+=1
    B[z][1]=w
    B[z][0]+=1 
    
A=sorted(A,key=lambda x:x[0],reverse=True) 
B=sorted(B,key=lambda x:x[0],reverse=True)  
if A[0][1]==B[0][1]:
    print(min(n-A[0][0]-B[1][0],n-A[1][0]-B[0][0]))
elif n-A[0][0]-B[0][0]==0 and A[0][1]==B[0][1]:
    print(int(n/2))
else:
    print(n-A[0][0]-B[0][0])

Submission status: Accepted

Solution code:
import collections

n=int(input())
a=list(map(int,input().split()))
a1=[a[2*i] for i in range(n-n//2)]
a2=[a[2*i+1] for i in range(n//2)]
c1=collections.Counter(a1)
c2=collections.Counter(a2)
e1=c1.most_common()
e2=c2.most_common()
d1=e1[0]
d2=e2[0]
if d1[0]!=d2[0]:
    print(n-d1[1]-d2[1])
elif len(e1)==1 and len(e2)==1:
    print(min(d1[1],d2[1]))
elif len(e1)==1:
    print(min((n//2-d2[1])+d1[1],n//2-e2[1][1]))
elif len(e2)==1:
    print(min((n-n//2-d1[1])+d2[1],n-n//2-e1[1][1]))
else:
    print(n-max(d1[1]+e2[1][1],d2[1]+e1[1][1]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
ans = count = 0

n=int(input())
v=list(map(int,input().split()))
v1=[]
v2=[]
for i in range(n):
    if i%2==1:
        v2.append(v[i])
    else:
        v1.append(v[i])
c1=collections.Counter(v1)
c2=collections.Counter(v2)
c1[0]=0;c2[0]=0
c1 = sorted(c1.items(), key=lambda tup: -tup[1])
c2 = sorted(c2.items(), key=lambda tup: -tup[1])
# print(c1,c2)
if not c1[0][0]==c2[0][0]:
    print(n-c1[0][1]-c2[0][1])
    # print(0)
else:
    # print(c1,c2)
    print(min(n-c1[0][1]-c2[1][1],n-c1[1][1]-c2[0][1]))

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
v.insert(0,0) #数合わせ
check=0 #全部同じ数字か？0なら全部同じ数字
now=v[1]
for i in range(2,n):
    if now!=v[i]:
        check=1
        break
        
odd=[]
even=[]
for i in range(1,n+1):
    if i%2==1:
        odd.append(v[i])
    else:
        even.append(v[i])
        
odd2=sorted(odd)
even2=sorted(even)
now_odd,now_even=odd2[0],even2[0]
rensa_odd,rensa_even=1,1
half=n//2
count_odd=[]
count_even=[]

for i in range(1,half):
    if odd2[i]==now_odd:
        rensa_odd+=1
    else:
        count_odd.append([rensa_odd,now_odd])
        now_odd=odd2[i]
        rensa_odd=1
count_odd.append([rensa_odd,now_odd])

for i in range(1,half):
    if even2[i]==now_even:
        rensa_even+=1
    else:
        count_even.append([rensa_even,now_even])
        now_even=even2[i]
        rensa_even=1
count_even.append([rensa_even,now_even])
count_odd2=sorted(count_odd,reverse=True)
count_even2=sorted(count_even,reverse=True)
#print(count_even2)

if check==0:
    print(half)
else:
    if count_odd2[0][1]!=count_even2[0][1]: #最頻値が違う
        print((half-count_odd2[0][0])+(half-count_even2[0][0]))
    else:
        if len(count_odd2)==1:
            print((half-count_odd2[0][0])+(half-count_even2[1][0]))
        elif len(count_even2)==1:
            print((half-count_odd2[1][0])+(half-count_even2[0][0]))
        else:
            print(min((half-count_odd2[0][0])+(half-count_even2[1][0]),(half-count_odd2[1][0])+(half-count_even2[0][0])))