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())
#a,b=map(int,input().split())
vl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

dic1={};dic2={}

for i in range(n):
    v=vl[i]
    if i%2==0:
        dic1[v]=dic1.get(v,0)+1
    else:
        dic2[v]=dic2.get(v,0)+1

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

#dic1の最頻値
mx1_val,mx1_count=dic1[0]
if len(dic1)==1:
    mx1_count2=0
else:
    mx1_count2=dic1[1][1]

mx2_val,mx2_count=dic2[0]
if len(dic2)==1:
    mx2_count2=0
else:
    mx2_count2=dic2[1][1]

ans=0
ansl=[]
total=n//2
if n==2:
    if vl[0]==vl[1]:
        ans=1
    else:
        ans=0
    print(ans)
else:
    if mx1_val != mx2_val:
        ans=n-(mx1_count+mx2_count)
    else:
        ans=min(n-mx1_count-mx2_count2,
        n-mx1_count2-mx2_count)
    print(ans)

Submission status: Accepted

Solution code:
import math
import sys

n = int(sys.stdin.readline().rstrip())
v = list(map(int, sys.stdin.readline().rstrip().split()))
v1 = v[::2]
v2 = v[1::2]
v1d = {}
v2d = {}
for vi in v1:
    if vi in v1d:
        v1d[vi] += 1
    else:
        v1d[vi] = 1
for vi in v2:
    if vi in v2d:
        v2d[vi] += 1
    else:
        v2d[vi] = 1

if max(v1d, key=v1d.get) != max(v2d, key=v2d.get):
    print(len(v1) - max(v1d.values()) + len(v2) - max(v2d.values()))
else:
    ans1 = 0
    if len(v1d.values()) == 1:
        tmp = 0
    else:
        tmp = sorted(v1d.values())[-2]
    ans1 = len(v1) - tmp + len(v2) - max(v2d.values())
    ans2 = 0
    if len(v2d.values()) == 1:
        tmp = 0
    else:
        tmp = sorted(v2d.values())[-2]
    ans2 = len(v1) - max(v1d.values()) + len(v2) - tmp
    print(min(ans1, ans2))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
if len(set(v)) == 1:
    print(n // 2)
    exit()
kisu = v[::2]
gusu = v[1::2]
kisu_color = Counter(kisu).most_common()[:2]
gusu_color = Counter(gusu).most_common()[:2]
if kisu_color[0][0] != gusu_color[0][0]:
    ans = n - kisu_color[0][1] - gusu_color[0][1]
else:
    ans = min(n - kisu_color[0][1] - gusu_color[1][1], n - kisu_color[1][1] - gusu_color[0][1])
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

N = ii()
A = li()

d_o = defaultdict(int)
d_e = defaultdict(int)

for i in range(N):
    if i % 2:
        d_o[A[i]] += 1
    else:
        d_e[A[i]] += 1

l_d = sorted(list(d_o.items()), key=lambda x:x[1], reverse=True)
l_e = sorted(list(d_e.items()), key=lambda x:x[1], reverse=True)

if l_d[0][0] != l_e[0][0]:
    print(N - l_d[0][1] - l_e[0][1])
elif len(l_d) == len(l_e) == 1:
        print(N - max(l_d[0][1], l_e[0][1]))
elif len(l_d) == 1:
        #ans = min(N - l_d[0][1] - l_e[1][1], N - l_e[0][1])
        #print(ans)
        print(N - l_d[0][1] - l_e[1][1])
elif len(l_e) == 1:
        #ans = min(N - l_d[1][1] - l_e[0][1], N - l_d[0][1])
        #print(ans)
        print(N - l_d[1][1] - l_e[0][1])
else:
    ans = min(N-l_d[1][1]-l_e[0][1], N-l_d[0][1]-l_e[1][1])
    print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

N = ii()
A = li()

d_o = defaultdict(int)
d_e = defaultdict(int)

for i in range(N):
    if i % 2:
        d_o[A[i]] += 1
    else:
        d_e[A[i]] += 1

l_d = sorted(list(d_o.items()), key=lambda x:x[1], reverse=True)
l_e = sorted(list(d_e.items()), key=lambda x:x[1], reverse=True)

if l_d[0][0] != l_e[0][0]:
    print(N - l_d[0][1] - l_e[0][1])
elif len(l_d) == len(l_e) == 1:
        print(N - max(l_d[0][1], l_e[0][1]))
elif len(l_d) == 1:
        ans = min(N - l_d[0][1] - l_e[1][1], N - l_e[0][1])
        print(ans)
elif len(l_e) == 1:
        ans = min(N - l_d[1][1] - l_e[0][1], N - l_d[0][1])
        print(ans)
else:
    ans = min(N-l_d[1][1]-l_e[0][1], N-l_d[0][1]-l_e[1][1])
    print(ans)

Submission status: Accepted

Solution code:
import collections

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

if len(set(v)) == 1:
  print(n//2)
else:
  v1 = v[::2]
  v2 = v[1::2]
  
  cmc1 = collections.Counter(v1).most_common()
  cmc2 = collections.Counter(v2).most_common()
  
  if cmc1[0][0] == cmc2[0][0]:    
    ans1 = (len(v1) - cmc1[0][1]) + (len(v2) - cmc2[1][1])
    ans2 = (len(v1) - cmc1[1][1]) + (len(v2) - cmc2[0][1])
    print(min(ans1, ans2))
  else:
    ans = (len(v1) - cmc1[0][1]) + (len(v2) - cmc2[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[::2]).most_common()
b = Counter(v[1::2]).most_common()
a.append([0,0])
b.append([0,0])
if a[0][0] != b[0][0]:
    print(n-(a[0][1] + b[0][1]))
else:
    print(min(n-(a[1][1] + b[0][1]), n-(a[0][1]+b[1][1])))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections
N = int(input())
V = list(map(int, input().split()))

V1 = V[::2]
V2 = V[1::2]


C1 = collections.Counter(V1)
C2 = collections.Counter(V2)

C1_s = C1.most_common()
C2_s = C2.most_common()

max1 = C1_s[0][1]
index1 = C1_s[0][0]

max2 = C2_s[0][1]
index2 = C2_s[0][0]

max1_2 = 0
max2_2 = 0
if len(C1_s) > 1:
    max1_2 = C1_s[1][1]

if len(C2_s) > 1:
    max2_2 = C2_s[1][1]


nokori1 = len(V1) - max1
nokori2 = len(V2) - max2

nokori1_2 = len(V1) - max1_2
nokori2_2 = len(V2) - max2_2

ans = 0
if index1 == index2:
    ans = min(nokori1_2 + nokori2, nokori1 + nokori2_2)
else:
    ans = nokori1 + nokori2
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections
N = int(input())
V = list(map(int, input().split()))

V1 = V[::2]
V2 = V[1::2]


C1 = collections.Counter(V1)
C2 = collections.Counter(V2)

C1_s = sorted(C1.items(), key=lambda x:x[1])[::-1]
C2_s = sorted(C2.items(), key=lambda x:x[1])[::-1]

max1 = C1_s[0][1]
index1 = C1_s[0][0]

max2 = C2_s[0][1]
index2 = C2_s[0][0]

max1_2 = 0
max2_2 = 0
if len(C1_s) > 1:
    max1_2 = C1_s[1][1]

if len(C2_s) > 1:
    max2_2 = C2_s[1][1]


nokori1 = len(V1) - max1
nokori2 = len(V2) - max2

nokori1_2 = len(V1) - max1_2
nokori2_2 = len(V2) - max2_2

ans = 0
if index1 == index2:
    ans = min(nokori1_2 + nokori2, nokori1 + nokori2_2)
else:
    ans = nokori1 + nokori2
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections
N = int(input())
V = list(map(int, input().split()))

V1 = V[::2]
V2 = V[1::2]

if len(set(V1)) == 1 and len(set(V2)) == 1 and V1[0] == V2[0]:
    print(len(V1))
    exit()


C1 = collections.Counter(V1)
C2 = collections.Counter(V2)

C1_s = sorted(C1.items(), key=lambda x:x[1])[::-1]
C2_s = sorted(C2.items(), key=lambda x:x[1])[::-1]

max1 = C1_s[0][1]
index1 = C1_s[0][0]

max2 = C2_s[0][1]
index2 = C2_s[0][0]

max1_2 = 0
max2_2 = 0
if len(C1_s) > 1:
    max1_2 = C1_s[1][1]

if len(C2_s) > 1:
    max2_2 = C2_s[1][1]


nokori1 = len(V1) - max1
nokori2 = len(V2) - max2

nokori1_2 = len(V1) - max1_2
nokori2_2 = len(V2) - max2_2

ans = 0
if index1 == index2:
    if len(set(V1)) == 1:
        ans = nokori2_2
    elif len(set(V2)) == 1:
        ans = nokori1_2
    else:
        ans = min(nokori1_2 + nokori2, nokori1 + nokori2_2)
else:
    ans = nokori1 + nokori2
print(ans)

Submission status: Accepted

Solution code:

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

evn=dict()
odd=dict()

maxevn1=0
maxevnv1=0
maxodd1=0
maxoddv1=0

maxevn2=0
maxevnv2=0
maxodd2=0
maxoddv2=0

for i in range(n):
    if i%2==0:
        if v[i] in evn:
            evn[v[i]]+=1
        else:
            evn[v[i]]=1
        if evn[v[i]]>maxevn1  and v[i]==maxevnv1:
            maxevn1=evn[v[i]]
        elif evn[v[i]]>maxevn1  and v[i]!=maxevnv1:
            maxevn2=maxevn1
            maxevnv2=maxevnv1
            maxevn1=evn[v[i]]
            maxevnv1=v[i]           
        elif evn[v[i]]>maxevn2:
            maxevn2=evn[v[i]]
            maxevnv2=v[i]           
    else:
        if v[i] in odd:
            odd[v[i]]+=1
        else:
            odd[v[i]]=1
        if odd[v[i]]>maxodd1 and v[i]==maxoddv1:
            maxodd1=odd[v[i]]
        elif odd[v[i]]>maxodd1 and v[i]!=maxoddv1:
            maxodd2=maxodd1
            maxoddv2=maxoddv1
            maxodd1=odd[v[i]]
            maxoddv1=v[i]           
        elif odd[v[i]]>maxodd2:
            maxodd2=odd[v[i]]
            maxoddv2=v[i]           

if maxevnv1!=maxoddv1:
    print(n-maxevn1-maxodd1)
else:
    kk=min(n-maxevn1-maxodd2,n-maxevn2-maxodd1)
    print(kk)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
d = {}
for num,i in enumerate(a):
  if num%2==0:
    if i in d:
      d[i][0] += 1
    else:
      d[i] = [1,0]
  else:
    if i in d:
      d[i][1] += 1
    else:
      d[i] = [0,1]
b = [i for i in d.values()]
c = [i for i in d.values()]
b.append([0,0])
c.append([0,0])
b.sort(key=lambda x:x[0],reverse=True)
c.sort(key=lambda x:x[1],reverse=True)
if b[0][1] == c[0][0] and b[0][1] != 0:
  print(n-max(b[0][0]+c[1][1],b[1][0]+c[0][1]))
else:
  print(n-b[0][0]-c[0][1])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

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

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

from collections import Counter

def solve():
    n = II()
    V = LI()
    
    even = V[::2]
    odd = V[1::2]
    harf = n // 2
    
    ec = Counter(even).most_common()
    oc = Counter(odd).most_common()
    #print(n)
    #print(ec[:3])
    #print(oc[:3])
    e1 = ec[0][1]
    o1 = oc[0][1]
    
    if ec[0][0] == oc[0][0]:
      # 小さい方を変える
      e2 = ec[1][1] if len(ec) > 1 else 0
      o2 = oc[1][1] if len(oc) > 1 else 0
      
      ans = min(n - e1 - o2, n - o1 - e2)
    else:
      ans = n - e1 - o1
    
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

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

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

from collections import Counter

def solve():
    n = II()
    V = LI()
    
    even = V[::2]
    odd = V[1::2]
    harf = n // 2
    
    ec = Counter(even).most_common()
    oc = Counter(odd).most_common()
    #print(n)
    #print(ec[:3])
    #print(oc[:3])

    
    if ec[0][0] == oc[0][0]:
      # 小さい方を変える
      if ec[0][1] < oc[0][1]:
        ans = n - oc[0][1] - (ec[1][1] if len(ec) > 1 else 0)
      elif ec[0][1] == oc[0][1]:
        ans = n - ec[0][1] - max((ec[1][1] if len(ec) > 1 else 0), (oc[1][1] if len(oc) > 1 else 0))
      else:
        ans = n - ec[0][1] - (oc[1][1] if len(oc) > 1 else 0)
    else:
      ans = n - ec[0][1] - oc[0][1]
    
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
numbers = [int(x) for x in input().split()]

odds = numbers[::2]
evens = numbers[1::2]

c1 = Counter(odds).most_common(2)
c2 = Counter(evens).most_common(2)

if c1[0][0] == c2[0][0]:
  print(min(
    N - c1[0][1] - (c2[1][1] if len(c2) > 1 else 0),
    N - c2[0][1] - (c1[1][1] if len(c1) > 1 else 0)
  ))    
else:  
  print(N - c1[0][1] - c2[0][1])

Submission status: Accepted

Solution code:
N = int(input())
K = list(map(int,input().rstrip().split(" ")))
Ka = [0] * (10 ** 5 + 1)
Ke = [0] * (10 ** 5 + 1)
Ka2 = []
Ke2 = []

for i in range(N):
    if i % 2 == 0: Ka[K[i]] += 1
    else: Ke[K[i]] += 1
Ka2 = sorted(Ka,reverse = True)
Ke2 = sorted(Ke,reverse = True)

if Ka.index(Ka2[0]) == Ke.index(Ke2[0]):
    if Ka2[0] - Ka2[1] < Ke2[0] - Ke2[1]:
        print(N - Ka2[1] - Ke2[0])
    else:
        print(N - Ka2[0] - Ke2[1])
else:
    print(N -Ka2[0] - Ke2[0])

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 奇数番目の最頻値と偶数番目の最頻値が同じ時に注意
N = ir()
V = lr()
odd = V[::2]
even = V[1::2]
cnt_odd = Counter(odd)
cnt_even = Counter(even)
cnt_odd_most = cnt_odd.most_common()
cnt_even_most = cnt_even.most_common()
if cnt_odd_most[0][0] == cnt_even_most[0][0]:
    answer = N
    if len(cnt_odd_most) > 1:
        temp = N - cnt_odd_most[1][1] - cnt_even_most[0][1]
        answer = min(answer, temp)
    if len(cnt_even_most) > 1:
        temp = N - cnt_odd_most[0][1] - cnt_even_most[1][1]
        answer = min(answer, temp)
    if answer == N:
        answer = N // 2
else:
    answer = N - cnt_odd_most[0][1] - cnt_even_most[0][1]

print(answer)

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
d_e={}
d_o={}
for i in range(n):
  if i%2==0:
    if v[i] in d_e:
      d_e[v[i]]+=1
    else:
      d_e[v[i]]=1
  else:
    if v[i] in d_o:
      d_o[v[i]]+=1
    else:
      d_o[v[i]]=1
d_o=sorted(d_o.items(),key=lambda x:x[1])
d_e=sorted(d_e.items(),key=lambda x:x[1])
if d_o[-1][0] != d_e[-1][0]:
    print(n-(d_o[-1][1]+d_e[-1][1]))
else:
  if (len(d_o)>=2) and(len(d_e)>=2):
    print(min(n-(d_o[-2][1]+d_e[-1][1]),n-(d_o[-1][1]+d_e[-2][1])))
  else:
    print(min(d_o[-1][1],d_e[-1][1]))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
from operator import itemgetter
def main():
    n=int(input())
    V=[int(_) for _ in input().split()]
    even,odd=V[0::2],V[1::2]
    even_c,odd_c=Counter(even),Counter(odd)
    even_ci,odd_ci=list(even_c.items()),list(odd_c.items())
    even_ci.sort(key=itemgetter(1),reverse=True)
    odd_ci.sort(key=itemgetter(1),reverse=True)
    if even_ci[0][0]==odd_ci[0][0]:
        if len(even_ci)>1 and len(odd_ci)>1:
            print(min(sum(even_c.values())-even_ci[1][1]+sum(odd_c.values())-odd_ci[0][1],
                        sum(even_c.values())-even_ci[0][1]+sum(odd_c.values())-odd_ci[1][1]))
        elif len(even_ci)>1 and len(odd_ci)==1:
            print(sum(even_c.values())-even_ci[1][1])        
        elif len(even_ci)==1 and len(odd_ci)>1:
            print(sum(odd_c.values())-odd_ci[0][1])        
        else:
            print(n//2)   
    else:
        print(sum(even_c.values())-even_ci[0][1]+sum(odd_c.values())-odd_ci[0][1])

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
from operator import itemgetter
def main():
    n=int(input())
    V=[int(_) for _ in input().split()]
    even=V[0::2]
    odd=V[1::2]
    even_c=Counter(even)
    odd_c=Counter(odd)
    even_ci,odd_ci=list(even_c.items()),list(odd_c.items())
    even_ci.sort(key=itemgetter(1),reverse=True)
    odd_ci.sort(key=itemgetter(1),reverse=True)
    if even_ci[0][0]==odd_ci[0][0]:
        if len(even_ci)>1 and len(odd_ci)>1:
            print(min(sum(even_c.values())-even_ci[1][1]+sum(odd_c.values())-odd_ci[0][1],
                        sum(even_c.values())-even_ci[0][1]+sum(odd_c.values())-odd_ci[1][1]))
        elif len(even_ci)>1 and len(odd_ci)==1:
            print(sum(even_c.values())-even_ci[1][1])        
        elif len(even_ci)==1 and len(odd_ci)>1:
            print(sum(odd_c.values())-odd_ci[0][1])        
        else:
            print(n//2)   
    else:
        print(sum(even_c.values())-even_ci[0][1]+sum(odd_c.values())-odd_ci[0][1])

if __name__=='__main__':
    main()

Submission status: Accepted

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

li1=[]
li2=[]

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

li1=collections.Counter(li1).most_common()
li2=collections.Counter(li2).most_common()

if li1[0][0]!=li2[0][0]:
    print(n-li1[0][1]-li2[0][1])
else:
    if len(li1)==1 and len(li2)==1:
        print(n//2)
    elif len(li1)==1:
        print(n-li1[0][1]-li2[1][1])
    elif len(li2)==1:
        print(n-li1[1][1]-li2[0][1])
    else:
        print(min(n-li1[0][1]-li2[1][1],n-li1[1][1]-li2[0][1]))

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int,input().split()))
E = []
O = []
T = [[0,i] for i in range(10**5+1)]
U = [[0,i] for i in range(10**5+1)]
for i in range(n):
    if i%2 == 0:
        E.append(L[i])
        T[L[i]][0] += 1
    else:
        O.append(L[i])
        U[L[i]][0] += 1
T.sort()
U.sort()
if T[10**5][1] != U[10**5][1]:
    print(n-(T[10**5][0]+U[10**5][0]))
else:
    print(min(n-(T[10**5-1][0]+U[10**5][0]), n-(T[10**5][0]+U[10**5-1][0])))

Submission status: Accepted

Solution code:
from collections import Counter
from operator import itemgetter

n = int(input())
v = list(map(int, input().split()))
vodd = []
veven = []


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

lstodd = Counter(vodd)
lsteven = Counter(veven)

ans = []
for i, ii in zip(range(2), lstodd.most_common()):
    for j, jj in zip(range(2), lsteven.most_common()):
        if ii[0] == jj[0]:continue
        ans.append(len(vodd) - ii[1] + len(veven) - jj[1])
if ans == []:ans.append(n//2)

print(min(ans))

Submission status: Accepted

Solution code:
from collections import Counter


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

v_odd = []
v_even = []
for index in range(len(v)):
    i = index + 1
    if i % 2 == 0:
        v_even.append(v[index])
    else:
        v_odd.append(v[index])
# print(v_odd)
# print(v_even)
# print(Counter(v_odd).most_common())
# print(Counter(v_even).most_common())

odd_1st = Counter(v_odd).most_common()[0][1]
even_1st = Counter(v_even).most_common()[0][1]
if Counter(v_odd).most_common()[0][0] != Counter(v_even).most_common()[0][0]:
    number_of_rewrites = len(v_odd) - odd_1st
    number_of_rewrites += len(v_even) - even_1st
else:
    odd_2nd = Counter(v_odd).most_common()[1][1] if len(Counter(v_odd).most_common()) >= 2 else 0
    even_2nd = Counter(v_even).most_common()[1][1] if len(Counter(v_even).most_common()) >= 2 else 0

    if odd_2nd >= even_2nd:
        number_of_rewrites = len(v_odd) - odd_2nd + len(v_even) - even_1st
    else:
        number_of_rewrites = len(v_odd) - odd_1st + len(v_even) - even_2nd
    # number_of_rewrites = min(
    #     len(v_odd) - odd_1st + len(v_even) - even_2nd,
    #     len(v_odd) - odd_2nd + len(v_even) - even_1st,
    # )

print(number_of_rewrites)

Submission status: Accepted

Solution code:
from collections import Counter


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

v_odd = []
v_even = []
for index in range(len(v)):
    i = index + 1
    if i % 2 == 0:
        v_even.append(v[index])
    else:
        v_odd.append(v[index])
# print(v_odd)
# print(v_even)
# print(Counter(v_odd).most_common())
# print(Counter(v_even).most_common())

odd_1st = Counter(v_odd).most_common()[0][1]
even_1st = Counter(v_even).most_common()[0][1]
if Counter(v_odd).most_common()[0][0] != Counter(v_even).most_common()[0][0]:
    number_of_rewrites = len(v_odd) - odd_1st
    number_of_rewrites += len(v_even) - even_1st
else:
    odd_2nd = Counter(v_odd).most_common()[1][1] if len(Counter(v_odd).most_common()) >= 2 else 0
    even_2nd = Counter(v_even).most_common()[1][1] if len(Counter(v_even).most_common()) >= 2 else 0

    number_of_rewrites = min(
        len(v_odd) - odd_1st + len(v_even) - even_2nd,
        len(v_odd) - odd_2nd + len(v_even) - even_1st,
    )

print(number_of_rewrites)

Submission status: Accepted

Solution code:
from collections import Counter


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

v_odd = []
v_even = []
for index in range(len(v)):
    i = index + 1
    if i % 2 == 0:
        v_even.append(v[index])
    else:
        v_odd.append(v[index])
# print(v_odd)
# print(v_even)
# print(Counter(v_odd).most_common())
# print(Counter(v_even).most_common())

if Counter(v_odd).most_common()[0][0] != Counter(v_even).most_common()[0][0]:
    number_of_rewrites = len(v_odd) - Counter(v_odd).most_common()[0][1]
    number_of_rewrites += len(v_even) - Counter(v_even).most_common()[0][1]
else:
    odd_1st = Counter(v_odd).most_common()[0][1]
    even_1st = Counter(v_even).most_common()[0][1]
    odd_2nd = Counter(v_odd).most_common()[1][1] if len(Counter(v_odd).most_common()) >= 2 else 0
    even_2nd = Counter(v_even).most_common()[1][1] if len(Counter(v_even).most_common()) >= 2 else 0

    number_of_rewrites = min(
        len(v_odd) - odd_1st + len(v_even) - even_2nd,
        len(v_odd) - odd_2nd + len(v_even) - even_1st,
    )

print(number_of_rewrites)

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()
b=Counter(v[1::2]).most_common()
a.append([0,0])
b.append([0,0])
if a[0][0]!=b[0][0]:
    print(n-(a[0][1]+b[0][1]))
else:
    print(min(n-(a[1][1]+b[0][1]),n-(a[0][1]+b[1][1])))

Submission status: Accepted

Solution code:
import itertools
from collections import Counter

N = int(input())
V = [int(x) for x in input().split()]
EV_counter = Counter(V[::2])
OD_counter = Counter(V[1::2])

# 1種類しかないとき対策
EV_counter[-1] = 0
OD_counter[-2] = 0

# 上位2種しか使う必要がない
EV = EV_counter.most_common(2)
OD = OD_counter.most_common(2)

answer = N
for (k1,v1), (k2,v2) in itertools.product(EV,OD):
  if k1 == k2:
    continue
  x = (N - v1 - v2)
  if answer > x:
    answer = x

print(answer)

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int,input().split()))
a = []
b = []
for i in range(n):
    if i % 2 == 1:
        a.append(v[i])
    elif i % 2 == 0:
        b.append(v[i])
ac = collections.Counter(a)
bc = collections.Counter(b)
p = ac.most_common()[0]
q = bc.most_common()[0]
if p[0] != q[0]:
    print(n - p[1] - q[1])
else:
    x = len(ac)
    y = len(bc)
    if x >= 2 and y >= 2: 
        r = ac.most_common()[1]
        s = bc.most_common()[1]
        if r[1] > s[1]:
            print(n - r[1] - q[1])
        elif r[1] <= s[1]:
            print(n - p[1] - s[1])
    elif x == 1 and y == 1:
        print(n//2)
    elif x == 1:
        s = bc.most_common()[1]
        print(n - p[1] - s[1])
    elif y == 1:
        r = ac.most_common()[1]
        print(n - r[1] - q[1])

Submission status: Accepted

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

v_even = v[::2]
v_odd = v[1::2]
#print(v_even,v_odd)

from collections import Counter

c_even = Counter(v_even)
c_odd = Counter(v_odd)
elist = c_even.most_common()
olist = c_odd.most_common()

if elist[0][0] != olist[0][0]:
    print(len(v) - elist[0][1] - olist[0][1])
elif len(elist) >= 2 and len(olist) >= 2:
    print(len(v) - max(elist[0][1] + olist[1][1],elist[1][1] + olist[0][1]))
elif len(elist) >= 2:
    print(len(v) - max(elist[1][1] + olist[0][1], elist[0][1]))
elif len(olist) >= 2:
    print(len(v) - max(olist[1][1] + elist[0][1], olist[0][1]))
else:
    print(n//2)

Submission status: Accepted

Solution code:
from collections import*
n, *v = map(int, open(0).read().split())
a, b = [Counter(v[i::2]).most_common() + [(0,0)] for i in (0,1)]
s, x = a[0]
t, y = b[0]
print(n - [x+y, max(x+b[1][1], y+a[1][1])][s==t])

Submission status: Accepted

Solution code:
N = int(input())
v = list(map(int, input().split()))
d_ki = {0 : 0, -1 : 0}
d_gu = {-2 : 0, -3 : 0}
for i in range(N):
    if i % 2 == 0:
        if v[i] not in d_ki:
            d_ki[v[i]] = 1
        else:
            d_ki[v[i]] += 1
    else:
        if v[i] not in d_gu:
            d_gu[v[i]] = 1
        else:
            d_gu[v[i]] += 1
ki = sorted(d_ki.items(), key=lambda x : x[1], reverse=True)
gu = sorted(d_gu.items(), key=lambda x : x[1], reverse=True)
ans = 0
for i in range(2):
    for j in range(2):
        if ki[i][0] == gu[j][0]:
            continue
        ans = max(ans, ki[i][1] + gu[j][1])
print(N - ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
vlist = list(map(int,input().split()))
list_odd = [vlist[i] for i in  range(n) if i % 2 ==0]
list_even = [vlist[i] for i in range(n) if i % 2 !=0]

def f(list1):
  ary1 = [[0,i] for i in range(10**5+2)]
  len1 = len(list1)
  for i in range(len1):
    ary1[list1[i]+1][0] += 1
  ary2 = sorted(ary1)
  return [ary2[-1],ary2[-2]]

list3 = f(list_odd)
list4 = f(list_even)


if list3[0][1] != list4[0][1]:
  print(n-list3[0][0]-list4[0][0])
else:
  x1 = n-list3[0][0]-list4[1][0]
  x2 = n-list3[1][0]-list4[0][0]
  print(min(x1,x2))

Submission status: Accepted

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

v0 =[]
v1 =[]

for i in range(n):
  if i % 2 == 0:
    v0.append(v[i])
  else:
    v1.append(v[i])
  
v0.sort()
v1.sort()

v0_count = [[0,0]]
v1_count = [[0,0]]

#print(len(v0_count))
k = 0
for i in range(n//2):
  if v0[i] == v0_count[k][0]:
    v0_count[k][1] += 1
  else:
    v0_count.append([v0[i],1])
    k += 1


v0_count.sort(key=lambda x: x[1], reverse=True)

k = 0
for i in range(n//2):
  if v1[i] == v1_count[k][0]:
    v1_count[k][1] += 1
  else:
    v1_count.append([v1[i],1])
    k += 1


v1_count.sort(key=lambda x: x[1], reverse=True)

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

Submission status: Accepted

Solution code:
n=int(input())
V=list(map(int,input().split()))
O=[]
E=[]
for i in range(n//2):
    O.append(V[2*i])
    E.append(V[2*i+1])
O.sort()
E.sort()
CO=[]
CE=[]
no=O[0]
co=0
ne=E[0]
ce=0
for i in range(n//2):
    if O[i]==no:
        co+=1
    else:
        CO.append([co,no])
        co=1
        no=O[i]
    if E[i]==ne:
        ce+=1
    else:
        CE.append([ce,ne])
        ce=1
        ne=E[i]
CO.append([co,no])
CE.append([ce,ne])

CO=list(reversed(sorted(CO)))
CE=list(reversed(sorted(CE)))

if CO[0][1]==CE[0][1]:
    if len(CO)==1 and len(CE)==1:
        print(n//2)
    elif len(CO)==1:
        print(n-CO[0][0]-CE[1][0])
    elif len(CE)==1:
        print(n-CO[1][0]-CE[0][0])
    else:
        print(n-max(CO[0][0]+CE[1][0],CO[1][0]+CE[0][0]))
else:
    print(n-CO[0][0]-CE[0][0])

Submission status: Accepted

Solution code:
# solution

import fractions
import math
import io

data=int(input())

array=list(map(int,input().split()))

ki=[0]*100001
gu=[0]*100001

for i in range(data):
    if i%2==0:
        ki[array[i]]+=1
    else:
        gu[array[i]]+=1
km1=[max(ki),ki.index(max(ki))]
km2=[sorted(ki)[-2],ki.index(sorted(ki)[-2])]
gm1=[max(gu),gu.index(max(gu))]
gm2=[sorted(gu)[-2],gu.index(sorted(gu)[-2])]

if km1[1]!=gm1[1]:
    print(data-km1[0]-gm1[0])
else:
    if km1[0]-km2[0]>=gm1[0]-gm2[0]:
        print(data-km1[0]-gm2[0])
    else:
        print(data-gm1[0]-km2[0])

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
if n==2 and v[0]==v[1]:
    print(1)
else:
    c_e=[]
    c_o=[]
    for h in range(max(v)+1):
        c_e.append([0,h])
        c_o.append([0,h])

    for i in range(n):
        num=v[i]
        if i%2==0:
            c_e[num][0]+=1
        else:
            c_o[num][0]+=1

    c_e.sort(reverse=True)
    c_o.sort(reverse=True)
    ae=c_e[0][0]
    ao=c_o[0][0]

    if c_e[0][1]==c_o[0][1]:
        ae2=c_e[1][0]
        ao2=c_o[1][0]
        if ae2>=ao2:
            ae=ae2
        else:
            ao=ao2
            
    print(n-ae-ao)

Submission status: Accepted

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

import collections

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

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

v_even_s = sorted(v_even.items(), key=lambda x:x[1], reverse=True)
v_odd_s  = sorted(v_odd.items(), key=lambda x:x[1], reverse=True)

if len(set(V)) == 1:
    print(int(len(V)/2))
elif v_even_s[0][0] == v_odd_s[0][0]:
    if v_even_s[0][1] > v_odd_s[0][1]:
        print(len(V) - v_even_s[0][1] - v_odd_s[1][1])
    elif v_even_s[0][1] < v_odd_s[0][1]:
        print(len(V) - v_even_s[1][1] - v_odd_s[0][1])
    else:
        if v_even_s[1][1] > v_odd_s[1][1]:
            print(len(V) - v_even_s[1][1] - v_odd_s[0][1])
        else:
            print(len(V) - v_even_s[0][1] - v_odd_s[1][1])
else:
    print(len(V) - v_even_s[0][1] - v_odd_s[0][1])

Submission status: Accepted

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

v_ls0 = [0] * (n//2)
v_ls1 = [0] * (n//2)
for i in range(n):
    if i % 2 == 0:
        v_ls0[i//2] = v_ls[i]
    else:
        v_ls1[i//2] = v_ls[i]
counter0 = collections.Counter(v_ls0)
counter1 = collections.Counter(v_ls1)
value01, count01 = counter0.most_common(2)[0]
if len(counter0.most_common(2)) == 1:
    value02, count02 = 0,0
else:   
    value02, count02 = counter0.most_common(2)[1]
value11, count11 = counter1.most_common(2)[0]
if len(counter1.most_common(2)) == 1:
    value12, count12 = 0,0
else:
    value12, count12 = counter1.most_common(2)[1]

if value01 != value11:
    ans = n - count01 - count11
else:
    # v_ls0を最頻値で置き換える
    ans = n - count01 - count12
    ans = min(ans, n - count11 - count02)
print(ans)