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

ki = {}
guu = {}

for i in range(len(V)):
    if i % 2 == 0:
        guu.setdefault(str(V[i]), 0)
        guu[str(V[i])] += 1
    else:
        ki.setdefault(str(V[i]), 0)
        ki[str(V[i])] += 1

guu = sorted(guu.items(), key=lambda x: -x[1])
ki = sorted(ki.items(), key=lambda x: -x[1])

if len(guu) == 1 and len(ki) != 1:
    if ki[0][0] != guu[0][0]:
        print(N//2 - ki[0][1])
        exit()
    else:
        print(N//2 - ki[1][1])
        exit()

if len(ki) == 1 and len(guu) != 1:
    if guu[0][0] != ki[0][0]:
        print(N//2 - guu[0][1])
        exit()
    else:
        print(N//2 - guu[1][1])
        exit()

if len(ki) == 1 and len(guu) == 1:
    if ki[0][0] == guu[0][0]:
        print(N//2)
        exit()
    else:
        print(0)
        exit()

if guu[0][0] != ki[0][0]:
    print(N - guu[0][1]-ki[0][1])
    exit()

if guu[0][1] > ki[0][1]:  # guuを採用する
    print(N - guu[0][1] - ki[1][1])
    exit()
if ki[0][1] > guu[0][1]:  # kiを採用する
    print(N - ki[0][1] - guu[1][1])
    exit()

if ki[0][1] == guu[0][1]:  # もし等しいなら
    print(N - ki[0][1] - max(ki[1][1], guu[1][1]))
    exit()

Submission status: Accepted

Solution code:
import collections

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

eNum=collections.Counter(r[1::2]).most_common()
oNum=collections.Counter(r[::2]).most_common()
eNum.append((0,0))
oNum.append((0,0))



if eNum[0][0]==oNum[0][0]:
  print(len(r)-max(eNum[0][1]+oNum[1][1],eNum[1][1]+oNum[0][1]))
else:
  print(len(r)-eNum[0][1]-oNum[0][1])

Submission status: Accepted

Solution code:
import collections

N = int(input())
V = list(map(int, input().split()))
Ve = V[::2]
Vo = V[1::2]

Ve_common = collections.Counter(Ve).most_common(2)
Vo_common = collections.Counter(Vo).most_common(2)
if len(set(V)) == 1:
    print(N//2)
elif Ve_common[0][0] != Vo_common[0][0]:
    print(N - Ve_common[0][1] - Vo_common[0][1])
else:
    print(min(N - Ve_common[1][1] - Vo_common[0][1], N - Ve_common[0][1] - Vo_common[1][1]))

Submission status: Accepted

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

E=Counter(V[::2]).most_common() + [(0, 0)]
O=Counter(V[1::2]).most_common() + [(0, 0)]
if E[0][0]!=O[0][0]:
  print(n-E[0][1]-O[0][1])
else:
  print(n-max(E[1][1]+O[0][1],E[0][1]+O[1][1]))

Submission status: Accepted

Solution code:
from sys import stdin
import collections

# コンテスト外回答

n = int(stdin.readline())
v = [int(x) for x in stdin.readline().rstrip().split()]

# [【Python】 リストから一つおきにデータを取り出す \- 旅行好きなソフトエンジニアの備忘録](http://ni4muraano.hatenablog.com/entry/2017/07/16/212133)
# [start:stop:step]
v1 = v[0::2]
v2 = v[1::2]

tmp = len(v1) - len(v2)

v_c = collections.Counter(v)
v1_c = collections.Counter(v1)
v2_c = collections.Counter(v2)

if v1_c.most_common()[0][0] == v2_c.most_common()[0][0]:

    if len(v) == (v_c.most_common()[0][1]):
        ans = len(v) // 2

    else:
        ansA = len(v1) - v1_c.most_common()[0][1] + \
            len(v2) - v2_c.most_common()[1][1]

        ansB = len(v1) - v1_c.most_common()[1][1] + \
            len(v2) - v2_c.most_common()[0][1]

        ans = min(ansA, ansB)

else:
    ans = len(v1) - v1_c.most_common()[0][1] + \
        len(v2) - v2_c.most_common()[0][1]

print(ans)

Submission status: Accepted

Solution code:
def c_zigzag():
    from collections import Counter
    N = int(input())
    V = [int(i) for i in input().split()]

    # 数列の(偶数|奇数)番目の要素のうち、出現頻度が高いもの上位2つを求める
    count_seq_even = Counter(V[::2]).most_common(2)
    count_seq_odd = Counter(V[1::2]).most_common(2)

    # 偶数番目を抜き出した数列と奇数番目を抜き出した数列のそれぞれについて
    # 最も出現頻度の高い要素を調べ、各々それに書き換えればいい。
    # ただし、それらが一致する場合は両方ともその要素に書き換えられないので、
    # 2番目に出現回数の多い要素も(あれば)調べておく。(3番以下を見る必要はない)

    # 両方の数列についてそれぞれ1種の要素しかなく、かつそれらが一致する
    # (全要素が等しい値)場合は、奇数番目か偶数番目かどちらかの要素を
    # すべて書き換えなければならない
    if (len(count_seq_even) == 1 and len(count_seq_odd) == 1
            and count_seq_even[0] == count_seq_odd[0]):
        return N // 2

    ans = float('inf')
    for e, ve in count_seq_even:
        for o, vo in count_seq_odd:
            if e == o:
                continue  # 一致している場合は書き換えられない
            ans = min(ans, N - (ve + vo))
    return ans

print(c_zigzag())

Submission status: Accepted

Solution code:


import collections

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

odd = []
even = []

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

# odd.sort()
# even.sort()

o_c = collections.Counter(odd)
o_e = collections.Counter(even)

o_most = o_c.most_common()
e_most = o_e.most_common()

if len(o_most) == len(e_most) == 1:
   if o_most[0][0] != e_most[0][0]:
       ans = 0
   else:
       ans = min(o_most[0][1], e_most[0][1])

else:
   if o_most[0][0] != e_most[0][0]:
       # odd
       if len(o_most) == 1:
           o_ans = 0
       else:
           o_ans = 0
           for i in range(len(o_most)-1):
               o_ans += o_most[i+1][1]

       # even
       if len(e_most) == 1:
           e_ans = 0
       else:
           e_ans = 0
           for i in range(len(e_most)-1):
               e_ans += e_most[i+1][1]
       # judge
       ans = o_ans + e_ans

   else:
       #最頻値が同じだった場合の処理が必要
       
       o_total=0
       for i in o_most:
       	o_total += i[1]
       
       e_total=0
       for i in e_most:
       	e_total+=i[1]
       
       aa=e_total-e_most[1][1]+o_total-o_most[0][1]
       bb=o_total-o_most[1][1]+e_total-e_most[0][1]
       ans = min(aa,bb)
       
       
       	



print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

evenCnt = Counter(V[::2]).items()
oddCnt = Counter(V[1::2]).items()

evenCnt = list(evenCnt)
oddCnt = list(oddCnt)

evenCnt.sort(key=lambda A: A[1], reverse=True)
oddCnt.sort(key=lambda A: A[1], reverse=True)

if evenCnt[0][0] != oddCnt[0][0]:
    ans = (-(-N // 2) - evenCnt[0][1]) + (N // 2 - oddCnt[0][1])
    print(ans)
else:
    ans = min(
        (-(-N // 2) - evenCnt[0][1]) + N // 2,
        -(-N // 2) + (N // 2 - oddCnt[0][1])
    )
    if len(evenCnt) > 1:
        ans = min(ans, (-(-N // 2) - evenCnt[1][1]) + (N // 2 - oddCnt[0][1]))
    if len(oddCnt) > 1:
        ans = min(ans, (-(-N // 2) - evenCnt[0][1]) + (N // 2 - oddCnt[1][1]))

    print(ans)

Submission status: Accepted

Solution code:
def calc(n, v):
    m1 = {}
    m2 = {}
    for i in range(0, n, 2):
        m1[v[i]] = 1 + (m1[v[i]] if v[i] in m1 else 0)
    for i in range(1, n, 2):
        m2[v[i]] = 1 + (m2[v[i]] if v[i] in m2 else 0)
    m1a = [(k, m1[k]) for k in m1]
    m1a.sort(key=lambda p: -p[1])
    m2a = [(k, m2[k]) for k in m2]
    m2a.sort(key=lambda p: -p[1])
    if (len(m1a) == 1):
        if (len(m2a) == 1):
            return m1a[0][1] if m1a[0][0] == m2a[0][0] else 0
        else:
            if (m1a[0][0] == m2a[0][0]):
                return n // 2 - m2a[1][1]
            else:
                return n // 2 - m2a[0][1]
    else:
        if(len(m2a) == 1):
            if (m1a[0][0] == m2a[0][0]):
                return n // 2 - m1a[1][1]
            return n // 2 - m1a[0][1]
        else:
            a = m1a[0][1] + (m2a[1][1] if m1a[0][0] ==
                             m2a[0][0] else m2a[0][1])
            b = m2a[0][1] + (m1a[1][1] if m1a[0][0] ==
                             m2a[0][0] else m1a[0][1])
            return n - a if a > b else n - b


n = int(input())
v = [int(s) for s in input().split()]
print(calc(n, v))

Submission status: Accepted

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

V1 = [V[n] for n in range(0, n, 2)] #v1, v3, v5, ...
V2 = [V[n] for n in range(1, n, 2)] #v2, v4, v6, ...

mx_V1 = max(V1)
V1_cnt = [[0, 0]] # V1に一種類しか数字が存在しなかったときのために
for i in range(1, mx_V1+1):
  V1_cnt.append([i, 0])
for v in V1:
  V1_cnt[v][1] += 1  
V1_cnt.sort(key=lambda x: x[1], reverse=True)

mx_V2 = max(V2)
V2_cnt = [[0, 0]] # V2に一種類しか数字が存在しなかったときのために
for j in range(1, mx_V2+1):
  V2_cnt.append([j, 0])
for v in V2:
  V2_cnt[v][1] += 1  
V2_cnt.sort(key=lambda x: x[1], reverse=True)

mxnum_V1_cnt = V1_cnt[0][0] #V1の中で一番出現回数多い数字
mxnum_V2_cnt = V2_cnt[0][0] #V2の中で一番出現回数多い数字
if mxnum_V1_cnt != mxnum_V2_cnt:
  ans = n - (V1_cnt[0][1] + V2_cnt[0][1])
else:
  ans = n - max(V1_cnt[0][1] + V2_cnt[1][1], V1_cnt[1][1] + V2_cnt[0][1])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque




def main():
    num = int(input())
    data = list(map(int, input().split()))
    odd_list = Counter(data[::2])
    even_list = Counter(data[1::2])
    odd_list = sorted(odd_list.items(), key=lambda x: -x[1])
    even_list = sorted(even_list.items(), key=lambda x: -x[1])

    a, b = num // 2, num // 2
    if odd_list[0][0] != even_list[0][0]:
        a = even_list[0][1]
        b = odd_list[0][1]
    elif len(odd_list) == 1 and len(even_list) == 1:
        a = 0
    elif len(odd_list) == 1:
        a = even_list[1][1]
    elif len(even_list) == 1:
        a = odd_list[1][1]
    else:
        if odd_list[0][1] + even_list[1][1] > odd_list[1][1] + even_list[0][1]:
            a = odd_list[0][1]
            b = even_list[1][1]
        else:
            a = odd_list[1][1]
            b = even_list[0][1]
    ans = num - a - b
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
c1, c2 = Counter(v[::2]), Counter(v[1::2])
mc1, mc2 = c1.most_common(2), c2.most_common(2)
mc1.append((0, 0))
mc2.append((0, 0))

print(n - (max(mc1[0][1] + mc2[1][1], mc1[1][1] + mc2[0][1]) if mc1[0][0] == mc2[0][0] else mc1[0][1] + mc2[0][1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

n=int(input())
v=list(map(int,input().split()))
v0=v[::2]
v1=v[1::2]

v0_c=collections.Counter(v0)
m0=v0_c.most_common()
v1_c=collections.Counter(v1)
m1=v1_c.most_common()
#print(m0)
#print(m1)

if len(m0)==1:
	mm0=[[m0[0][0],len(v0)-m0[0][1]], ["X",len(v0)]]
else:
  	mm0=[[m0[0][0],len(v0)-m0[0][1]], [m0[1][0],len(v0)-m0[1][1]]]
if len(m1)==1:
	mm1=[[m1[0][0],len(v1)-m1[0][1]], ["X",len(v1)]]
else:
  	mm1=[[m1[0][0],len(v1)-m1[0][1]], [m1[1][0],len(v1)-m1[1][1]]]

print(mm0[0][1]+mm1[0][1] if mm0[0][0]!=mm1[0][0] else min(mm0[0][1]+mm1[1][1],mm0[1][1]+mm1[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter
from itertools import product

NUM = 0
CNT = 1

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

c_odd = Counter(v[1::2])
c_even = Counter(v[0::2])

odd_12 = c_odd.most_common()[:2]
even_12 = c_even.most_common()[:2]
# [(num, cnt),(num, cnt)]

ans = n
for o, e in product(odd_12 + [(-1, 0)], even_12 + [(-1, 0)]):
    # print(o, e)
    if o[NUM] == e[NUM]:
        continue
    ans = min(ans, n - (o[CNT] + e[CNT]))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
o=[[0,i] for i in range(100001)]
e=[[0,i] for i in range(100001)]
for i in range(n//2):
 o[l[i*2]][0]+=1
 e[l[i*2+1]][0]+=1
o.sort(reverse=True)
e.sort(reverse=True)
res=10000000000000000
for i in range(2):
  for j in range(2):
    if o[i][1]!=e[j][1]:
      res=min(res,n-o[i][0]-e[j][0])
print(res)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
def main():
    def input():
        return sys.stdin.readline()[:-1]
    n = int(input())
    v = list(map(int,input().split()))
    o = []
    e = []
    for k in range(n):
        if k%2 == 0:
            e.append(v[k])
        else:
            o.append(v[k])
    c = Counter(e).most_common()
    d = Counter(o).most_common()
    if c[0][0] != d[0][0]:
        print(n-c[0][1]-d[0][1])
    else:
        if len(c) >= 2 and len(d) >= 2:
            print(min(n-d[0][1]-c[1][1],n-d[1][1]-c[0][1]))
        elif len(c) >= 2 and len(d) == 1:
            print(n-d[0][1]-c[1][1])
        elif len(d) >= 2 and len(c) == 1:
            print(n-d[1][1]-c[0][1])
        else:
            print(n//2)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# ABC111C - /\/\/\/
from collections import Counter


def main():
    N, *V = open(0).read().split()
    N = int(N)
    even, odd = Counter(V[::2]).most_common(2), Counter(V[1::2]).most_common(2)
    if even[0][0] != odd[0][0]:  # replace except the modes of both parities
        ans = N - even[0][1] - odd[0][1]
    else:
        if len(even) == 1 or len(odd) == 1:
            ans = N // 2  #  replace half of the sequence
        else:  # replace smaller pair of even no.1 & odd 2 / even 2 & odd 1
            ans = N - max(even[0][1] + odd[1][1], even[1][1] + odd[0][1])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC111C - /\/\/\/
from collections import Counter


def main():
    N, *V = map(int, open(0).read().split())
    even, odd = Counter(V[::2]).most_common(2), Counter(V[1::2]).most_common(2)
    if even[0][0] != odd[0][0]:  # replace except the modes of both parities
        ans = N - even[0][1] - odd[0][1]
    else:
        if len(even) == 1 or len(odd) == 1:
            ans = N // 2  #  replace half of the sequence
        else:  # replace smaller pair of even no.1 & odd 2 / even 2 & odd 1
            ans = N - max(even[0][1] + odd[1][1], even[1][1] + odd[0][1])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC111 C

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
v = LI()
s,t = [],[]
for i in range(n):
    if i % 2:
        t.append(v[i])
    else:
        s.append(v[i])
s = list(Counter(s).items())
s.sort(key = itemgetter(1),reverse = True)
t = list(Counter(t).items())
t.sort(key = itemgetter(1),reverse = True)
m = min(len(s),len(t))
for i in range(m):
    a,b = s[i]
    c,d = t[i]
    if a != c:
        print((n//2 - b)+(n//2 - d))
        quit()
    elif a == c:
        if i + 1 < len(s) - 1:
            tmp1 = s[i+1][1]
        else:
            tmp1 = 0
        if i + 1 < len(t) - 1:
            tmp2 = t[i+1][1]
        else:
            tmp2 = 0
        print(min(((n//2 - b) + (n//2 - tmp2),((n//2 - d) + (n//2 - tmp1)))))
        quit()

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int,input().split()))
e = v[0::2]
o = v[1::2]
e_com = collections.Counter(e).most_common(2)
o_com = collections.Counter(o).most_common(2)
if e_com[0][0] != o_com[0][0]:
  print(n - e_com[0][1] - o_com[0][1])
elif e_com[0][0] == o_com[0][0]:
  if len(e_com) > 1 and len(o_com)>1:
    print(min(n - e_com[1][1] - o_com[0][1], n - e_com[0][1] - o_com[1][1]))
  elif len(e_com) > 1 and len(o_com) == 1:
    print(n - e_com[1][1])
  elif len(e_com) == 1 and len(o_com) > 1:
    print(n - o_com[1][1])
  else:
    print(n//2)

Submission status: Accepted

Solution code:
from collections import Counter
from statistics import mode


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

A = V[::2]
B = V[1::2]
countA = sorted(list(Counter(A).items()), key=lambda x: -x[1])
countB = sorted(list(Counter(B).items()), key=lambda x: -x[1])
if len(set(V)) == 1:
    print(n//2)
elif countA[0][0] != countB[0][0]:
    print(len(A)-countA[0][1] + len(B)-countB[0][1])
else:
    print(min(
        len(A)-countA[0][1] + len(B)-countB[1][1],
        len(A)-countA[1][1] + len(B)-countB[0][1]
        ))

Submission status: Accepted

Solution code:
from collections import Counter

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

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

cv1 = Counter(v1).most_common(2)
cv2 = Counter(v2).most_common(2)

if cv1[0][0] != cv2[0][0]:
    print(n - cv1[0][1] - cv2[0][1])
else:
    p1 = cv1[0][1] + cv2[1][1] if len(cv2) >= 2 else n - cv1[0][1]
    p2 = cv2[0][1] + cv1[1][1] if len(cv1) >= 2 else n - cv2[0][1]
    print(n - max(p1, p2))

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

Solution code:
import collections
n = int(input())
V = [int(i) for i in input().split()]
E,O = [],[]
E_cnt2,O_cnt2 = 0,0
for i in range(n):
    if i%2:
        E.append(V[i])
    else:
        O.append(V[i])
e = collections.Counter(E)
tmp = e.most_common()
E_mode,E_cnt = tmp[0][0],tmp[0][1]
if len(tmp)>=2:
    E_cnt2 = tmp[1][1]

o = collections.Counter(O)
tmp = o.most_common()
O_mode,O_cnt = tmp[0][0],tmp[0][1]
if len(tmp)>=2:
    O_cnt2 = tmp[1][1]
if E_mode!=O_mode:
    print(len(E)-E_cnt+len(O)-O_cnt)
else:
    print(min((len(E)-E_cnt+len(O)-O_cnt2),len(E)-E_cnt2+len(O)-O_cnt))

Submission status: Accepted

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

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

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

vvv_odd = vvv[::2]
vvv_even = vvv[1::2]

dp_odd = [0] * max(vvv_odd)
dp_even = [0] * max(vvv_even)

for v_odd, v_even in zip(vvv_odd, vvv_even):
    dp_odd[v_odd - 1] += 1
    dp_even[v_even - 1] += 1

max_appear_odd = max(dp_odd)
max_appear_even = max(dp_even)

max_odd_val = dp_odd.index(max_appear_odd) + 1
max_even_val = dp_even.index(max_appear_even) + 1

if max_odd_val == max_even_val:
    next_appear_odd = sorted(dp_odd, reverse=True)[1]
    next_appear_even = sorted(dp_even, reverse=True)[1]
    if next_appear_odd > next_appear_even:
        max_appear_odd = next_appear_odd
    else:
        max_appear_even = next_appear_even

n_replace = N // 2 - max_appear_odd
n_replace += N // 2 - max_appear_even

print(n_replace)

Submission status: Accepted

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

v1=v[::2]
v2=v[1::2]
dic1=Counter(v1)
dic2=Counter(v2)
dic1=sorted(dic1.items(),key=lambda x:x[1],reverse=True)
dic2=sorted(dic2.items(),key=lambda x:x[1],reverse=True)

if n==2:
    if v[0]==v[1]:
        ans=1
    else:
        ans=0

elif dic1[0][0]!=dic2[0][0]:
    ans=len(v1)-dic1[0][1]
    ans+=(len(v2)-dic2[0][1])

elif dic1[0][0]==dic2[0][0] and len(v1)==dic1[0][1] and len(v2)==dic2[0][1]:
    ans=len(v2)

elif dic1[0][0]==dic2[0][0] and len(v1)==dic1[0][1] and len(v2)!=dic2[0][1]:
    ans=len(v2)-dic2[1][1]
    
elif dic1[0][0]==dic2[0][0] and len(v1)!=dic1[0][1] and len(v2)==dic2[0][1]:
    ans=len(v1)-dic1[1][1]
    
elif dic1[0][0]==dic2[0][0]:
    ans = min(len(v1)-dic1[0][1]+len(v2)-dic2[1][1],len(v1)-dic1[1][1]+len(v2)-dic2[0][1])
print(ans)

Submission status: Accepted

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

even = {}
odd = {}

for i in range(n):
    if i % 2 == 0:
        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

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

ans = 0
if even_sorted[0][0] != odd_sorted[0][0]:
    ans += n - even_sorted[0][1] - odd_sorted[0][1]
else:
    if len(even_sorted) == 1 or len(odd_sorted) == 1:
        even_sorted.append((-1, 0))
        odd_sorted.append((-1, 0))
    x = n - even_sorted[0][1] - odd_sorted[1][1]
    y = n - even_sorted[1][1] - odd_sorted[0][1]
    ans += min(x, y)

print(ans)

Submission status: Accepted

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

dl = {}
du = {}

for i in range(n):
  v = l[i]
  if i%2 == 0:
    if v in dl:
      dl[v] += 1
    else:
      dl[v] = 1
  else:
    if v in du:
      du[v] += 1
    else:
      du[v] = 1

if max(dl, key=dl.get) != max(du, key=du.get):
  ans = n - max(dl.values()) - max(du.values())
else:
  vsl = list(dl.values())
  vsl.sort(reverse=True)
  vsu = list(du.values())
  vsu.sort(reverse=True)
  if len(vsl) == 1 or len(vsu) == 1:
    ans = n//2
  else:
    ans = min(n-vsu[0]-vsl[1], n-vsl[0]-vsu[1])
  
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = list(map(int, input().split()))
v_even = []
v_odd = []

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

even = Counter(v_even).most_common(2)+[(0,0)]
odd = Counter(v_odd).most_common(2)+[(0,0)]

if even[0][0] != odd[0][0]:
    ans = n - even[0][1] - odd[0][1]
else:
    temp1 = even[0][1] + odd[1][1]
    temp2 = even[1][1] + odd[0][1]
    temp = max(temp1, temp2)
    ans = n - temp

print(ans)

Submission status: Accepted

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

from collections import defaultdict

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

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

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

if d_even[0][0] == d_odd[0][0]:
    d_even.append((0, 0))
    d_odd.append((0, 0))
    tmp = max(d_even[0][1] + d_odd[1][1], d_even[1][1] + d_odd[0][1])
    ans = n - tmp
else:
    ans = n - d_even[0][1] - d_odd[0][1]

print(ans)

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(2)
B = Counter(V[1::2]).most_common(2)

A.append((0, 0))
B.append((0, 0))

a_value, a_count = A[0]
a2_value, a2_count = A[1]
b_value, b_count = B[0]
b2_value, b2_count = B[1]

if a_value != b_value:
    ans = n - a_count - b_count
else:
    ans = min(n-a_count-b2_count, n-a2_count-b_count)

print(ans)

Submission status: Accepted

Solution code:
import collections

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

counter_e = collections.Counter()
counter_o = collections.Counter()
for i, v in enumerate(vs):
    if i % 2 == 0:
        counter_e[v] += 1
    else:
        counter_o[v] += 1
key_e, max_e = sorted(counter_e.items(), key=lambda x: (x[1], -x[0]))[-1]
key_o, max_o = sorted(counter_o.items(), key=lambda x: (x[1], x[0]))[-1]
if key_e != key_o:
    print(n - max_e - max_o)
elif len(counter_e.keys()) == 1 and len(counter_o.keys()) == 1:
    print(n // 2)
elif len(counter_e.keys()) == 1:
    key_o2, max_o2 = sorted(counter_o.items(), key=lambda x: (x[1], x[0]))[-2]
    print(n - max_e - max_o2)
elif len(counter_e.keys()) == 2 and len(counter_o.keys()) == 1:
    key_e2, max_e2 = sorted(counter_e.items(), key=lambda x: (x[1], -x[0]))[-2]
    print(n - max_e2 - max_o)
else:
    key_e2, max_e2 = sorted(counter_e.items(), key=lambda x: (x[1], -x[0]))[-2]
    key_o2, max_o2 = sorted(counter_o.items(), key=lambda x: (x[1], x[0]))[-2]
    print(min(n - max_e - max_o2, n - max_e2 - max_o))

Submission status: Accepted

Solution code:
import collections

n = int(input())
v = list(map(int,input().split()))
v1 = v[::2]
v2 = v[1::2]
v1 = collections.Counter(v1)
v2 = collections.Counter(v2)
ans = 0
if v1.most_common()[0][0] == v2.most_common()[0][0]:
    if v1.most_common()[0][1] < v2.most_common()[0][1]:
        ans += n//2 - v2.most_common()[0][1]
        ans += n//2 - v1.most_common()[1][1]
        print(ans)
    elif v1.most_common()[0][1] > v2.most_common()[0][1]:
        ans += n//2 - v1.most_common()[0][1]
        ans += n//2 - v2.most_common()[0][1]
        print(ans)
    else:
        if v1.most_common()[0][1] == n//2:
            print(n//2)
        else:
            if v1.most_common()[1][1] > v2.most_common()[1][1]:
                ans += n//2 - v2.most_common()[0][1]
                ans += n//2 - v1.most_common()[1][1]
                print(ans)
            else:
                ans += n//2 - v1.most_common()[0][1]
                ans += n//2 - v2.most_common()[1][1]
                print(ans)
else:
    ans += n//2 - v1.most_common()[0][1]
    ans += n//2 - v2.most_common()[0][1]
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
V = list(map(int,input().split()))
V_odd = []
V_even = []
for i in range(n):
    if i % 2 == 0:
        V_odd += [V[i]]
    else:
        V_even += [V[i]]
V_odd = list(Counter(V_odd).most_common()) + [(-1,0)]
V_even = list(Counter(V_even).most_common()) + [(-2,0)]
if V_odd[0][0] != V_even[0][0]:
    print(len(V)-V_odd[0][1]-V_even[0][1])
elif V_odd[1][1] >= V_even[1][1]:
    print(len(V)-V_odd[1][1]-V_even[0][1])
else:
    print(len(V)-V_odd[0][1]-V_even[1][1])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
E = Counter(v[::2]).most_common()
O = Counter(v[1::2]).most_common()
if E[0][0] != O[0][0]:
    print(n - E[0][1] - O[0][1])
else:
    if E[0][1] == O[0][1] == n //2:
        print(E[0][1])
    else:
        print(min(n-E[1][1]-O[0][1], n-E[0][1]-O[1][1]))

Submission status: Accepted

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

import heapq

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

A_even=[]
A_odd=[]
A_evenq = []
A_oddq = []
heapq.heapify(A_evenq)
heapq.heapify(A_oddq)

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

A_even.sort()
A_odd.sort()
i=0
max_even=0
i_even=0
while i < N//2:
    j = i
    count=0
    while j < N//2:
        if A_even[i] == A_even[j]:
            count+=1
            j+=1
        else:
            heapq.heappush(A_evenq,-count)
            if max_even < count:
                max_even=count
                i_even = A_even[i]
            break
    i=j
heapq.heappush(A_evenq,-count)
if max_even < count:
    max_even=count
    i_even = A_even[i-1]

i=0
max_odd=0
i_odd=0
while i < N//2:
    j = i
    count=0
    while j < N//2:
        if A_odd[i] == A_odd[j]:
            count+=1
            j+=1
        else:
            heapq.heappush(A_oddq,-count)
            if max_odd < count:
                max_odd=count
                i_odd = A_odd[i]
            break
    i=j
heapq.heappush(A_oddq,-count)
if max_odd < count:
    max_odd=count
    i_odd = A_odd[i-1]

ans=N-max_odd-max_even
if i_even == i_odd:
    if len(A_evenq) == 1 and len(A_oddq) == 1:
        ans = N//2
    elif len(A_evenq) == 1:
        heapq.heappop(A_oddq)
        odd2 = -heapq.heappop(A_oddq)
        ans = N-max_even-odd2
    elif len(A_oddq) == 1:
        heapq.heappop(A_evenq)
        even2 = -heapq.heappop(A_evenq)
        ans = N-max_odd-even2
    else:
        ans=0
        heapq.heappop(A_oddq)
        odd2 = -heapq.heappop(A_oddq)
        heapq.heappop(A_evenq)
        even2 = -heapq.heappop(A_evenq)
        ans = min(N-even2-max_odd,N-odd2-max_even)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
V=list(map(int,input().split()))
E=Counter(V[::2]).most_common()
O=Counter(V[1::2]).most_common()
if E[0][0]!=O[0][0]:
    print(n-E[0][1]-O[0][1])
else:
    if E[0][1]==O[0][1]==n//2:
        print(E[0][1])
    else:
        print(min(n-E[1][1]-O[0][1],n-E[0][1]-O[1][1]))

Submission status: Accepted

Solution code:
N = int(input())
vs = list(map(int, input().split()))
vs0 = vs[0::2]
vs1 = vs[1::2]
M0 = {}
for v in vs0:
  if not(v in M0):
    M0[v] = 0
  M0[v] += 1
M1 = {}
for v in vs1:
  if not(v in M1):
    M1[v] = 0
  M1[v] += 1
L0 = []
for v in M0:
  n = M0[v]
  L0.append((n, v))
L0.sort(reverse=True)
L0.append((0, -1))
L1 = []
for v in M1:
  n = M1[v]
  L1.append((n, v))
L1.sort(reverse=True)
L1.append((0, -1))
#print(L0)
#print(L1)
if L0[0][1] == L1[0][1]:
  r = min(N-L0[1][0]-L1[0][0], N-L0[0][0]-L1[1][0])
else:
  r = N-L0[0][0]-L1[0][0]
print(r)

Submission status: Accepted

Solution code:
n = int(input())
nums = list(map(int, input().split()))
 
nums1 = nums[0::2]
nums2 = nums[1::2]
 
bug1 = [0] * 100001
bug2 = [0] * 100001
 
for num in nums1:
    bug1[num] += 1
 
for num in nums2:
    bug2[num] += 1
 
max_v_1 = max(bug1)
max_i_1 = bug1.index(max_v_1)
max_v_2 = max(bug2)
max_i_2 = bug2.index(max_v_2)
 
if max_i_1 == max_i_2:
    bug1[max_i_1] = 0
    bug2[max_i_2] = 0
    sec_max_v_1 = max(bug1)
    sec_max_v_2 = max(bug2)
    print(n - max(max_v_1, max_v_2) - max(sec_max_v_1, sec_max_v_2))
else:
    print(n - max_v_1 - max_v_2)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
V=list(map(int,input().split()))
E=Counter(V[::2]).most_common()+[(0,0)]
O=Counter(V[1::2]).most_common()+[(0,0)]
print(n-E[0][1]-O[0][1] if E[0][0]!=O[0][0] else n-max(E[0][1]+O[1][1],O[0][1]+E[1][1]))

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
V=list(map(int,input().split()))
E=Counter(V[::2]).most_common()
O=Counter(V[1::2]).most_common()
if E[0][0]==O[0][0]:
	E2=Counter(V[1::2]).most_common()[1][1] if len(E)>1 else 0
	O2=Counter(V[::2]).most_common()[1][1] if len(O)>1 else 0 
print(n-E[0][1]-O[0][1] if E[0][0]!=O[0][0] else n-max(E[0][1]+E2,O[0][1]+O2))