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:
#!/usr/bin/env python3

import sys
# import math
# from string import ascii_lowercase, ascii_upper_case, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)


def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1               # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n = int(input())
    L = list(map(int, input().split()))
    L_even = L[0:n:2]
    L_odd = L[1:n:2]

    def calc_appeared_times(L):
        d = dict()
        maximum = 0
        appeared_num = None
        for num in L:
            if num in d:
                d[num] += 1
            else:
                d[num] = 1
            if d[num] > maximum:
                maximum = d[num]
                appeared_num = num
        return appeared_num, d

    even_max_appeared, d_even = calc_appeared_times(L_even)
    odd_max_appeared, d_odd = calc_appeared_times(L_odd)
    if even_max_appeared != odd_max_appeared:
        print(n - d_even[even_max_appeared] - d_odd[odd_max_appeared])
    else:
        second_max_even = 0
        for k, v in d_even.items():
            if k != even_max_appeared and v > second_max_even:
                second_max_even = v
        second_max_odd = 0
        for k, v in d_odd.items():
            if k != odd_max_appeared and v > second_max_odd:
                second_max_odd = v
        if second_max_even >= second_max_odd:
            print(n - second_max_even - d_odd[odd_max_appeared])
        else:
            print(n - second_max_odd - d_even[even_max_appeared])


if __name__ == "__main__":
    main()

Submission status: Accepted

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

c_0 = collections.Counter(a[::2])
c_1 = collections.Counter(a[1::2])

if len(c_0.most_common()) == 1 and len(c_1.most_common()) == 1:
  if c_0.most_common()[0][0] == c_1.most_common()[0][0]:
    print(n // 2)
  else:
    print(0)
elif len(c_0.most_common()) == 1:
  if c_0.most_common()[0][0] == c_1.most_common()[0][0]:
    print(n // 2 - c_1.most_common()[1][1])
  else:
    print(n // 2 - c_1.most_common()[0][1])
elif len(c_1.most_common()) == 1:
  if c_0.most_common()[0][0] == c_1.most_common()[0][0]:
    print(n // 2 - c_0.most_common()[1][1])
  else:
    print(n // 2 - c_0.most_common()[0][1])
elif c_0.most_common()[0][0] == c_1.most_common()[0][0]:
  if c_0.most_common()[0][1] > c_1.most_common()[0][1]:
    print(n - c_0.most_common()[0][1] - c_1.most_common()[1][1])
  elif c_0.most_common()[0][1] < c_1.most_common()[0][1]:
    print(n - c_0.most_common()[1][1] - c_1.most_common()[0][1])
  else:
    print(n - c_0.most_common()[0][1] - max(c_0.most_common()[1][1],c_1.most_common()[1][1]))
else:
  print(n - c_0.most_common()[0][1] - c_1.most_common()[0][1])

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
odd=[v[i] for i in range(1,n,2)]
even=[v[i] for i in range(0,n,2)]
from collections import defaultdict
d_odd=defaultdict(int)
d_even=defaultdict(int)
for i in range(len(odd)):
    d_odd[odd[i]]+=1
for i in range(len(even)):
    d_even[even[i]]+=1

d_odd=sorted(d_odd.items(),key=lambda x:x[1],reverse=True)
d_even=sorted(d_even.items(),key=lambda x:x[1],reverse=True)
if d_odd[0][0]!=d_even[0][0]:
    ans=(len(odd)-d_odd[0][1])+(len(even)-d_even[0][1])
else:
    if len(d_odd)==1 and len(d_even)==1:
        ans=len(odd)
    elif len(d_odd)==1:
        ans=(len(even)-d_even[1][1])
    elif len(d_even)==1:
        ans=(len(odd)-d_odd[1][1])
    else:
        x=(len(odd)-d_odd[1][1])+(len(even)-d_even[0][1])
        y=(len(odd)-d_odd[0][1])+(len(even)-d_even[1][1])
        ans=min(x,y)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
a=sorted(v[::2])
b=sorted(v[1::2])
import itertools,heapq
ha=[]
hb=[]
ga = itertools.groupby(a)
gb = itertools.groupby(b)
for key,group in ga:
  heapq.heappush(ha, [-len(list(group)),key])
for key,group in gb:
  heapq.heappush(hb, [-len(list(group)),key])
A=heapq.heappop(ha)
B=heapq.heappop(hb)
if A[1]==B[1]:
  A2=heapq.heappop(ha) if len(ha)>0 else [0,0]
  B2=heapq.heappop(hb) if len(hb)>0 else [0,0]
  print(n+min(A[0]+B2[0],A2[0]+B[0]))
else:
  print(n+A[0]+B[0])

Submission status: Accepted

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

C0 = collections.Counter(V[0::2])
C0[0] = 0

C1 = collections.Counter(V[1::2])
C1[0] = 0

if C0.most_common()[0][0] != C1.most_common()[0][0]:
    print(N-C0.most_common()[0][1]-C1.most_common()[0][1])
else:
    print(min(N-C0.most_common()[0][1]-C1.most_common()[1][1],
              N-C0.most_common()[1][1]-C1.most_common()[0][1]))

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))

l = []
r = []
al = []
for i, x in enumerate(A):
  if i % 2 == 0:
    l.append(x)
  else:
    r.append(x)
  al.append(x)

c = collections.Counter(l)
d = collections.Counter(r)
e = collections.Counter(al)

if len(e.keys()) <= 1:
  print(N//2)
  exit()
else:
  mx = -1
  ans = 0
  ck = max(c.items(), key=lambda x:x[1])[0]
  cmx = max(c.items(), key=lambda x:x[1])[1]

  dk = max(d.items(), key=lambda x:x[1])[0]
  dmx = max(d.items(), key=lambda x:x[1])[1]

  if ck == dk:
    if cmx > dmx:
      ans += N//2 - cmx
      ans += N//2 - d.most_common(2)[1][1]
    elif cmx < dmx:
      ans += N//2 - dmx
      ans += N//2 - c.most_common(2)[1][1]
    else:
      if c.most_common(2)[1][1] >= d.most_common(2)[1][1]:
        ans += N//2 - dmx
        ans += N//2 - c.most_common(2)[1][1]
      else:
        ans += N//2 - cmx
        ans += N//2 - d.most_common(2)[1][1]
  else:
      ans += N//2 - cmx
      ans += N//2 - dmx

print(ans)

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools
l = [0]*(10**5+10)
r = [0]*(10**5+10)

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

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

max_l = l.index(max(l))
max_r = r.index(max(r))
count = 0

if max_l == max_r:
    for i in range(len(r)):
        if r[i] == r[max_r]:
            r[i] = 0
        if l[i] == l[max_l]:
            l[i] = 0
if max(l) > max(r):
    max_l = l.index(max(l))
else:
    max_r = r.index(max(r))

for i in range(n):
    if i % 2 == 0 and v[i] != max_l:
        count += 1
    if i % 2 != 0 and v[i] != max_r:
        count += 1
print(count)

Submission status: Accepted

Solution code:
n = int(input())
u = list(map(int, input().split()))
u0 = u[0::2]
u1 = u[1::2]
s0 = [0] * 100001
s1 = [0] * 100001
for i in u0:
  s0[i] += 1
for i in u1:
  s1[i] += 1
maxi_0 = 0
maxi2_0 = 0
for i in range(100001):
  if s0[i] >= s0[maxi_0]:
    maxi2_0 = maxi_0
    maxi_0 = i
  elif s0[i] >= s0[maxi2_0]:
    maxi2_0 = i
maxi_1 = 0
maxi2_1 = 0
for i in range(100001):
  if s1[i] >= s1[maxi_1]:
    maxi2_1 = maxi_1
    maxi_1 = i
  elif s1[i] >= s1[maxi2_1]:
    maxi2_1 = i
if maxi_0 != maxi_1:
  print(n - s0[maxi_0] - s1[maxi_1])
else:
  print(n - max(s0[maxi_0] + s1[maxi2_1], s0[maxi2_0] + s1[maxi_1]))

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
dic1={}
dic2={}
for i in range(0,n):
  if i%2==0:
    at=v[i]
    if at in dic1:
      dic1[at]+=1
    else:
      dic1[at]=1
  else:
    at=v[i]
    if at in dic2:
      dic2[at]+=1
    else:
      dic2[at]=1 

count1=0
count2=0
kep1=0
kep2=0
for i in range(len(dic1)):
  at=dic1.popitem()
  moji=at[0]
  count=at[1]
  if count>count2:
    kep2=moji
    count2=count
  if count2>count1:
    moji_tamotu=kep1
    count_tamotu=count1
    kep1=kep2
    kep2=moji_tamotu
    count1=count2
    count2=count_tamotu

odd_1_moji=kep1
odd_1_count=count1
odd_2_moji=kep2
odd_2_count=count2
    
count1=0
count2=0
kep1=0
kep2=0
for i in range(len(dic2)):
  at=dic2.popitem()
  moji=at[0]
  count=at[1]
  if count>count2:
    kep2=moji
    count2=count
  if count2>count1:
    moji_tamotu=kep1
    count_tamotu=count1
    kep1=kep2
    kep2=moji_tamotu
    count1=count2
    count2=count_tamotu

even_1_moji=kep1
even_1_count=count1
even_2_moji=kep2
even_2_count=count2

if even_1_moji!=odd_1_moji:
  kouho=[odd_1_moji,even_1_moji]*(n//2)
else:
  if even_1_count+odd_2_count>=even_2_count+odd_1_count:
    kouho=[odd_2_moji,even_1_moji]*(n//2)
  else:
    kouho=[odd_1_moji,even_2_moji]*(n//2)
    
res=0
for i in range(0,n):
  if v[i]!=kouho[i]:
    res+=1
print(res)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
V = list(map(int, input().split()))
a1, a2, *_ = Counter(V[0::2]).most_common() + [(0, 0)]
b1, b2, *_ = Counter(V[1::2]).most_common() + [(0, 0)]
print(N - max(a1[1] + b2[1], a2[1] + b1[1]) if a1[0] == b1[0] else N - (a1[1] + b1[1]))

Submission status: Accepted

Solution code:
n = int(input())
V = list(map(int,input().split()))
import collections
co0 = collections.Counter()
co1 = collections.Counter()
for i in range(n):
    if i%2==0:
        co0[V[i]]+=1
    else:
        co1[V[i]]+=1
co0[-1] =0
co1[-1] =0
c0 = list(co0.items())
c0.sort(key=lambda x:x[1])
c1 = list(co1.items())
c1.sort(key=lambda x:x[1])
if c0[-1][0] == c1[-1][0]:
    b = max(c0[-1][1] + c1[-2][1],c0[-2][1] + c1[-1][1])
else:
    b = c0[-1][1] + c1[-1][1]
print(n-b)

Submission status: Accepted

Solution code:
from collections import defaultdict
from operator import itemgetter

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

temp = [defaultdict(int, {"": 0}), defaultdict(int, {"": 0})]
for i in range(0, n, 2):
    temp[0][v_list[i]] += 1
for i in range(1, n, 2):
    temp[1][v_list[i]] += 1

even_list = sorted(temp[0].items(), reverse=True, key=itemgetter(1))
odd_list = sorted(temp[1].items(), reverse=True, key=itemgetter(1))

if even_list[0][0] != odd_list[0][0]:
    ans = n - even_list[0][1] - odd_list[0][1]
else:
    ans = min(n - even_list[0][1] - odd_list[1][1], n - even_list[1][1] - odd_list[0][1])
print(ans)

Submission status: Accepted

Solution code:
import math
import fractions
from functools import reduce


def main():
    n = int(input())
    a = input_list()
    if len(set(a)) == 1:
        print(n//2)
        exit(0)
    evens = {}
    odds = {}
    for i, v in enumerate(a):
        if i % 2 == 0:
            if v in evens:
                evens[v] += 1
            else:
                evens[v] = 1
        else:
            if v in odds:
                odds[v] += 1
            else:
                odds[v] = 1
    if len(evens) == 1 and len(odds) == 1:
        print(0)
        exit(0)

    e = sorted(evens.items(), key=lambda x: x[1], reverse=True)
    o = sorted(odds.items(), key=lambda x: x[1], reverse=True)
    if e[0][0] != o[0][0]:
        even_ans = (n // 2) - e[0][1]
        odd_ans = (n // 2) if len(o) == 1 else (n // 2) - o[0][1]
        a1 = even_ans+odd_ans

        even_ans = (n // 2) if len(e) == 1 else (n // 2) - e[0][1]
        odd_ans = (n // 2) - o[0][1]
        a2 = even_ans+odd_ans
        print(min(a1, a2))
    else:
        even_ans = (n // 2) - e[0][1]
        odd_ans = (n // 2) if len(o) == 1 else (n // 2) - o[1][1]
        a1 = even_ans + odd_ans

        even_ans = (n // 2) if len(e) == 1 else (n // 2) - e[1][1]
        odd_ans = (n // 2) - o[0][1]
        a2 = even_ans + odd_ans
        print(min(a1, a2))

# 6
# 3 1 3 2 4 2

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

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

even = v[0:n:2]
odd = v[1:n:2]

c_even = [0]*(10**5+100)
c_odd = [0]*(10**5 + 100)

for i in even:
    c_even[i]+=1
for i in odd:
    c_odd[i]+=1

m_even = -1
m_odd = -1


for i,j in enumerate(c_even):
    if j > m_even:
        m_even = j 
        m_even_num = i

for i,j in enumerate(c_odd):
    if j > m_odd:
        m_odd = j 
        m_odd_num = i

if m_even_num == m_odd_num:
    print(min(n - m_odd - sorted(c_even,reverse=True)[1], n - m_even - sorted(c_odd,reverse=True)[1]))
else:
    print(n - m_even - m_odd)

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

ans2=float("inf")
ans3=float("inf")
ans4=float("inf")


if v1c[0][0]!=v2c[0][0]:
  ans=len(v1)-v1c[0][1]+len(v2)-v2c[0][1]
  print(ans)

else:
  if len(v1c) !=1:
    ans2=len(v1)-v1c[1][1]+len(v2)-v2c[0][1]
  if len(v2c) !=1:
    ans3=len(v1)-v1c[0][1]+len(v2)-v2c[1][1]
  else:
    ans4=min(len(v1),len(v2))
  print(min(ans2,ans3,ans4))

Submission status: Accepted

Solution code:
from collections import Counter


n = int(input())
m = list(map(int, input().split()))
a = Counter(m[0::2]).most_common()
b = Counter(m[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])))

Submission status: Accepted

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

a = [v[2*i] for i in range(int(n/2))]
b = [v[2*i+1] for i in range(int(n/2))]

from collections import Counter

x = Counter(a)
y = Counter(b)
x = list(x.items())
y = list(y.items())
x.sort(reverse=1,key=lambda p: p[1])
y.sort(reverse=1,key=lambda p: p[1])

if x[0][0] != y[0][0]:
    print(n-x[0][1]-y[0][1])
else:
    if x[0][0] == y[0][0]:
        if min(len(x),len(y))>=2:
            print(n-max(x[0][1]+y[1][1],y[0][1]+x[1][1]))
        elif len(x)==2:
            print(n-x[1][1]-y[0][1])
        elif len(y)==2:
            print(n-y[1][1]-x[0][1])
        else:
            print(int(n/2))

Submission status: Accepted

Solution code:
from collections import Counter

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

C0 = Counter(V[0::2]).most_common()
C0.append((0, 0))
C1 = Counter(V[1::2]).most_common()
C1.append((0, 0))
ans = N
for k0, v0 in C0:
    for k1, v1 in C1:
        if k0 != k1:
            ans = min(ans, N - v0 - v1)
            break
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
ans = 0

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

odd = V[0::2]
even = V[1::2]

odd_count = np.bincount(odd)
odd_argmax = np.argmax(odd_count)
odd_count_num = odd_count[odd_argmax]
odd_count[odd_argmax] = -1
odd_argmax_second = np.argmax(odd_count)
odd_count_num_second = odd_count[odd_argmax_second]
# print("odd_argmax", odd_argmax)
# print("odd_count_num", odd_count_num)
# print("odd_argmax_second", odd_argmax_second)
# print("odd_count_num_second", odd_count_num_second)

even_count = np.bincount(even)
even_argmax = np.argmax(even_count)
even_count_num = even_count[even_argmax]
# print(even_count)
even_count[even_argmax] = 0
even_argmax_second = np.argmax(even_count)
even_count_num_second = even_count[even_argmax_second]
# print(even_count)
# print("even_argmax", even_argmax)
# print("even_count_num", even_count_num)
# print("even_argmax_second", even_argmax_second)
# print("even_count_num_second", even_count_num_second)

if odd_argmax == even_argmax:
    ans = min(half_n - odd_count_num + half_n - even_count_num_second, half_n - odd_count_num_second + half_n - even_count_num)
else:
    ans += half_n - odd_count_num
    ans += half_n - even_count_num
print(ans)

Submission status: Accepted

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

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

even = [l[_] for _ in range(n) if _%2 ==1]
odd = [l[_] for _ in range(n) if _%2 ==0]

from collections import Counter

odd_cnt = Counter(odd)
even_cnt = Counter(even)

if odd_cnt.most_common()[0][0] != even_cnt.most_common()[0][0]:
    print(len(odd) - odd_cnt.most_common()[0][1] + len(even) - even_cnt.most_common()[0][1])
else:
    if odd_cnt.most_common()[1][1] >= even_cnt.most_common()[1][1]:
        print(len(odd) - odd_cnt.most_common()[1][1] + len(even) - even_cnt.most_common()[0][1])
    else:
        print(len(odd) - odd_cnt.most_common()[0][1] + len(even) - even_cnt.most_common()[1][1])

Submission status: Accepted

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

c1 = Counter(V[::2])
c2 = Counter(V[1::2])
c1_items = sorted(c1.items(), key=lambda x: -x[1])
c2_items = sorted(c2.items(), key=lambda x: -x[1])

if c1_items[0][0] == c2_items[0][0]:
    if len(c1_items) == 1 and len(c2_items) == 1:
        ans = n // 2
    elif len(c1_items) == 1:
        ans = n - c1_items[0][1] - c2_items[1][1]
    elif len(c2_items) == 1:
        ans = n - c1_items[0][1] - c2_items[1][1]
    else:
        v1 = n - c1_items[0][1] - c2_items[1][1]
        v2 = n - c1_items[1][1] - c2_items[0][1]
        ans = min(v1, v2)
else:
    ans = n - c1_items[0][1] - c2_items[0][1]
print(ans)

Submission status: Accepted

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

c1 = Counter(V[::2])
c2 = Counter(V[1::2])
# print('c1, c2', c1, c2)

c1_items = sorted(c1.items(), key=lambda x: -x[1])
c2_items = sorted(c2.items(), key=lambda x: -x[1])
c1_max = c1_items[0][1]
c2_max = c2_items[0][1]
c1_second = 0
c2_second = 0

c1_max_group = set()
for key, value in c1_items:
    if value == c1_max:
        c1_max_group.add(key)
    else:
        c1_second = value
        break
c2_max_group = set()
for key, value in c2_items:
    if value == c2_max:
        c2_max_group.add(key)
    else:
        c2_second = value
        break

# print(c1_max_group, c1_max, c1_second)
# print(c2_max_group, c2_max, c2_second)
if len(c1_max_group) == 1 and len(c2_max_group) == 1 and list(
        c1_max_group)[0] in c2_max_group:
    v1 = n - c1_max - c2_second
    v2 = n - c1_second - c2_max
    ans = min(v1, v2)
else:
    ans = n - c1_max - c2_max
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import collections

def search_b(a_char, a_count, b_most_common, fixed_count):
  thres = fixed_count - a_count
  for (b_char, b_count) in [b for b in b_most_common if b[1] > thres]:
    if a_char != b_char:
      return a_count + b_count
  
  return fixed_count


def search(a_most_common, b_most_common):
  fixed_count = -1
  b_max = b_most_common[0][1]

  for (a_char, a_count) in a_most_common:
    if a_count <= fixed_count - b_max:
      break

    fixed_count = search_b(a_char, a_count, b_most_common, fixed_count)
  
  return fixed_count


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

  a = V[::2]
  b = V[1::2]
  
  a_counter = collections.Counter(a)
  b_counter = collections.Counter(b)

  a_most_common = a_counter.most_common()
  b_most_common = b_counter.most_common()

  if len(a_most_common) == 1 and len(b_most_common) == 1 and a_most_common[0][0] == b_most_common[0][0]:
    print(n // 2)
    return

  print(n - search(a_most_common, b_most_common))


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import collections

n = int(input())
li = list(map(int,input().split()))
x = li[::2]
y = li[1::2]
c = collections.Counter(li)
c0 = collections.Counter(x)
c1 = collections.Counter(y)

if c.most_common()[0][1] == len(li): #1種類だけのリストだったら
    print(n//2)

elif c0.most_common()[0][0] != c1.most_common()[0][0]: #c0に最多ものとc1に最多のものが違うなら
    print(n - c0.most_common()[0][1] - c1.most_common()[0][1])

else:
    if len(c0.keys())==1: #c0が一種類だったら
        print(n//2 - c1.most_common()[1][1]) #c1は2番目に多いものに帰る
    elif len(c1.keys())==1: #c1が一種類だったら
        print(n//2 - c0.most_common()[1][1]) #c0は2番目に多いものに帰る
    elif c0.most_common()[0][1] + c1.most_common()[1][1] < c0.most_common()[1][1] + c1.most_common()[0][1]:
        print(n - c0.most_common()[1][1] - c1.most_common()[0][1])
    else:
        print(n - c0.most_common()[0][1] - c1.most_common()[1][1])

Submission status: Accepted

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

# 奇数番目を見る
odd_counter = collections.Counter(Vs[0::2])
odd_counter_tuple = odd_counter.most_common()
# 頻度1番目と2番目のkeyを取り出す
if len(odd_counter_tuple) == 1:
    odd1 = odd_counter_tuple[0][0]
    odd2 = None
else:
    odd1 = odd_counter_tuple[0][0]
    odd2 = odd_counter_tuple[1][0]

# 偶数番目を見る
even_counter = collections.Counter(Vs[1::2])
even_counter_tuple = even_counter.most_common()
# 頻度1番目と2番目のkeyを取り出す
if len(even_counter_tuple) == 1:
    even1 = even_counter_tuple[0][0]
    even2 = None
else:
    even1 = even_counter_tuple[0][0]
    even2 = even_counter_tuple[1][0]

if odd1 != even1:
    ans = n - odd_counter[odd1] - even_counter[even1]
else:
    ans = min(n-odd_counter[odd2]-even_counter[even1], n-odd_counter[odd1]-even_counter[even2])

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#https://atcoder.jp/contests/abc111/tasks/arc103_a
n=ii()
arr=limii()
arra=[]
arrb=[]

for i in range(n):
    if i%2==0:
        arra.append(arr[i])
    else:
        arrb.append(arr[i])
    
r=list(Counter(arra).most_common(2))
s=list(Counter(arrb).most_common(2))

if len(r)==1 and len(s)==1:
    if r[0][0]==s[0][0]:
        print(len(arra))
        exit()
    else:
        print(0)
        exit()

if r[0][0]==s[0][0]:
    if r[0][1]==s[0][1]:
        print(len(arra)-max(r[0][1],s[0][1]) + len(arra)-max(r[1][1],s[1][1]))
        exit()
    else:
        print(len(arra)-max(r[0][1],s[0][1]) + len(arra)-max(r[1][1],s[1][1]))
        exit()
else:
    print(len(arra)-r[0][1]+len(arra)-s[0][1])
    exit()

#print(n-(c+d))

Submission status: Accepted

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

lst1 = lst[::2]
lst2 = lst[1::2]

count1 = Counter(lst1)
count2 = Counter(lst2)
  
if count1.most_common()[0][0] != count2.most_common()[0][0]:
  ans = n - count1.most_common()[0][1] - count2.most_common()[0][1]

else:
  if len(count1) != 1 and len(count2) != 1:
    k = max(count1.most_common()[0][1] + count2.most_common()[1][1], count1.most_common()[1][1] + count2.most_common()[0][1])
    ans = n - k
  elif len(count1) == 1 and len(count2) == 1:
    ans = n//2
  else:
    if len(count1) < len(count2):
      ans = n//2 - count2.most_common()[0][1]
    else:
      ans = n//2 - count1.most_common()[0][1]
print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))

l = []
r = []
al = []
for i, x in enumerate(A):
  if i % 2 == 0:
    l.append(x)
  else:
    r.append(x)
  al.append(x)

c = collections.Counter(l)
d = collections.Counter(r)
e = collections.Counter(al)

if len(e.keys()) <= 1:
  print(N//2)
  exit()
else:
  mx = -1
  ans = 0
  ck = max(c.items(), key=lambda x:x[1])[0]
  cmx = max(c.items(), key=lambda x:x[1])[1]

  dk = max(d.items(), key=lambda x:x[1])[0]
  dmx = max(d.items(), key=lambda x:x[1])[1]

  if ck == dk:
    if cmx > dmx:
      ans += N//2 - cmx
      print(ans)
      print(c.most_common(2))
      print(d.most_common(2))
      ans += N//2 - (d.most_common(2)[1][1])
    elif cmx < dmx:
      ans += N//2 - dmx
      ans += N//2 - (c.most_common(2)[1][1])
    else:
      if c.most_common(2)[1][1] >= d.most_common(2)[1][1]:
        ans += N//2 - dmx
        ans += N//2 - (c.most_common(2)[1][1])
      else:
        ans += N//2 - cmx
        ans += N//2 - d.most_common(2)[1][1]
  else:
      ans += N//2 - cmx
      ans += N//2 - dmx

print(ans)

Submission status: Accepted

Solution code:
import sys
import os
import math
import bisect
import collections
import itertools

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    V = il()
    odd = collections.Counter(V[0::2]).most_common()
    even = collections.Counter(V[1::2]).most_common()

    odd.append([0, 0])
    even.append([0, 0])

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int, input().split()))
e=[]
o=[]
for i, vi in enumerate(v,start=0):
  if i%2 == 0:
    e.append(vi)
  else:
    o.append(vi)
ec=collections.Counter(e)
oc=collections.Counter(o)

ecs = sorted(ec.items(), key=lambda x:x[1],reverse=True)
ocs = sorted(oc.items(), key=lambda x:x[1],reverse=True)

t=ecs[0][1]+ocs[0][1]
if ecs[0][0] == ocs[0][0]:
  if ecs[0][1] == ocs[0][1]:
    te = 0 if len(ecs) < 2 else ecs[1][1]
    to = 0 if len(ocs) < 2 else ocs[1][1]
    t=max(te,to)+ecs[0][1]
print(n-t)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
l=list(map(int,input().split()))
f=l[0::2]
s=l[1::2]
fn=sorted(Counter(f).items(),key=lambda x:x[1],reverse=True) + [(0, 0)]
sn=sorted(Counter(s).items(),key=lambda x:x[1],reverse=True) + [(0, 0)]
if fn[0][0]!=sn[0][0]:
  ans=n-fn[0][1]-sn[0][1]
else:
  ans=n-max(fn[1][1]+sn[0][1],fn[0][1]+sn[1][1])
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import Counter
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(n: int, v: "List[int]"):
    v1 = v[::2]
    v2 = v[1::2]
    c1 = Counter(v1)
    c2 = Counter(v2)
    mc1 = c1.most_common(2)
    mc2 = c2.most_common(2)
    if len(mc1) == 1:
        mc1.append((999990, 0))
    if len(mc2) == 1:
        mc2.append((999999, 0))
    cv1, cnt1 = mc1[0]
    cv2, cnt2 = mc2[0]
    if cv1 != cv2:
        print(n - cnt1 - cnt2)
    else:
        print(n - max(cnt1 + mc2[1][1], cnt2 + mc1[1][1]))


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