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:
import collections

n = int(input())
v = list(map(int, input().split()))
v_odd = collections.Counter(v[::2])
v_even = collections.Counter(v[1::2])

if v_odd == v_even and len(v_odd) == 1:
    print(n // 2)
    exit()
if v_odd.most_common()[0][0] != v_even.most_common()[0][0]:
    print(len(v[::2]) - v_odd.most_common()[0][1] + len(v[1::2]) - v_even.most_common()[0][1])
else:
    c1 = len(v[1::2]) - v_even.most_common()[0][1] + len(v[::2]) - v_odd.most_common()[1][1]
    c2 = len(v[1::2]) - v_even.most_common()[1][1] + len(v[::2]) - v_odd.most_common()[0][1]
    print(min(c1, c2))

Submission status: Accepted

Solution code:
import sys
from collections import Counter

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
LIST = lambda: list(map(int, INPUT().split()))

sys.setrecursionlimit(10 ** 9)


def main():
    N = INT()
    V = LIST()
    a, b = [Counter(V[i::2]).most_common() + [(0, 0)] for i in (0, 1)]
    i, x = a[0]
    j, y = b[0]
    print(N - [x + y, max(x + b[1][1], y + a[1][1])][i == j])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
n=int(input())
v=list(input().split())
ans=0

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

ep=collections.Counter(even).most_common()
op=collections.Counter(odd).most_common()
ep.append((0,0))
op.append((0,0))

if ep[0] != op[0]:
    ans=n-(ep[0][1]+op[0][1])
else:
    if len(even)-ep[1][1]<len(odd)-op[1][1]:
        ans=n-ep[1][1]-op[0][1]
    else:
        ans=n-ep[0][1]-op[1][1]
print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())
v = [int(i) for i in input().split()]
o = collections.Counter(v[0::2])
e = collections.Counter(v[1::2])
o_count = o.most_common()[:2]
e_count = e.most_common()[:2]

ans = None
if o_count[0] != e_count[0]:
    ans = n - o_count[0][1] - e_count[0][1]
elif len(o_count) == 1 and len(e_count) == 1:
    ans = n // 2
elif len(o_count) == 1:
    ans = n//2 - e_count[1][1]
elif len(e_count) == 1:
    ans = n//2 - o_count[1][1]
else:
    ans = min(
        n - o_count[0][1] - e_count[1][1],
        n - e_count[0][1] - o_count[1][1]
    )
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    n=int(input())
    v=list(map(int,input().split()))
    do=defaultdict(int)
    de=defaultdict(int)
    for i,x in enumerate(v):
        if i%2:
            do[x]+=1
        else:
            de[x]+=1
    o1,e1,o2,e2=0,0,0,0
    for x in do.keys():
        if do[x]>o1:
            o2=o1
            o1=do[x]
            o1n=x
        else:
            o2=max(o2,do[x])
    for x in de.keys():
        if de[x]>e1:
            e2=e1
            e1=de[x]
            e1n=x
        else:
            e2=max(e2,de[x])
    if o1n!=e1n:
        print(n-(o1+e1))
    else:
        print(n-max(o1+e2,o2+e1))
            
            

if __name__ == '__main__':
    main()

Submission status: Accepted

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

odd = [a[i] for i in range(n) if i%2==0]
even = [a[i] for i in range(n) if i%2!=0]

cntOdd = Counter(odd).most_common(2)
cntEven = Counter(even).most_common(2)

 

if cntOdd[0][0] != cntEven[0][0]:
    print(n - cntOdd[0][1] - cntEven[0][1])
else:
    if len(cntEven) == 1 and len(cntEven):
        print(n//2)
    else:
        ans1 = n - cntOdd[1][1] - cntEven[0][1]
        ans2 = n - cntOdd[0][1] - cntEven[1][1]
        print(min(ans1, ans2))

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
v = list(map(int,input().split()))
even = [v[i] for i in range(n) if i % 2 == 0]
odd = [v[i] for i in range(n) if i%2==1]
ct_even = Counter(even).most_common(2)
ct_odd = Counter(odd).most_common(2)
if ct_even[0][0]!=ct_odd[0][0]:
  print(n-ct_even[0][1]-ct_odd[0][1])
else:
  if len(ct_even)==1 and len(ct_odd)==1:
    print(n//2)
  elif len(ct_even)!=1 and len(ct_odd)==1:
    print(n//2 - ct_even[1][1])
  elif len(ct_even)==1 and len(ct_odd)!=1:
    print(n//2-ct_odd[1][1])
  else:
    k=max(ct_even[0][1]+ct_odd[1][1], ct_even[1][1]+ct_odd[0][1])
    print(n-k)

Submission status: Accepted

Solution code:
from collections import Counter

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

if len(set(v)) != 1:
    a = Counter(v[0::2]).most_common()
    b = Counter(v[1::2]).most_common()
    if a[0][0] != b[0][0]:
        print((len(v[0::2]) - a[0][1]) + (len(v[1::2]) - b[0][1]))
    else:
        if a[0][1] < b[0][1]:
            print((len(v[0::2]) - a[0][1]) + (len(v[1::2]) - b[1][1]))
        elif a[0][1] > b[0][1]:
            print((len(v[0::2]) - a[1][1]) + (len(v[1::2]) - b[0][1]))
        elif a[0][1] == b[0][1]:
            if len(a) != 1 and len(b) != 1:
                if a[1][1] > b[1][1]:
                    print((len(v[0::2]) - a[1][1]) + (len(v[1::2]) - b[0][1]))
                elif a[1][1] < b[1][1]:
                    print((len(v[0::2]) - a[0][1]) + (len(v[1::2]) - b[1][1]))
                elif a[1][1] == b[1][1]:
                    print((len(v[0::2]) - a[0][1]) + (len(v[1::2]) - b[1][1]))
            elif len(a) != 1:
                print((len(v[0::2]) - a[1][1]) + (len(v[1::2]) - b[0][1]))
            elif len(b) != 1:
                print((len(v[0::2]) - a[0][1]) + (len(v[1::2]) - b[1][1]))
else:
    print(len(v[1::2]))

Submission status: Accepted

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

even_idx_cnt = {}
odd_idx_cnt = {}
for idx, v in enumerate(V):
    if idx % 2 == 0:
        if v not in even_idx_cnt:
            even_idx_cnt[v] = 0
        even_idx_cnt[v] += 1
    else:
        if v not in odd_idx_cnt:
            odd_idx_cnt[v] = 0
        odd_idx_cnt[v] += 1

if len(even_idx_cnt) == 1 and len(odd_idx_cnt) == 1:
    if even_idx_cnt.keys() == odd_idx_cnt.keys():
        print(N - even_idx_cnt[V[0]])
    else:
        print(0)
    exit()

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

if even_idx_cnt[0][0] != odd_idx_cnt[0][0]:
    print(N - even_idx_cnt[0][1] - odd_idx_cnt[0][1])
elif len(even_idx_cnt) == 1:
    print(N - even_idx_cnt[0][1] - odd_idx_cnt[1][1])
elif len(odd_idx_cnt) == 1:
    print(N - even_idx_cnt[1][1] - odd_idx_cnt[0][1])
else:
    if even_idx_cnt[1][1] > odd_idx_cnt[1][1]:
        print(N - even_idx_cnt[1][1] - odd_idx_cnt[0][1])
    else:
        print(N - even_idx_cnt[0][1] - odd_idx_cnt[1][1])

Submission status: Accepted

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

even_idx_cnt = {}
odd_idx_cnt = {}
for idx, v in enumerate(V):
    if idx % 2 == 0:
        if v not in even_idx_cnt:
            even_idx_cnt[v] = 0
        even_idx_cnt[v] += 1
    else:
        if v not in odd_idx_cnt:
            odd_idx_cnt[v] = 0
        odd_idx_cnt[v] += 1

if len(even_idx_cnt) == 1 and len(odd_idx_cnt) == 1:
    if even_idx_cnt.keys() == odd_idx_cnt.keys():
        print(N - even_idx_cnt[V[0]])
    else:
        print(0)
    exit()

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

if even_idx_cnt[0][0] != odd_idx_cnt[0][0]:
    print(N - even_idx_cnt[0][1] - odd_idx_cnt[0][1])
else:
    ans = []
    if len(odd_idx_cnt) >= 2:
        ans.append(N - even_idx_cnt[0][1] - odd_idx_cnt[1][1])
    if len(even_idx_cnt) >= 2:
        ans.append(N - even_idx_cnt[1][1] - odd_idx_cnt[0][1])
    print(min(ans))

Submission status: Accepted

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

d1 = {0:0}
d2 = {0:0}
for i, v in enumerate(V):
    d = d2 if i&1 else d1
    if v not in d: d[v]=0
    d[v]+=1
    

d1 = sorted(list(d1.items()), key=lambda x:-x[1])
d2 = sorted(list(d2.items()), key=lambda x:-x[1])

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

Submission status: Accepted

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

dic0 = [0 for i in range(10**5+1)]
dic1 = [0 for i in range(10**5+1)]
for i in range(n):
    if i%2==0:    
        dic0[v[i]] += 1
    if i%2==1:
        dic1[v[i]] += 1

MX0_1 = max(dic0)
MX1_1 = max(dic1)

li0 = []
li1 = []


for i in range(10**5+1):
    # if dic0[i]>10:
    #     print(i,dic0[i])
    # if dic1[i]>10:
    #     print(i,dic1[i],"\n")
    
    if dic0[i] == MX0_1:
        li0.append(i)
        dic0[i] = 0
    if dic1[i] == MX1_1:
        li1.append(i)
        dic1[i] = 0

# print(li0,li1)
# print(MX0_1,MX0_1)

if len(li0)>=2 or len(li1)>=2:
    print(n-MX0_1-MX1_1)
elif li0[0] == li1[0]:
    print(min(n-MX0_1-max(dic1), n-MX1_1-max(dic0)))
elif li0[0] != li1[0]:
    print(n-MX0_1-MX1_1)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
O=A[0::2]
E=A[1::2]

from collections import Counter
o=Counter(O).most_common(2)
o.append((0,0))
e=Counter(E).most_common(2)
e.append((0,0))

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

Submission status: Accepted

Solution code:
n = int(input())
S = [int(x) for x in input().split()]

S0 = {}
S1 = {}
for i in range(len(S)):
  if i%2 == 0:
    if S[i] not in S0:
      S0[S[i]] =1
    else:
      S0[S[i]] += 1
  else:
    if S[i] not in S1:
      S1[S[i]] =1
    else:
      S1[S[i]] += 1

if max(S0, key = S0.get) != max(S1, key = S1.get):
  Answer = len(S) - max(S0.values()) -max(S1.values())
  print(Answer)
  exit()
      
S0list = sorted(list(S0.values()),reverse = True)
S1list = sorted(list(S1.values()),reverse = True)

S00 = S0list[0]

if len(S0list) == 1:
  S01 = 0
else:
  S01 = S0list[1]
S10 = S1list[0]
if len(S1list) == 1:
  S11 = 0
else:
  S11 = S1list[1]
Answer = min(len(S) - S00 - S11,len(S) - S01 - S10)

print(Answer)

Submission status: Accepted

Solution code:
#ABC111C
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
a=Counter(v[::2]).most_common(2)+[(0,0)]
b=Counter(v[::-2]).most_common(2)+[(0,0)]
#most_common()の引数は頻度が高い数を何番目まで表示するか
#[(0,0)]を追加しないとvがすべて同じ場合範囲外指定してしまう
if a[0][0]!=b[0][0]:
    print(n-a[0][1]-b[0][1])
else:
    print(n-max(a[0][1]+b[1][1],a[1][1]+b[0][1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
ans = 0
f,s = [],[]
count = 0
for i in map(int,input().split()):
    if count%2 == 0:
        f.append(i)
    else:
        s.append(i)
    count += 1
c1,c2 = Counter(f),Counter(s)
a,b = [],[]
for i in c1.keys():
    a.append([i,c1[i]])
for j in c2.keys():
    b.append([j,c2[j]])
a = sorted(a,key=lambda x: x[1],reverse = True)
b = sorted(b,key=lambda x: x[1],reverse = True)
if a[0][0] == b[0][0]:
    if a[0][1] == b[0][1]:
        if len(a) == 1 and len(b) == 1:
            print(n-a[0][1])
        elif len(a) == 1 or len(b) == 1:
            if len(a) != 1:
                print(n-a[1][1]-b[0][1])
            else:
                print(n-a[0][1]-b[1][1])
        else:
            print(n-a[0][1]-max(a[1][1],b[1][1]))
    elif a[0][1] > b[0][1]:
        if len(b) == 1:
            print(n-a[0][1])
        else:
            print(n-a[0][1]-b[1][1])
    else:
        if len(a) == 1:
            print(n-b[0][1])
        else:
            print(n-a[1][1]-b[0][1])
else:
    print(n-a[0][1]-b[0][1])

Submission status: Accepted

Solution code:
import collections

def f(l):
    array = []
    c = collections.Counter(l)
    for key, val in c.items():
        array.append([key,val])
    array = sorted(array, key=lambda x:x[1], reverse=True)
    return array

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

    V_odd = V[::2]
    V_even = V[1::2]
    mode_list_odd = f(V_odd)
    mode_list_even = f(V_even)

    first_mode_odd = mode_list_odd[0][0]
    first_mode_even = mode_list_even[0][0]

    ans = 0
    if first_mode_odd == first_mode_even:
        if len(mode_list_odd) == 1:
            if len(mode_list_even) == 1:
                ans = min(len(V_odd),len(V_even))
            else:
                ans = len(V_even)-mode_list_even[1][1]
        else:
            if len(mode_list_even) == 1:
                ans = len(V_odd)-mode_list_odd[1][1]
            else:
                count1 = len(V_odd)-mode_list_odd[0][1]+len(V_even)-mode_list_even[1][1]
                count2 = len(V_odd)-mode_list_odd[1][1]+len(V_even)-mode_list_even[0][1]
                ans = min(count1, count2)
    else:
        ans = len(V_odd)-mode_list_odd[0][1] + len(V_even)-mode_list_even[0][1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
V = list(map(int,input().split()))
c1 = Counter(V[::2])
c2 = Counter(V[1::2])

l1 = sorted(c1.items(),key=lambda x:x[1], reverse=True)
l2 = sorted(c2.items(),key=lambda x:x[1], reverse=True)
l1.append((-1,-1))
l2.append((-1,-1))

if l1[0][0] == l2[0][0]:
    if l1[0][1] == l2[0][1] == n // 2: ans = n // 2
    else: ans = n - max(l1[0][1]+l2[1][1],l1[1][1]+l2[0][1])
else:
    ans = n - l1[0][1] - l2[0][1]
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter



def main():
    N = int(input())
    V = [int(x) for x in input().split()]

    ac = Counter()
    bc = Counter()
    for i in range(N):
        if i & 1:
            ac[V[i]] += 1
        else:
            bc[V[i]] += 1

    acm = ac.most_common()
    bcm = bc.most_common()

    if acm[0][0] != bcm[0][0]:
        print(N // 2 - acm[0][1] + N // 2 - bcm[0][1])
    else:
        ans = float("inf")
        for i in acm:
            if i[0] == bcm[0][0]:
                continue
            ans = min(ans, N // 2 - i[1] + N // 2 - bcm[0][1])

        for j in bcm:
            if acm[0][0] == j[0]:
                continue
            ans = min(ans, N // 2 - j[1] + N // 2 - acm[0][1])
        if ans == float("inf"):
            print(N // 2)
        else:
            print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter



def main():
    N = int(input())
    V = [int(x) for x in input().split()]

    a = []
    b = []
    for i in range(N):
        if i & 1:
            a.append(V[i])
        else:
            b.append(V[i])

    ac = Counter(a)
    bc = Counter(b)

    acm = ac.most_common()
    bcm = bc.most_common()

    if acm[0][0] != bcm[0][0]:
        print(N // 2 - acm[0][1] + N // 2 - bcm[0][1])
    else:
        ans = float("inf")
        for i in acm:
            if i[0] == bcm[0][0]:
                continue
            ans = min(ans, N // 2 - i[1] + N // 2 - bcm[0][1])

        for j in bcm:
            if acm[0][0] == j[0]:
                continue
            ans = min(ans, N // 2 - j[1] + N // 2 - acm[0][1])
        if ans == float("inf"):
            print(N // 2)
        else:
            print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter

n, *v = map(int, sys.stdin.read().split())

def main():
    c1 = sorted(Counter(v[::2]).items(), reverse=True, key=lambda x: x[1])
    c2 = sorted(Counter(v[1::2]).items(), reverse=True, key=lambda x: x[1])
    
    if c1[0][0] != c2[0][0]:
        return n // 2 - c1[0][1] + n // 2 - c2[0][1]
    else:
        if len(c1) == len(c2) == 1:
            return n // 2
        elif len(c1) == 1:
            return n // 2 - c2[1][1]
        elif len(c2) == 1:
            return n // 2 - c1[1][1]
        else:
            return n - max(c1[0][1] + c2[1][1], c1[1][1] + c2[0][1])

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = [i for i in map(int,input().split())]
v_odd = v[::2]
v_even = v[1::2]
odd_nums = (collections.Counter(v_odd)).most_common()
even_nums = (collections.Counter(v_even)).most_common()
ans = 0
if odd_nums[0][0] != even_nums[0][0]: # 最頻出数値が等しくない場合
  for i in range(1,len(odd_nums)):
    ans += odd_nums[i][1]
  for i in range(1,len(even_nums)):
    ans += even_nums[i][1]
else: # 最頻出数値が等しい場合
  ans1=0
  ans2=0
  for i in range(1,len(odd_nums)):
    ans1 += odd_nums[i][1]
  for i in range(len(even_nums)):
    if i==1:
      continue
    ans1 += even_nums[i][1]
  for i in range(len(odd_nums)):
    if i==1:
      continue
    ans2 += odd_nums[i][1]
  for i in range(1,len(even_nums)):
    ans2 += even_nums[i][1]
  ans = min(ans1,ans2)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC111C
import collections

n = int(input())
v = list(map(int, input().split()))
if len(set(v)) == 1:
    print(n//2)
    exit()

v_odd = [0] * (n // 2)
v_even = [0] * (n // 2)
for i in range(n):
    if (i+1)%2 == 1:
        v_odd[i//2] = v[i]
    else:
        v_even[i//2] = v[i]
c_v_odd = collections.Counter(v_odd)
c_v_even = collections.Counter(v_even)

if c_v_odd.most_common()[0][0] == c_v_even.most_common()[0][0]:
    if c_v_odd.most_common()[0][1] > c_v_even.most_common()[0][1]:
        odd_count = n//2 - c_v_odd.most_common()[0][1]
        even_count = n//2 - c_v_even.most_common()[1][1]
        ans = odd_count + even_count
    elif c_v_odd.most_common()[0][1] < c_v_even.most_common()[0][1]:
        odd_count = n//2 - c_v_odd.most_common()[1][1]
        even_count = n//2 - c_v_even.most_common()[0][1]
        ans = odd_count + even_count
    else:
        if c_v_odd.most_common()[1][1] > c_v_even.most_common()[1][1]:
            odd_count = n//2 - c_v_odd.most_common()[1][1]
            even_count = n//2 - c_v_even.most_common()[0][1]
            ans = odd_count + even_count
        else:
            odd_count = n//2 - c_v_odd.most_common()[0][1]
            even_count = n//2 - c_v_even.most_common()[1][1]
            ans = odd_count + even_count
else:
    odd_count = n//2 -  c_v_odd.most_common()[0][1]
    even_count = n//2 -  c_v_even.most_common()[0][1]
    ans = odd_count + even_count
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

o = v[1::2]
e = v[::2]

cnt_o = Counter(o).most_common() + [(0,0)]
cnt_e = Counter(e).most_common() + [(0,0)]

print(n-cnt_o[0][1]-cnt_e[0][1] if cnt_o[0][0] != cnt_e[0][0] else n-max(cnt_e[0][1]+cnt_o[1][1], cnt_e[1][1]+cnt_o[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter

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

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

if n % 2 == 0:
    al = n // 2
    bl = al
else:
    al = n // 2 + 1
    bl = n // 2

if len(a) == 1 and len(b) == 1:
    if a[0][0] == b[0][0]:
        print(min(a[0][1], b[0][1]))
    else:
        print(0)
elif len(a) == 1 or len(b) == 1:
    if len(a) == 1:
        if a[0][0] == b[0][0]:
            print(bl - b[1][1])
        else:
            print(bl - b[0][1])
    if len(b) == 1:
        if a[0][0] == b[0][0]:
            print(al - a[1][1])
        else:
            print(al - a[0][1])
else:
     if a[0][0] == b[0][0]:
         print(min(n - a[0][1] - b[1][1], n - a[1][1] - b[0][1])) 
     else:
         print(n - a[0][1] - b[0][1])

Submission status: Accepted

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

h_o_l = len(h[1::2])
h_e_l = len(h[::2])

h_o = collections.Counter(h[1::2])
h_e = collections.Counter(h[::2])

o_ = h_o.most_common()
e_ = h_e.most_common()
o_.append((0,0))
e_.append((0,0))

if o_[0][0] != e_[0][0]:
    print(N-o_[0][1]-e_[0][1])
elif o_[0][0] == e_[0][0]:
    a =min(N-o_[0][1]-e_[1][1],N-e_[0][1]-o_[1][1])
    print(a)

Submission status: Accepted

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

Odd, Evn = [], []
for i in range(n):
    Evn.append(V[i]) if i%2 == 0 else Odd.append(V[i])
Evn = sorted( dict(Counter(Evn)).items(), key = lambda x:x[1], reverse=True)
Odd = sorted( dict(Counter(Odd)).items(), key = lambda x:x[1], reverse=True)

cnt = -1
for evn in Evn[0:2]:
    for odd in Odd[0:2]:
        if evn[0] != odd[0]:
            cnt = max(cnt, evn[1]+odd[1])
print(n-cnt) if cnt > 0 else print(n//2)

Submission status: Accepted

Solution code:
import math
import itertools
import fractions
import heapq
import collections
import bisect
import sys

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

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

def counter_top(c,exclude):
    for a in c.most_common():
        if a[0]!=exclude:
            return a
    return (object(),0)

def conv_seq(seq,exclude = None):
    c = collections.Counter(seq)
    mc = counter_top(c,exclude)
    return sum(c.values()) - mc[1],mc[0]

N = I()
V = LI()

s1,a = conv_seq(V[::2])
s2,b = conv_seq(V[1::2])


if s1!=s2:
    print(s1+s2)
else:
    s1_new,_ = conv_seq(V[::2],exclude=b)
    s2_new,_ = conv_seq(V[1::2],exclude=a)
    print(min(s1_new+s2,s1+s2_new))

Submission status: Accepted

Solution code:
import collections

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

v0 = v[0::2]
v1 = v[1::2]

c0 = collections.Counter(v0)
c1 = collections.Counter(v1)

ans0 = n//2 - c0.most_common()[0][1]
ans1 = n//2 - c1.most_common()[0][1]

if c0.most_common()[0][0] == c1.most_common()[0][0]:
    if len(c0.most_common()) > 1:
        ans0_1 = n//2 - c0.most_common()[1][1]
    else:
        ans0_1 = n//2

    if len(c1.most_common()) > 1:
            ans1_1 = n//2 - c1.most_common()[1][1]
    else:
        ans1_1 = n//2

    ans = min(ans0 + ans1_1, ans0_1 + ans1)
else:
    ans = ans0 + ans1

print(ans)

Submission status: Accepted

Solution code:
def main():
    n, *v = map(int, open(0).read().split())
    be = [0] * int(1e5)
    bo = [0] * int(1e5)
    for e, o in zip(*[iter(v)] * 2):
        be[e - 1] += 1
        bo[o - 1] += 1
    if be.index(max(be)) == bo.index(max(bo)):
        ve = max(be)
        vo = max(bo)
        be[be.index(ve)] = 0
        bo[bo.index(vo)] = 0
        print(min(n - ve - max(bo), n - vo - max(be)))
    else:
        print(n - max(be) - max(bo))
    return


main()

Submission status: Accepted

Solution code:
import os
import sys

import numpy as np

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

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N = int(sys.stdin.buffer.readline())
V = list(map(int, sys.stdin.buffer.readline().split()))

V = np.array(V, dtype=int)

V1 = V[::2]
V2 = V[1::2]
vc1 = np.bincount(V1, minlength=10 ** 5 + 1)
vc2 = np.bincount(V2, minlength=10 ** 5 + 1)

i1 = vc1.argmax()
i2 = vc2.argmax()

if i1 != i2:
    ans = N - (vc1[i1] + vc2[i2])
    print(ans)
else:
    m1 = vc1[i1]
    vc1[i1] = 0
    m2 = vc2[i2]
    vc2[i2] = 0
    j1 = vc1.argmax()
    j2 = vc2.argmax()
    ans = N - max(m1 + vc2[j2], m2 + vc1[j1])
    print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int, input().split()))
even = []
odd = []
for i in range(n):
    if i % 2 == 0:
        even += [v[i]]
    else:
        odd += [v[i]]
even_dict = collections.Counter(even)
odd_dict = collections.Counter(odd)
max_even = max(even_dict.items(), key = lambda x:x[1])
max_odd = max(odd_dict.items(), key = lambda x:x[1])
#最高の値が同じでない
if max_even[0] != max_odd[0]:
    print(len(even) - max_even[1] + len(odd) - max_odd[1])
else:
    #同じだった場合
    if max_even[1] == len(even) and max_odd[1] == len(odd):
        second_even = (-1,0)
        second_odd = (-1,0)
    elif max_even[1] == len(even):
        second_even = (-1,0)
        second_odd = max([i for i in odd_dict.items() if i != max_odd], key = lambda x:x[1])
    elif max_odd[1] == len(odd):
        second_even = max([i for i in even_dict.items() if i != max_even], key = lambda x:x[1])
        second_odd = (-1,0)
    else:
        second_even = max([i for i in even_dict.items() if i != max_even], key = lambda x:x[1])
        second_odd = max([i for i in odd_dict.items() if i != max_odd], key = lambda x:x[1])
    #二つ目に大きいやつも同じかどうか
    if second_even[1] > second_odd[1]:
        print(len(even) - second_even[1] + len(odd) - max_odd[1])
    else:
        print(len(even) - max_even[1] + len(odd) - second_odd[1])

Submission status: Accepted

Solution code:
import sys, math
def input():
    return sys.stdin.readline()[:-1]
from itertools import permutations, combinations
from collections import defaultdict, Counter

sys.setrecursionlimit(10**7)
n = int(input())
v = list(map(int, input().split()))

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

c1 = Counter(v1).most_common()
c2 = Counter(v2).most_common()

cnt1 = c1[0][1]
cnt2 = c2[0][1]

if c1[0][0] != c2[0][0]:
    print(n-cnt1-cnt2)
else:
    if len(c1)==1 or len(c2)==2:
        print(n//2)
    else:
        cnt11 = n - c1[1][1] - c2[0][1]
        cnt22 = n - c1[0][1] - c2[1][1]
        print(min(cnt11, cnt22))

Submission status: Accepted

Solution code:
import collections

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

ve = []
for i in range(0, n, 2):
    ve.append(v[i])
ce = collections.Counter(ve).most_common()
vo = []
for i in range(1, n, 2):
    vo.append(v[i])
co = collections.Counter(vo).most_common()

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

Submission status: Accepted

Solution code:

n = int(input())
V = list(map(int, input().split()))
A = [] # 偶数
B = [] # 奇数

import collections

for i in range(n):
  if i % 2 == 0:
    A.append(V[i])
  else:
    B.append(V[i])

A = collections.Counter(A).most_common()[:2]
B = collections.Counter(B).most_common()[:2]
count = 0

if A[0][0] == B[0][0]:
  count += max(A[0][1], B[0][1])
  if A[0][1] == B[0][1]:
    if len(A) > 1 and len(B) > 1:
      count += max(A[1][1], B[1][1])
    elif len(A) > 1:
      count += max(A[1][1])
    elif len(B) > 1:
      count += max(B[1][1])
else:
  count += A[0][1] + B[0][1]

print(n-count)

Submission status: Accepted

Solution code:
import collections

N = int(input())
A,B = [], []
for i,v in enumerate(input().split()):
    if i%2==0:
        A.append(int(v))
    else:
        B.append((int(v)))

ca = collections.Counter(A).most_common(2)
cb = collections.Counter(B).most_common(2)
if len(ca)<2:
    ca.append((-1, 0))
if len(cb)<2:
    cb.append((-2, 0))

ans = 10**6
for v1,c1 in ca:
    for v2,c2 in cb:
        if v1!=v2:
            ans = min(ans, N-c1-c2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

if sum(V)/N == V[0] and sum(V)/N == V[N-1]:
  print(N//2)
  exit()

def get_most_common(lis):
  c = Counter(lis)
  m = c.most_common()
  return m

odds = V[0::2]
evens = V[1::2]

m_odds = get_most_common(odds)
m_evens = get_most_common(evens)

if m_odds[0][0] == m_evens[0][0]:
  minus = max(m_odds[0][1] + m_evens[1][1], m_odds[1][1] + m_evens[0][1])
else:
  minus = m_odds[0][1] + m_evens[0][1]
  
print(N - minus)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
v = list(map(int, input().split()))
v1 = [v[i] for i in range(0, n, 2)]
v2 = [v[i] for i in range(1, n, 2)]

cnt1 = defaultdict(int)
cnt2 = defaultdict(int)
for x in v1:
  cnt1[x] += 1
for x in v2:
  cnt2[x] += 1
vc1 = []
vc2 = []
for k, v in cnt1.items():
  vc1.append((v, k))
for k, v in cnt2.items():
  vc2.append((v, k))
vc1.sort()
vc1.reverse()
vc2.sort()
vc2.reverse()

vc1.append((0, vc1[0][0] + 1))
vc2.append((0, vc2[0][0] + 1))

ans = n
for _, x1 in vc1[:2]:
  for _, x2 in vc2[:2]:
    if x1 == x2: continue
    s = 0
    for x in v1:
      if x != x1: s += 1
    for x in v2:
      if x != x2: s += 1
    ans = min(ans, s)
print(ans)