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()))
even=[]
odd=[]
for i in range(n):
  if i%2==0:
    even.append(v[i])
  else:
    odd.append(v[i])
from collections import Counter
even=Counter(even)
odd=Counter(odd)

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

a=even[0]
if a[1]==n//2:
  b=0
else:
  b=even[1][1]
  
c=odd[0]
if c[1]==n//2:
  d=0
else:
  d=odd[1][1]
  
if a[0]==c[0]:
  print(min(n-a[1]-d,n-b-c[1]))
else:
  print(n-a[1]-c[1])

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
v0 = v[0:n-1:2]
v1 = v[1:n:2]
from collections import Counter
V0 = Counter(v0)
V1 = Counter(v1)
if V0.most_common()[0][0] != V1.most_common()[0][0]:
    a = V0.most_common()[0][1]
    b = V1.most_common()[0][1]
else:
    if V0.most_common()[0][1] > V1.most_common()[0][1]:
        if len(V1) > 1:
            a = V0.most_common()[0][1]
            b = V1.most_common()[1][1]
        else:
            a = V0.most_common()[0][1]
            b = 0
    elif V0.most_common()[0][1] < V1.most_common()[0][1]:
        if len(V0) > 1:
            a = V1.most_common()[0][1]
            b = V0.most_common()[1][1]
        else:
            a = 0
            b = V1.most_common()[0][1]
    else:
        if len(V0) == 1 and len(V1) == 1:
            a = 0
            b = V0.most_common()[0][1]
        elif len(V0) == 1:
            a = V0.most_common()[0][1]
            b = V1.most_common()[1][1]
        elif len(V1) == 1:
            a = V0.most_common()[1][1]
            b = V1.most_common()[0][1]
        else:
            a = V0.most_common()[0][1]
            b = max(V0.most_common()[1][1],V1.most_common()[1][1])
print(n-a-b)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

odd = [v[i] for i in range(0, n, 2)]
odd_val, odd_cnt = zip(*Counter(odd).most_common())

even = [v[i] for i in range(1, n, 2)]
even_val, even_cnt = zip(*Counter(even).most_common())

if even_val[0] != odd_val[0]:
    ans = n-even_cnt[0]-odd_cnt[0]
else:
    if (even_cnt[0] + odd_cnt[1]) >= (even_cnt[1] + odd_cnt[0]):
        ans = n-even_cnt[0]-odd_cnt[1]
    else:
        ans = n-even_cnt[1]-odd_cnt[0]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

a_even = [0] * (n//2); a_odd = [0] * (n//2)

for i in range(n):
    if i % 2 == 0:
        a_even[i//2] = a[i]
    else:
        a_odd[i//2] = a[i]

dic_even = Counter(a_even); dic_odd = Counter(a_odd)

first_even_key, first_even_num = max(dic_even.items(), key=lambda x: x[1])
dic_even.pop(first_even_key)
if len(dic_even) > 0:
    second_even_key, second_even_num = max(dic_even.items(), key=lambda x: x[1])
else:
    second_even_key = -1; second_even_num = 0

first_odd_key, first_odd_num = max(dic_odd.items(), key=lambda x: x[1])
dic_odd.pop(first_odd_key)
if len(dic_odd) > 0:
    second_odd_key, second_odd_num = max(dic_odd.items(), key=lambda x: x[1])
else:
    second_odd_key = -1; second_odd_num = 0

if first_even_key == first_odd_key:
    print(n - max(first_even_num + second_odd_num, second_even_num + first_odd_num))
else:
    print(n - first_even_num - first_odd_num)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
V = list(map(int, input().split()))
x = len(set(V))
if x == 1:
    print(n // 2)
else:
    A = list()
    B = list()
    for i, v in enumerate(V):
        if i & 1:
            A.append(v)
        else:
            B.append(v)
    A = Counter(A).most_common()
    B = Counter(B).most_common()
    a = A[0]
    b = B[0]
    if a[0] == b[0]:
        if len(A) == 1:
            b = B[1]
        elif len(B) == 1:
            a = A[1]
        else:
            if A[1][1] >= B[1][1]:
                a = A[1]
            else:
                b = B[1]
    print(n - a[1] - b[1])

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    v = LI()
    acnt = collections.Counter(v[::2])
    bcnt = collections.Counter(v[1::2])
    acnt = list(acnt.items())
    bcnt = list(bcnt.items())
    acnt.sort(key=lambda x: x[1], reverse=True)
    bcnt.sort(key=lambda x: x[1], reverse=True)

    acnt.append((-1,0))
    bcnt.append((-1,0))

    ans = 0

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

    print(ans)

main()

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    v = LI()
    acnt = collections.Counter(v[::2])
    bcnt = collections.Counter(v[1::2])
    acnt = list(acnt.items())
    bcnt = list(bcnt.items())
    acnt.sort(key=lambda x: x[1], reverse=True)
    bcnt.sort(key=lambda x: x[1], reverse=True)

    ans = 0

    if len(acnt) == len(bcnt) == 1:
        if acnt[0][0] == bcnt[0][0]:
            ans = n//2
        else:
            ans = 0
    elif len(acnt)==1 or len(bcnt) == 1:
        if acnt[0][0] == bcnt[0][0]:
            if len(acnt) != 1:
                ans = n//2 - acnt[1][1]
            else:
                ans = n//2 - bcnt[1][1]
        else:
            ans = n//2 - min(acnt[0][1], bcnt[0][1])
    else:
        if acnt[0][0] == bcnt[0][0]:
            ans = n - acnt[0][1] - max(acnt[1][1], bcnt[1][1])
        else:
            ans = n - acnt[0][1]*2

    print(ans)

main()

Submission status: Accepted

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

list_odd=v[1::2]
list_even=v[0::2]

cnt_odd=Counter(list_odd)
cnt_even=Counter(list_even)

cnt_odd_sorted=sorted(cnt_odd.items(),reverse=True,key=lambda x:x[1])
cnt_even_sorted=sorted(cnt_even.items(),reverse=True,key=lambda x:x[1])

if cnt_odd_sorted[0][0]!=cnt_even_sorted[0][0]:
    ans=len(list_odd)-cnt_odd_sorted[0][1]
    ans+=len(list_even)-cnt_even_sorted[0][1]

else:
    if len(cnt_odd)==1 and len(cnt_even)==1:
        ans=len(list_even)
    elif len(cnt_odd)==1:
        ans=len(list_even)-cnt_even_sorted[1][1]
    elif len(cnt_even)==1:
        ans=len(list_odd)-cnt_odd_sorted[1][1]
    else:
        ans1=len(list_even)-cnt_even_sorted[1][1]
        ans1+=len(list_odd)-cnt_odd_sorted[0][1]

        ans2=len(list_odd)-cnt_odd_sorted[1][1]
        ans2+=len(list_even)-cnt_even_sorted[0][1]
        ans=min(ans1,ans2)

print(ans)

Submission status: Accepted

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

for i in range(0,N,2):
  D1[A[i]]+=1
for i in range(1,N,2):
  D2[A[i]]+=1
  
D1_sorted = sorted(D1.items(), key=lambda x:x[1], reverse = True) + [(-1,0)]
D2_sorted = sorted(D2.items(), key=lambda x:x[1], reverse = True) + [(-1,0)]
i, j = 0, 0
while i < len(D1_sorted) -1 and j < len(D2_sorted) -1:
  if D1_sorted[i][0] != D2_sorted[j][0]:
    break
  elif D1_sorted[i][1] > D2_sorted[j][1]:
    j+=1
  elif D1_sorted[i][1] < D2_sorted[j][1]:
    i+=1
  else:
    if D1_sorted[i+1][1] > D2_sorted[j+1][1]:
      i+=1
    else:
      j+=1

print(N-D1_sorted[i][1]-D2_sorted[j][1])

Submission status: Accepted

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

from collections import Counter
c1 = list(Counter(v1).items())
c2 = list(Counter(v2).items())

c1 = sorted(c1, key= lambda x: x[1], reverse= True)
c2 = sorted(c2, key= lambda x: x[1], reverse= True)

c1.append((0,0))
c2.append((0,0))

#print(c1[:3], c2[:3])
if c1[0][0] != c2[0][0]:
    print(n - c1[0][1] - c2[0][1])
else:
    a = c1[0][1] + c2[1][1]
    b = c1[1][1] + c2[0][1]
    if a < b:
        print(n - b)
    else:
        print(n - a)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
n = int(input())
v = [[], []]
for i, j in enumerate(input().split()):
    if i & 1:
        v[0].append(int(j))
    else:
        v[1].append(int(j))

v1, v2 = Counter(v[0]), Counter(v[1])
mx1 , mx2 = v1.most_common()[0], v2.most_common()[0]
if mx1[0] == mx2[0]:
    if mx1[1] + mx2[1] == n:
        print(n//2)
        sys.exit() 
    nx1, nx2 = v1.most_common()[1][1], v2.most_common()[1][1]# 次に最も多い数
    if nx1 > nx2:
        print(n - nx1 - mx2[1])
    else:
        print(n - mx1[1] - nx2)
else:
    print(n - mx1[1] - mx2[1])

Submission status: Accepted

Solution code:
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n = i_input()
    v = i_list()

    fd = {}
    sd = {}
    for i,k in enumerate(v):
        if i%2==0:
            fd[k] = fd.get(k,0) + 1
        else:
            sd[k] = sd.get(k,0) + 1

    a = max(fd, key=fd.get)
    b = max(sd, key=sd.get)
    c = max(fd.values())
    d = max(sd.values())

    if a != b:
        print(n - c - d)
    else:
        fd[a] = 0
        sd[b] = 0
        e = max(fd.values())
        f = max(sd.values())
        z = max(c+f , d+e)
        print(n-z)


if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]

N,V = ilint()
odd = counter()
even = counter()
for i in range(N):
    if i%2==0:
        even.add(V[i])
    else:
        odd.add(V[i])
E = [(k,even[k]) for k in even]+[(0,0)]
O = [(k,odd[k]) for k in odd]+[(0,0)]
E.sort(reverse=True, key=lambda x:x[1])
O.sort(reverse=True, key=lambda x:x[1])
if E[0][0]!=O[0][0]:
    print(N-E[0][1]-O[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())
V = list(map(int, input().split()))
odd_dict = {}
even_dict = {}
for i in range(N):
    if i % 2 == 0:
        if odd_dict.get(V[i]) is None:
            odd_dict[V[i]] = 1
        else:
            odd_dict[V[i]] += 1
    else:
        if even_dict.get(V[i]) is None:
            even_dict[V[i]] = 1
        else:
            even_dict[V[i]] += 1

odd_dict_sorted = sorted(odd_dict.items(), key=lambda a: a[1])
even_dict_sorted = sorted(even_dict.items(), key=lambda a: a[1])

odd_dict_max = odd_dict_sorted.pop()
even_dict_max = even_dict_sorted.pop()
if odd_dict_max[0] == even_dict_max[0] and N // 2 == odd_dict_max[1]:
    ans = N // 2
elif odd_dict_max[0] == even_dict_max[0]:
    odd_dict_max_t = odd_dict_sorted.pop()
    even_dict_max_t = even_dict_sorted.pop()
    ans = N - odd_dict_max[1] - max(even_dict_max_t[1],odd_dict_max_t[1])
else:
    ans = N - odd_dict_max[1] - even_dict_max[1]
print(ans)

Submission status: Accepted

Solution code:
import collections

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

vs_even = vs[0::2]
vs_odd = vs[1::2]

count_e = collections.Counter(vs_even).most_common()
count_o = collections.Counter(vs_odd).most_common()

#ダミー
count_e.append((0,0))
count_o.append((0,0))


e_max = len(vs_even) - count_e[0][1]
e_second = len(vs_even) - count_e[1][1]
o_max = len(vs_odd) - count_o[0][1]
o_second = len(vs_odd) - count_o[1][1]

if(count_e[0][0] != count_o[0][0]):
    print(e_max + o_max)

else:
    print(min(e_second+o_max, e_max+o_second))

Submission status: Accepted

Solution code:
import collections
N = int(input())
N_List = list(map(int,input().split()))
hanN = N//2
ki = N_List[1::2]
gu = N_List[0::2]
kicnt = collections.Counter(ki)
gucnt = collections.Counter(gu)

kidict = kicnt.most_common()
gudict = gucnt.most_common()

kilen = len(kidict)
gulen = len(gudict)

if kilen == 1 & gulen == 1:
    if kidict == gudict:
        ans = hanN
    else:
        ans = 0
elif kidict[0][0] == gudict[0][0]:
    if kidict[0][1] > gudict[0][1]:
        ans = sum(map(lambda x:x[1],kidict[1:])) + sum(map(lambda x:x[1],gudict[2:])) + gudict[0][1]
    elif kidict[0][1] < gudict[0][1]:
        ans = sum(map(lambda x:x[1],gudict[1:])) + sum(map(lambda x:x[1],kidict[2:])) + kidict[0][1]
    else:
        if gudict[1][1] >= kidict[1][1]:
            ans = sum(map(lambda x:x[1],kidict[1:])) + sum(map(lambda x:x[1],gudict[2:])) + gudict[0][1]
        else:
            ans = sum(map(lambda x:x[1],gudict[1:])) + sum(map(lambda x:x[1],kidict[2:])) + kidict[0][1]
else:
    ans = sum(map(lambda x:x[1],gudict[1:])) + sum(map(lambda x:x[1],kidict[1:]))

print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
lsp = list(map(int,input().split()))
lse = []
lso = []
for i in range(n):
    if i%2 == 0:
        lse.append(lsp[i])
    else:
        lso.append(lsp[i])

countere = collections.Counter(lse)
countero = collections.Counter(lso)

valuee = sorted(list(countere.values()),reverse=True)+[0]
valueo = sorted(list(countero.values()),reverse=True)+[0]

keyse = []
for i in countere.keys():
    if countere[i] == valuee[0]:
        keyse.append(i)
keyso = []
for i in countero.keys():
    if countero[i] == valueo[0]:
        keyso.append(i)
factor = 0
if len(keyse) == 1 and len(keyso) == 1 and keyso == keyse:
    factor = min(valuee[0]-valuee[1],valueo[0]-valueo[1])

print(n-valuee[0]-valueo[0]+factor)

Submission status: Accepted

Solution code:
import collections
import math

n=int(input())
a=list(map(int,input().split()))
ev=[a[2*i] for i in range(n//2)]
od=[a[2*i+1] for i in range(n//2)]

ccev=collections.Counter(ev)
ccod=collections.Counter(od)

mcev=ccev.most_common()
mcod=ccod.most_common()

if mcev[0][0]!=mcod[0][0]:
    print(n-mcev[0][1]-mcod[0][1])
    exit()
elif len(mcev)==1 and len(mcod)==1:
    print(n-mcev[0][1])
    exit()
elif len(mcev)==1:
    print(n-mcod[1][1])
    exit()
elif len(mcod)==1:
    print(n-mcev[1][1])
    exit()
else:
    tt=max(mcev[1][1],mcod[1][1])
    print(n-mcev[0][1]-tt)
    exit()

Submission status: Accepted

Solution code:
n, *v = map(int, open(0).read().split())
from collections import*
d, e = (Counter(i).most_common()+[(0,0)] for i in (v[0::2], v[1::2]))
print(n-d[0][1]-e[0][1] if d[0] != e[0] else n-max(d[0][1]+e[1][1], d[1][1]+e[0][1]))

Submission status: Accepted

Solution code:
n = int(input())
v  = list(map(int, input().split()))
a = [v[i] for i in range(0, n, 2)]
b = [v[i] for i in range(1, n, 2)]
d = {}
for i in a:
    if i not in d:
        d[i] = 1
    else:
        d[i] +=1
e = {}
for i in b:
    if i not in e:
        e[i] = 1
    else:
        e[i] +=1
d = sorted(d.items(), key=lambda x: -x[1])
e = sorted(e.items(), key=lambda x: -x[1])
if d[0] != e[0]:
    print(n-d[0][1]-e[0][1])
else:
    if len(d) == len(e) == 1:
        print(n//2)
    elif len(d) == 1 and len(e) > 1:
        print(n//2-e[1][1])
    elif len(d) > 1 and len(e) == 1:
        print(n//2-d[1][1])
    else:
        print(n-max(d[0][1]+e[1][1], d[1][1]+e[0][1]))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc111/tasks/arc103_a

import sys
# sys.setrecursionlimit(100000)
from collections import Counter


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    V = input_int_list()
    cnt = n // 2
    even = Counter(V[0::2]).most_common()
    odd = Counter(V[1::2]).most_common()
    if even[0][0] != odd[0][0]:
        ans = (cnt) - even[0][1] + (cnt) - odd[0][1]
    else:
        if len(even) > 1 and len(odd) > 1:
            ans = min(cnt - even[0][1] + cnt - odd[1][1], cnt - even[1][1] + cnt - odd[0][1])
        elif len(even) == 1 and len(odd) == 1:
            ans = cnt
        elif len(even) == 1 and len(odd) > 1:
            ans = min(cnt - odd[1][1])
        elif len(odd) == 1 and len(even) > 1:
            ans = min(cnt - even[1][1])
    print(ans)

    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter


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

ans = 0
l = n//2
if a[0][0] != b[0][0]:
    ans = l - a[0][1] + l - b[0][1]
else:
    if len(b) > 1:
        x = l - a[0][1] + l - b[1][1]
        y = l - a[1][1] + l - b[0][1]

    else:
        x = l - a[0][1] + l
        y = l - b[0][1] + l

    ans = min(x, y)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


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

ans = 0

if a[0][1] > b[0][1]:
    ans += (n//2) - a[0][1]
    if a[0][0] == b[0][0]:
        if len(b) > 1:
            ans += (n//2) - b[1][1]
        else:
            ans += (n//2)
    else:
        ans += (n // 2) - b[0][1]
elif a[0][1] < b[0][1]:
    ans += (n // 2) - b[0][1]
    if a[0][0] == b[0][0]:
        if len(a) > 1:
            ans += (n // 2) - a[1][1]
        else:
            ans += (n // 2)
    else:
        ans += (n // 2) - a[0][1]
else:
    ans += (n // 2) - b[0][1]
    if a[0][0] == b[0][0]:
        if len(a) > 1:
            ans += (n // 2) - max(a[1][1], b[1][1])
        else:
            ans += (n // 2)
    else:
        ans += (n // 2) - a[0][1]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
v=list(map(int,input().split()))
even=[]
odd=[]
for n in range(N):
    if n%2==0:
        even.append(v[n])
    else:
        odd.append(v[n])
evens=Counter(even)
odds=Counter(odd)
even_value_max=max(evens.values())
odds_value_max=max(odds.values())
even_numbers=[]
odds_numbers=[]
for key,value in evens.items():
    if value==even_value_max:
        even_numbers.append(key)
for key,value in odds.items():
    if value==odds_value_max:
        odds_numbers.append(key)
if len(evens)==1 and len(odds)==1 and v[0]==v[1]:
    answer=N//2
elif len(even_numbers)>=2 or len(odds_numbers)>=2:
    answer=N-even_value_max-odds_value_max
else:
    even=even_numbers[0]
    odd=odds_numbers[0]
    if even!=odd:
        answer=N-even_value_max-odds_value_max
    else:
        evens[even]=0
        odds[odd]=0
        if max(odds.values())>=max(evens.values()):
            answer=N-max(odds.values())-even_value_max
        else:
            answer=N-max(evens.values())-odds_value_max
print(answer)

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int, input().split()))
even = [v[2*i] for i in range(n//2)]
odd = [v[2*i+1] for i in range(n//2)]
dEven = collections.Counter(even).most_common()
dOdd = collections.Counter(odd).most_common()
if dEven[0][0] != dOdd[0][0]:
    ans = n - dEven[0][1]  - dOdd[0][1]
else:
    if len(dEven) >=2 and len(dOdd) >= 2:
        ans = n - max(dEven[0][1]+dOdd[1][1], dEven[1][1]+dOdd[0][1])
    elif len(dEven) < len(dOdd):
        ans = n - (dEven[0][1]+dOdd[1][1])
    elif len(dEven) > len(dOdd):
        ans = n - (dEven[1][1]+dOdd[0][1])
    else:
        ans = n//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
ev = Counter([v[i] for i in range(0, n, 2)])
od = Counter([v[i] for i in range(1, n, 2)])
evk = list(ev.keys())
odk = list(od.keys())
ev_key = evk[0]
for k in evk:
    if ev[k] > ev[ev_key]:
        ev_key = k
od_key = odk[0]
for k in odk:
    if od[k] > od[od_key]:
        od_key = k
evv = list(ev.values()) + [0]
evv.sort(reverse = True)
odv = list(od.values()) + [0]
odv.sort(reverse = True)

if ev_key != od_key:
    print(n - evv[0] - odv[0])
else:
    print(min(n - evv[0] - odv[1], n - evv[1] - odv[0]))

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]

one=[]
two=[]

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

one_count=[0]*(10**5+1)
two_count=[0]*(10**5+1)

for i in one:
    one_count[i]+=1
for i in two:
    two_count[i]+=1

one_first_index=0
one_max_count=0
for i,value in enumerate(one_count):
    if one_max_count<value:
        one_max_count=value
        one_first_index=i
one_second_index=0
one_second_max_count=0
for i,value in enumerate(one_count):
    if one_second_max_count<value and i!=one_first_index:
        one_second_max_count=value
        one_second_index=i

two_first_index=0
two_max_count=0
for i,value in enumerate(two_count):
    if two_max_count<value:
        two_max_count=value
        two_first_index=i
two_second_index=0
two_second_max_count=0
for i,value in enumerate(two_count):
    if two_second_max_count<value and i!=two_first_index:
        two_second_max_count=value
        two_second_index=i



if one_first_index!=two_first_index:
    print(n-one_max_count-two_max_count)
elif one_max_count-one_second_max_count<=two_max_count-two_second_max_count:
    print(n - one_second_max_count - two_max_count)
else:
    print(n - two_second_max_count - one_max_count)

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
import collections as col
o=col.Counter(v[0:n:2])
e=col.Counter(v[1:n:2])
ook=o.most_common()
eok=e.most_common()
ok=ook[0][1]+eok[0][1]
if ook[0][0]==eok[0][0]:
    if len(ook)<2 and len(eok)<2:
        print(n//2)
        exit()
    elif len(ook)<2:
        ok=ook[0][1]+eok[1][1]
    elif len(eok)<2:
        ok=ook[1][1]+eok[0][1]
    else:
        ok=max(ook[0][1]+eok[1][1],ook[1][1]+eok[0][1])
print(n-ok)

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = list(map(int,input().split()))
a1 = collections.Counter(a[0::2]).most_common()
a2 = collections.Counter(a[1::2]).most_common()
a1.append((0,0))
a2.append((0,0))
#print(a1)
#print(a2)

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

Submission status: Accepted

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

c1=Counter(V[0::2]).most_common()+[(0,0)]
c2=Counter(V[1::2]).most_common()+[(0,0)]
if c1[0][0]!=c2[0][0]:
    print(N-c1[0][1]-c2[0][1])
else:
    print(min(N-c1[0][1]-c2[1][1],N-c1[1][1]-c2[0][1]))

Submission status: Accepted

Solution code:
N=int(input())
*V,=map(int,input().split())
import collections

c1=collections.Counter(V[0::2]).most_common()
c2=collections.Counter(V[1::2]).most_common()
c1.append([0,0])
c2.append([0,0])
if c1[0][0]!=c2[0][0]:
    print(N-c1[0][1]-c2[0][1])
else:
    print(min(N-c1[0][1]-c2[1][1],N-c1[1][1]-c2[0][1]))

Submission status: Accepted

Solution code:
N=int(input())
*V,=map(int,input().split())
import collections

import collections

c1=collections.Counter(V[0::2])
c1=sorted(c1.items(), key=lambda x: x[1], reverse=True)
e=c1[0][0]

c2=collections.Counter(V[1::2])
c2=sorted(c2.items(), key=lambda x: x[1], reverse=True)
o=c2[0][0]

cnt=0
if e!=o:
    cnt=sum(e!=i for i in V[0::2])
    cnt+=sum(o!=i for i in V[1::2])
else:
    if len(c1)==1 and len(c2)==1:
        cnt=N//2
    elif len(c1)==1:
        cnt1=sum(e!=i for i in V[0::2])
        cnt1+=sum(c2[1][0]!=i for i in V[1::2])
        
        cnt2=sum(e!=i for i in V[0::2])
        cnt2+=sum(c2[1][1]!=i for i in V[1::2])
        cnt=min(cnt1,cnt2)
    elif len(c2)==1:
        cnt1=sum(c1[1][0]!=i for i in V[0::2])
        cnt1+=sum(o!=i for i in V[1::2])

        cnt2=sum(c1[1][1]!=i for i in V[0::2])
        cnt2+=sum(o!=i for i in V[1::2])
        cnt=min(cnt1,cnt2)
    else:    
        cnt1=sum(e!=i for i in V[0::2])
        cnt1+=sum(c2[1][0]!=i for i in V[1::2])
        
        cnt2=sum(c1[1][0]!=i for i in V[0::2])
        cnt2+=sum(o!=i for i in V[1::2])
        cnt=min(cnt1,cnt2)
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
kisu = []
gusu = []
for i in range(n):
    if i % 2 == 0:
        kisu.append(v[i])
    else:
        gusu.append(v[i])
k = sorted(Counter(kisu).items(), key=lambda x: x[1], reverse=True)
g = sorted(Counter(gusu).items(), key=lambda x: x[1], reverse=True)
ans1 = 0
ans2 = 0
#print(k, g)
if k[0][0] == g[0][0]:
    if k[0][1] > g[0][1]:
        ans1 = (n//2) - k[0][1]
        if len(g) < 2:
            ans2 = n // 2
        else:
            ans2 = (n//2) - g[1][1]
    elif k[0][1] < g[0][1]:
        ans1 = (n//2) - g[0][1]
        if len(k) < 2:
            ans2 = n // 2
        else:
            ans2 = (n//2) - k[1][1]
    else:
        if len(k) < 2:
            ans2 = n // 2
        else:
            if k[1][1] == g[1][1]:
                ans1 = (n // 2) - k[0][1]
                ans2 = (n // 2) - g[1][1]
            elif k[1][1] > g[1][1]:
                ans1 = (n // 2) - k[1][1]
                ans2 = (n // 2) - g[0][1]
            else:
                ans1 = (n // 2) - k[0][1]
                ans2 = (n // 2) - g[1][1]
else:
    ans1 = (n // 2) - k[0][1]
    ans2 = (n // 2) - g[0][1]
print(ans1 + ans2)

Submission status: Accepted

Solution code:

from collections import Counter

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

evens = Counter(X[::2]).most_common()
odds = Counter(X[1::2]).most_common()

evens.append((0, 0))
odds.append((0, 0))

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

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    N = int(input())
    V = [int(a) for a in input().split()]
    
    even = list()
    odd = list()

    for i in range(N):
        if (i + 1) % 2 == 0:
            odd.append(V[i])
        else:
            even.append(V[i])

    even_counter = Counter(even).most_common()
    odd_counter = Counter(odd).most_common()

    ans = 0
    if even_counter[0][0] != odd_counter[0][0]:
        if len(even_counter) >= 2:
            ans += len(even) - even_counter[0][1]

        if len(odd_counter) >= 2:
            ans += len(odd) - odd_counter[0][1]

    else:
        if len(even_counter) == 1 and len(odd_counter) == 1:
            ans += even_counter[0][1]

        elif len(even_counter) == 1 and len(odd_counter) >= 2:
            ans += len(odd) - odd_counter[1][1]

        elif len(even_counter) >= 2  and len(odd_counter) == 1:
            ans += len(even) - even_counter[1][1]

        elif len(even_counter) >= 2 and len(odd_counter) >= 2:
            cost_even = len(even) - even_counter[0][1] + len(odd)  - odd_counter[1][1]
            cost_odd  = len(odd)  - odd_counter[0][1]  + len(even) - even_counter[1][1]
            ans += min(cost_even, cost_odd)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    N = int(input())
    V = [int(a) for a in input().split()]
    
    even = list()
    odd = list()

    for i in range(N):
        if (i + 1) % 2 == 0:
            odd.append(V[i])
        else:
            even.append(V[i])

    even_counter = Counter(even).most_common()
    odd_counter = Counter(odd).most_common()

    ans = 0
    if even_counter[0][0] != odd_counter[0][0]:
        if len(even_counter) >= 2:
            ans += len(even) - even_counter[0][1]

        if len(odd_counter) >= 2:
            ans += len(odd) - odd_counter[0][1]

    else:
        if len(even_counter) == 1 and len(odd_counter) == 1:
            ans += even_counter[0][1]

        elif len(even_counter) == 1 and len(odd_counter) >= 2:
            ans += len(odd) - odd_counter[1][1]

        elif len(even_counter) >= 2  and len(odd_counter) == 1:
            ans += len(even) - even_counter[1][1]

        elif len(even_counter) >= 2 and len(odd_counter) >= 2:
            cost_even = len(even) - even_counter[0][1] + len(odd)  - odd_counter[1][1]
            cost_odd  = len(odd)  - odd_counter[0][1]  + len(even) - even_counter[1][1]

            ans += min(cost_even, cost_odd)

    print(ans)

if __name__ == "__main__":
    main()