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:
#3
n = int(input())
lista = [int(i) for i in input().split()]

evn = {}
odd = {}
for i in range(n):
    #print(i,evn,odd)
    if i%2==0:
        candi = lista[i]
        if candi not in evn:
            evn[candi] = 1
        else:
            evn[candi] += 1
    else:
        candi = lista[i]
        if candi not in odd:
            odd[candi] = 1
        else:
            odd[candi] += 1
        
maxevnkey1 = 0
maxevnval1 = 0
maxevnkey2 = 0
maxevnval2 = 0

for key in evn:
    val = evn[key]
    if val>maxevnval2:
        if val>=maxevnval1:
            maxevnkey2,maxevnval2 = maxevnkey1,maxevnval1
            maxevnkey1,maxevnval1 = key,val
        else:
            maxevnkey2,maxevnval2 = key,val

maxoddkey1 = 0
maxoddval1= 0
maxoddkey2 = 0
maxoddval2= 0

for key in odd:
    val = odd[key]
    if val>maxoddval2:
        if val>=maxoddval1:
            maxoddkey2,maxoddval2 = maxoddkey1,maxoddval1
            maxoddkey1,maxoddval1 = key,val
        else:
            maxoddkey2,maxoddval2 = key,val
            
ans = 0
if maxevnkey1!=maxoddkey1:
    ans = n-maxevnval1-maxoddval1
else:
    temp = max(maxevnval1+maxoddval2,maxevnval2+maxoddval1)
    ans = n-temp
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
V = [int(a) for a in input().split(" ")]
arr1,arr2 = V[::2], V[1:][::2]
dic1,dic2 = defaultdict(lambda: 0), defaultdict(lambda: 0)
for v1, v2 in zip(arr1, arr2):
    dic1[v1] += 1
    dic2[v2] += 1
rank1 = sorted(dic1.items(), key=lambda t: t[1])[::-1]
rank2 = sorted(dic2.items(), key=lambda t: t[1])[::-1]
if rank1[0][0] == rank2[0][0]:
    if rank1[0][1] == rank2[0][1]:
        nadd1 = rank1[1][1] if len(rank1)>=2 else 0
        nadd2 = rank2[1][1] if len(rank2)>=2 else 0
        print(N-rank1[0][1]-max(nadd1, nadd2))
    else:
        rank1, rank2 = rank1, rank2 if rank1[0][0]>rank2[0][0] else rank2, rank1
        nadd = rank2[1][1] if len(rank2)>=2 else 0
        print(N-rank1[0][1]-nadd)
else:
    print(N-(rank1[0][1]+rank2[0][1]))

Submission status: Accepted

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

if even[0][0] != odd[0][0]:
    ans =  n - even[0][1] - odd[0][1]
else:
    if len(even) == 1 or len(odd)==1:#単一の要素からなる場合ex.)1111
        ans = n//2
    else:
        ans =  n - max(even[0][1] + odd[1][1],even[1][1]+odd[0][1])
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from operator import itemgetter
import sys
input = sys.stdin.readline

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

a = defaultdict(int)
b = defaultdict(int)
a[0] = 0
b[0] = 0

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

a_sorted = sorted(a.items(), key=itemgetter(1), reverse=True)
b_sorted = sorted(b.items(), key=itemgetter(1), reverse=True)
if a_sorted[0][0] == b_sorted[0][0]:
    c1 = n/2 - a_sorted[0][1]
    c1 += n/2 - b_sorted[1][1]
    c2 = n/2 - b_sorted[0][1]
    c2 += n/2 - a_sorted[1][1]
    c = min(c1, c2)
else:
    c = n/2 - a_sorted[0][1]
    c += n/2 - b_sorted[0][1]
print(int(c))

Submission status: Accepted

Solution code:
###############################################################################

from bisect import bisect_left as binl
from copy import copy, deepcopy

def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


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


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


###############################################################################


def make_1st_2nd_list(countlist):
    max_v_list = []
    max_v_list_2nd = []
    max_count = -1
    max_count_2nd = -1

    for i, count in enumerate(countlist):
        if count > max_count:
            max_count_2nd = max_count
            max_v_list_2nd = max_v_list
            max_count = count
            max_v_list = [i]
        elif count == max_count:
            max_v_list.append(i)
        elif count > max_count_2nd:
            max_count_2nd = count
            max_v_list_2nd = [i]
        elif count == max_count_2nd:
            max_v_list_2nd.append(i)

    return (max_v_list, max_v_list_2nd)


def main():
    n = intin()
    vlist = intina()

    countlist0 = [0] * (10**5 + 1)
    countlist1 = [0] * (10**5 + 1)
    for i, v in enumerate(vlist):
        if i % 2 == 0:
            countlist0[v] += 1
        else:
            countlist1[v] += 1

    max_v_list0, max_v_list_2nd0 = make_1st_2nd_list(countlist0)
    max_v_list1, max_v_list_2nd1 = make_1st_2nd_list(countlist1)

    if len(max_v_list0) == 1 and len(max_v_list1) == 1 and \
       max_v_list0[0] == max_v_list1[0]:
        if not max_v_list_2nd0 and not max_v_list_2nd1:
            print(n // 2)
        elif not max_v_list_2nd0:
            print(n - countlist0[max_v_list0[0]]
                  - countlist1[max_v_list_2nd1[0]])
        elif not max_v_list_2nd1:
            print(n - countlist0[max_v_list_2nd0[0]]
                  - countlist1[max_v_list1[0]])
        else:
            c0 = countlist0[max_v_list0[0]]
            c1 = countlist1[max_v_list1[0]]

            ans1 = n - countlist0[max_v_list0[0]] \
                  - countlist1[max_v_list_2nd1[0]]
            ans2 = n - countlist0[max_v_list_2nd0[0]] \
                  - countlist1[max_v_list1[0]]
            print(min(ans1, ans2))

        return

    print(n - countlist0[max_v_list0[0]]
          - countlist1[max_v_list1[0]])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

N = int(input())
L = [int(v) for v in input().split()]

m = [{}, {}]
vm = [[(0, 0), (0, 0)], [(0, 0), (0, 0)]]

for i in range(N):
    if m[i % 2].get(L[i]) != None:
        m[i % 2][L[i]] += 1
    else:
        m[i % 2][L[i]] = 1

for i in range(N):
    k = L[i]
    v = m[i % 2][k]
    t1 = vm[i % 2][0]
    t2 = vm[i % 2][1]
    
    if t1[1] < v:
        vm[i % 2][1] = [t1[0], t1[1]]
        vm[i % 2][0] = [k, v]
    elif t2[1] < v and t1[0] != k:
        vm[i % 2][1] = [k, v]
        
f1 = vm[0][0]
f2 = vm[0][1]
s1 = vm[1][0]
s2 = vm[1][1]

p = 0
if f1[0] != s1[0]:
    p = N - f1[1] - s1[1]
else:
    p = N - max([f1[1] + s2[1], f2[1] + s1[1]])
    
print(p)

Submission status: Accepted

Solution code:
import collections
n=int(input())
v=list(map(int,input().split()))
v0=[]
v1=[]
for i in range(n//2):
    v0.append(v[2*i])
    v1.append(v[2*i+1])
if collections.Counter(v0).most_common()[0][0] == collections.Counter(v1).most_common()[0][0]:
    if collections.Counter(v0).most_common()[0][1] == len(v0):
        if collections.Counter(v1).most_common()[0][1] == len(v1):
            print(len(v0))
        else:
            print(len(v1)-collections.Counter(v1).most_common()[1][1])
    else:
        if collections.Counter(v1).most_common()[0][1] == len(v1):
            print(len(v0)-collections.Counter(v0).most_common()[0][1])
        else:
            print(min(len(v)-collections.Counter(v0).most_common()[1][1]-collections.Counter(v1).most_common()[0][1],\
                     len(v)-collections.Counter(v0).most_common()[0][1]-collections.Counter(v1).most_common()[1][1]))
else:
    print(len(v)-collections.Counter(v0).most_common()[0][1]-collections.Counter(v1).most_common()[0][1])

Submission status: Accepted

Solution code:
import collections

n=int(input())
a=[int(x) for x in input().split()]
evn=[]
odd=[]

def ct(lrg,sml):
    for lst in [lrg,sml]:
        if len(lst)==1:
            lst.append((0,0))
    if lrg[0][0]==sml[0][0]:
        return min(n-lrg[1][1]-sml[0][1],n-lrg[0][1]-sml[1][1])
    else:
        return n-lrg[0][1]-sml[0][1]

for i in range(len(a)):
    if i%2:
        evn.append(a[i])
    else:
        odd.append(a[i])
        
cevn=collections.Counter(evn).most_common()
codd=collections.Counter(odd).most_common()

if cevn[0][1]>codd[0][1]:
    ans = ct(cevn,codd)
else:
    ans = ct(codd,cevn)
    
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
v_list=list(map(int,input().split()))
even=1
e_dict={}
o_dict={}
for i in v_list:
    if even==1:
        if i in e_dict:
            e_dict[i]=e_dict[i]+1
        else:
            e_dict[i]=1
        even=0
    else:
        if i in o_dict:
            o_dict[i]=o_dict[i]+1
        else:
            o_dict[i]=1
        even=1
max_e=max(e_dict,key=e_dict.get)
max_o=max(o_dict,key=o_dict.get)
if max_e==max_o:
    max1e=max(e_dict.values())
    max1o=max(o_dict.values())
    e_dict.pop(max_e)
    o_dict.pop(max_o)
    if e_dict=={}:
        max2e=0
    else:
        max2e=max(e_dict.values())
    if o_dict=={}:
        max2o=0
    else:
        max2o=max(o_dict.values())
    answer=min(n-max1e-max2o,n-max1o-max2e)
    print(answer)
else:
    answer=n-max(e_dict.values())-max(o_dict.values())
    print(answer)

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
from collections import Counter
even=[]
odd=[]
for i in range(n):
    if i%2==0:
        even.append(v[i])
    else:
        odd.append(v[i])
E=Counter(even).most_common()
O=Counter(odd).most_common()
E.append([0,0])
O.append([0,0])
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:
import numpy as np
from copy import deepcopy
import math
import itertools
from collections import Counter

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

V_odd = [V[i] for i in range(N) if i % 2 == 1]
V_even = [V[i] for i in range(N) if i % 2 == 0]


char_common_odd_1 = Counter(V_odd).most_common(2)[0][0]
n_common_odd_1 = Counter(V_odd).most_common(2)[0][1]
char_common_even_1 = Counter(V_even).most_common(2)[0][0]
n_common_even_1 = Counter(V_even).most_common(2)[0][1]


if char_common_odd_1 == char_common_even_1: # 最頻出の文字が同じ場合
  if (n_common_odd_1 == N/2) and (n_common_even_1 == N/2): # 全ての文字が同じ場合
    ans = N/2
  elif (n_common_odd_1 == N/2): # 奇数が同じ文字の場合
    n_common_even_2 = Counter(V_even).most_common(2)[1][1]
    ans = N/2 - n_common_even_2
  elif (n_common_even_1 == N/2): # 偶数が同じ文字の場合
    n_common_odd_2 = Counter(V_odd).most_common(2)[1][1]
    ans = N/2 - n_common_odd_2
  else: # 偶奇それぞれ2種類以上の数字がある場合
    n_common_odd_2 = Counter(V_odd).most_common(2)[1][1]
    n_common_even_2 = Counter(V_even).most_common(2)[1][1]
    var_odd1_even2 = N - n_common_odd_1 - n_common_even_2
    var_odd2_even1 = N - n_common_odd_2 - n_common_even_1
    ans = min(var_odd1_even2, var_odd2_even1)
else: # 最頻出の文字が違う場合
  ans = N - n_common_odd_1 - n_common_even_1
  
print(int(ans))

Submission status: Accepted

Solution code:
import re
n = int(input())
vs = list(map(int, input().split()))
edic = {}
odic = {}

for i in range(n):
    item = vs[i]
    if i % 2 == 0:
        if item in edic:
            edic[item] += 1
        else:
            edic[item] = 1
    else:
        if item in odic:
            odic[item] += 1
        else:
            odic[item] = 1

elist = sorted(edic.items(), key=lambda x: -x[1])
olist = sorted(odic.items(), key=lambda x: -x[1])

if elist[0][0] != olist[0][0]:
    print(len(vs) - elist[0][1] - olist[0][1])
else:
    ogain = egain = 0
    if len(elist) >= 2:
        egain = elist[1][1]
    if len(olist) >= 2:
        ogain = olist[1][1]
    print(min(len(vs) - elist[0][1] - ogain, len(vs) - olist[0][1] - egain ))

Submission status: Accepted

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

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

v_even = v[::2]
v_odd = v[1::2]

c_even = collections.Counter(v_even)
c_odd = collections.Counter(v_odd)

most_even = c_even.most_common(2)
most_odd = c_odd.most_common(2)

if most_even[0][0] != most_odd[0][0]:
    print(n - most_even[0][1] - most_odd[0][1])
elif len(most_even) == len(most_odd) == 1:
    print(int(n/2))
else:
    if most_even[0][1] > most_odd[0][1]:
        print(n - most_even[0][1] - most_odd[1][1])
    elif most_even[0][1] < most_odd[0][1]:
        print(n - most_odd[0][1] - most_even[1][1])
    else:
        print(n - most_even[0][1] - max(most_even[1][1], most_odd[1][1]))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = input().split()

v1 = v[::2]
v2 = v[1::2]
c1 = Counter(v1).most_common(2)
c2 = Counter(v2).most_common(2)

if len(set(v)) == 1:
    print(len(v) // 2)
elif 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:
#!/usr/bin/env python
import collections


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

odd_c = collections.Counter(odd).most_common()
even_c = collections.Counter(even).most_common()

if odd_c[0][0] != even_c[0][0]:
    ans = len(v) - odd_c[0][1] - even_c[0][1]
elif len(odd_c) == 1 and len(even_c) == 1:
    ans = odd_c[0][1]
elif len(odd_c) == 1:
    ans = len(v)//2 - even_c[1][1]
elif len(even_c) == 1:
    ans = len(v)//2 - odd_c[1][1]
else:
    e = len(v) - odd_c[0][1] - even_c[1][1]
    o = len(v) - even_c[0][1] - odd_c[1][1]
    ans = e if e < o else o
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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


dict_odd = {}
dict_even = {}

for i in range(n):
  #リストの奇数番目、０始まりなのであまりは０
  if i %2 == 0:
    dict_odd[a[i]] = dict_odd.get(a[i], 0) + 1
  else:
    dict_even[a[i]] = dict_even.get(a[i], 0) + 1
    
#print(dict_odd, dict_even)    

v_odd_sorted = sorted(dict_odd.values())
v_even_sorted = sorted(dict_even.values())

odd_sorted = sorted(dict_odd.items(), key=lambda x : x[1])
even_sorted = sorted(dict_even.items(), key=lambda x : x[1])

#print(v_odd_sorted, v_even_sorted)    
ans = 0
#数字が一種類しかない場合は、一方を置き換える必要がある
if len(set(a)) == 1:
  ans = sum(v_odd_sorted)
  #print(sum(v_odd_sorted))
else:
  #偶数列と奇数列について、書き換えた後が同じ数字なら成立しないので、一方の書き換える先を次に多い数字にする
  if odd_sorted[-1][0] == even_sorted[-1][0]:
    comp1 = ans = sum(v_odd_sorted[:-1]) + sum(v_even_sorted[:-2]) + v_even_sorted[-1]
    comp2 = ans = sum(v_even_sorted[:-1]) + sum(v_odd_sorted[:-2]) + v_odd_sorted[-1]
    ans = min([comp1, comp2])
    """
    if odd_sorted[-1][1] >= even_sorted[-1][1]:
      #偶数列を変更したほうが少ない場合
      ans = sum(v_odd_sorted[:-1]) + sum(v_even_sorted[:-2]) + v_even_sorted[-1]
    else:
      #奇数列を変更したほうが少ない場合
      ans = sum(v_even_sorted[:-1]) + sum(v_odd_sorted[:-2]) + v_odd_sorted[-1]
      """
  else:
    ans = sum(v_odd_sorted[:-1]) + sum(v_even_sorted[:-1])
print(ans)

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

n = int(input())
a = list(map(int, input().split()))
even = defaultdict(int)
odd = defaultdict(int)
for i in range(n):
    if i % 2 == 0:
        even[a[i]] += 1
    else:
        odd[a[i]] += 1
x = sorted(even.items(), reverse=True, key=lambda t:t[1])
y = sorted(odd.items(), reverse=True, key=lambda t:t[1])
even_index = 0
odd_index = 0
if x[0][0] == y[0][0]:
    if x[0][1] > y[0][1]:
        even_index = 0
        odd_index = 1
    elif x[0][1] < y[0][1]:
        odd_index = 0
        even_index = 1
    else:
        if len(x) > 1:
            print(n - x[0][1] - max(x[1][1], y[1][1]))
        else:
            print(n // 2)
        exit()
            
else:
    even_index = 0
    odd_index = 0
print(n - x[even_index][1] - y[odd_index][1])

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int, input().split()))
c_odd  = collections.Counter(v[::2])
c_even = collections.Counter(v[1::2])
odd    = c_odd.most_common()[:2]
even   = c_even.most_common()[:2]
if (len(odd) == 1) & (len(even) == 1):
	if odd[0][0] == even[0][0]:
		ans = odd[0][1]
	else:
		ans = 0
elif len(odd) == 1:
	if odd[0][0] == even[0][0]:
		ans = n - odd[0][1] - even[1][1]
	else:
		ans = n - odd[0][1] - even[0][1]
elif len(even) == 1:
	if odd[0][0] == even[0][0]:
		ans = n - odd[1][1] - even[0][1]
	else:
		ans = n - odd[0][1] - even[0][1]
else:
	if odd[0][0] == even[0][0]:
		ans = min(n - odd[0][1] - even[1][1], n - odd[1][1] - even[0][1])
	else:
		ans = n - odd[0][1] - even[0][1]
print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
V=[int(i) for i in input().split()]
if len(set(V))==1:
  print(n//2)
else:
  O,E=[],[]
  for i in range(n//2):
    O.append(V[2*i])
    E.append(V[2*i+1])
  cO=collections.Counter(O).most_common()
  cE=collections.Counter(E).most_common()
  if cO[0][0]!=cE[0][0]:
    print(n-cO[0][1]-cE[0][1])
  else:
    print(min(n-cO[0][1]-cE[1][1],n-cO[1][1]-cE[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int,input().split()))
 
c1 = Counter(v[::2])
c2 = Counter(v[1::2])
m1 = c1.most_common()
m2 = c2.most_common()
t2 = n//2
t1 = n - t2
 
if len(m1) == len(m2) == 1:
    print(n//2 if m1[0][0] == m2[0][0] else 0)
elif len(m1) == 1:
    if m1[0][0] != m2[0][0]:
        print(t1 - m2[0][1])
    else:
        print(t1 - m2[1][1])
elif len(m2) == 1:
    if m1[0][0] != m2[0][0]:
        print(t2 - m1[0][1])
    else:
        print(t2 - m1[1][1])
else:
    if m1[0][0] != m2[0][0]:
        print(n - m1[0][1] - m2[0][1])
    else:
        ans = n - m1[1][1] - m2[1][1]
        if m1[0][0] != m2[1][0]:
            ans = min(ans, n - m1[0][1] - m2[1][1])
        if m1[1][0] != m2[0][0]:
            ans = min(ans, n - m1[1][1] - m2[0][1])
        print(ans)

Submission status: Accepted

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

import numpy as np
odd  =np.array(v)[1::2]
even =np.array(v)[0::2]
odd_count = np.bincount(odd)
odd_mode = np.argmax(odd_count)
even_count = np.bincount(even)
even_mode = np.argmax(even_count)

ans = 0
if odd_mode!=even_mode:
    ans += len(odd) - odd_count.max()
    ans += len(even) - even_count.max()
    print(ans)
else:
    odd_max = odd_count.max()
    odd_result=list(odd_count)
    odd_result.remove(odd_max)
    
    even_max = even_count.max()
    even_result=list(even_count)
    even_result.remove(even_max)
    
    ans1 = len(odd) - odd_count.max()
    ans1 += len(even) - np.array(even_result).max()
    
    ans2 = len(even) - even_count.max()
    ans2 += len(odd) - np.array(odd_result).max()
    print(min(ans1,ans2))

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10000)
INF = float('inf')

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

v1 = Counter(V[::2]).most_common(2)
v2 = Counter(V[1::2]).most_common(2)


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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from math import *
from itertools import *
from collections import *
from functools import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(n: int, v: "List[int]"):
    e = v[0::2]
    o = v[1::2]

    ec = sorted([(vv, k) for k, vv in Counter(e).items()], reverse=True) + [(0, None)]
    oc = sorted([(vv, k) for k, vv in Counter(o).items()], reverse=True) + [(0, None)]

    ee1 = ec[0]
    oo1 = oc[1] if oc[0][1] == ee1[1] else oc[0]
    ret1 = len(v) - ee1[0] - oo1[0]

    oo2 = oc[0]
    ee2 = ec[1] if ec[0][1] == oo2[1] else ec[0]
    ret2 = len(v) - ee2[0] - oo2[0]
   
    return min(ret1, ret2)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    v = [ int(next(tokens)) for _ in range(n) ]  # type: "List[int]"
    result = solve(n, v)
    print(result)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
odd_dic = {}
even_dic = {}
for a in range(n):
    b = v[a]
    if a % 2 == 0:
        dic = odd_dic
    else:
        dic = even_dic
    if b in dic:
        dic[b] += 1
    else:
        dic[b] = 1
answer = n
odd_dic = sorted(odd_dic.items(), key=lambda x:x[1])
even_dic = sorted(even_dic.items(), key=lambda x:x[1])
if odd_dic[-1][0] == even_dic[-1][0]:
    answer -= odd_dic[-1][1]
    tmp = 0
    if len(even_dic) > 1:
        tmp = even_dic[-2][1]
    if len(odd_dic) > 1:
        tmp = max(odd_dic[-2][1], tmp)
    answer -= tmp
else:
    answer -= odd_dic[-1][1]
    answer -= even_dic[-1][1]
print(answer)

Submission status: Accepted

Solution code:
from collections import Counter
_ = input().strip()
V = list(map(int, input().strip().split(' ')))
odd_c = Counter(V[0::2]).most_common()
even_c = Counter(V[1::2]).most_common()
if odd_c[0][0] != even_c[0][0]:
  print(len(V)-odd_c[0][1]-even_c[0][1])
else:
  if len(odd_c) == 1 and len(even_c) == 1:
    print(len(V)//2)
  elif len(odd_c) == 1 and len(even_c) != 1:
    print(len(V)//2-even_c[1][1])
  elif len(odd_c) != 1 and len(even_c) == 1:
    print(len(V)//2-odd_c[1][1])
  else:
    print(len(V)-max(odd_c[0][1] + even_c[1][1], odd_c[1][1] + even_c[0][1]))

Submission status: Accepted

Solution code:
import collections

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

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

if len(collections.Counter(v))==1:
    max_eq = max(len(odd),len(even))
    print(n - max_eq)
    exit()

odd_most = collections.Counter(odd).most_common(2)
even_most = collections.Counter(even).most_common(2)

max_count=0
for odd_top in odd_most:
    for even_top in even_most:
        if odd_top[0] == even_top[0]:
            continue
        max_count = max(max_count,odd_top[1]+even_top[1])
print(n-max_count)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
from collections import Counter

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

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

even_c = Counter(even)
odd_c = Counter(odd)
even_list = even_c.most_common()
odd_list = odd_c.most_common()

if even_list[0][0] != odd_list[0][0]:
    print(n - even_list[0][1] - odd_list[0][1])
else:
    if len(even_list) > 1 and len(odd_list) > 1:
        print(min(n - even_list[0][1] - odd_list[1][1], n - even_list[1][1] - odd_list[0][1]))
    elif len(even_list) == 1 and len(odd_list) == 1:
        print(int(n / 2))
    elif len(even_list) == 1:
        print(n - even_list[0][1] - odd_list[1][1])
    else:
        print(n - even_list[1][1] - odd_list[0][1])

Submission status: Accepted

Solution code:
from collections import Counter

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

a1 = list(Counter(V[::2]).items())
a2 = list(Counter(V[1::2]).items())

a1.sort(key=lambda a: a[1], reverse=True)
a2.sort(key=lambda a: a[1], reverse=True)

if a1[0][0] != a2[0][0]:
    print(N - a1[0][1] - a2[0][1])
else:
    if len(a1) == 1 and len(a2) == 1:
        print(N // 2)
    elif len(a1) == 1:
        print(N - a2[1][1])
    elif len(a2) == 1:
        print(N - a1[1][1])
    else:
        print(min(N - a1[1][1] - a2[0][1], N - a1[0][1] - a2[1][1]))

Submission status: Accepted

Solution code:
import math
N = int( input())
temp = list(map(int,input().split(" ")))
ans = 10**20
odd =[v for i,v in enumerate(temp) if i % 2 == 1]
even =[v for i,v in enumerate(temp) if i % 2 == 0]
dicodd = {}
diceven = {}
for i in temp:
    if i not in dicodd:
        dicodd[i] = 0
    if i not in diceven:
        diceven[i] = 0
for o in odd:
    dicodd[o] += 1
for e in even:
    diceven[e] += 1


maxOdd = max(dicodd.items(),key= lambda x:x[1])
dicodd.pop(maxOdd[0])

max2Odd = (0,0)
if len(dicodd) != 0:
    max2Odd = max(dicodd.items(),key= lambda x:x[1])

maxEven = max(diceven.items(),key= lambda x:x[1])
diceven.pop(maxEven[0])

max2Even = (0,0)
if len(diceven) != 0:
    max2Even = max(diceven.items(),key= lambda x:x[1])

if maxOdd[0] != maxEven[0]:
    print(N - maxOdd[1] - maxEven[1])
else:
    ans1 = N - maxOdd[1] - max2Even[1]
    ans2 = N - max2Odd[1] - maxEven[1]
    print(min(ans1,ans2))

Submission status: Accepted

Solution code:
import collections

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

v_2=[]
v_1=[]
for i,t in enumerate(v):
  if i%2==0:
    v_2.append(t)
  else:
    v_1.append(t)

v_2=collections.Counter(v_2)
v_1=collections.Counter(v_1)

if v_2.most_common()[0][0] != v_1.most_common()[0][0]:
    print(len(v)-v_2.most_common()[0][1]-v_1.most_common()[0][1])
else:
    if v_2.most_common()[0][1] > v_1.most_common()[0][1]:
      print(len(v)-v_2.most_common()[0][1]-v_1.most_common()[1][1])
    elif v_2.most_common()[0][1] < v_1.most_common()[0][1]:
      print(len(v)-v_2.most_common()[1][1]-v_1.most_common()[0][1])
    else:
        if v_2.most_common()[0][1]==len(v)/2:
            print(int(len(v)/2))
        else:
            if v_2.most_common()[1][1]>=v_1.most_common()[1][1]:
                print(len(v)-v_2.most_common()[1][1]-v_1.most_common()[0][1])
            else:
                print(len(v)-v_2.most_common()[0][1]-v_1.most_common()[1][1])

Submission status: Accepted

Solution code:
import collections

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

odd = []
even = []
for i in range(n_half):
  odd.append(v[i*2])
  even.append(v[i*2+1])

oc = collections.Counter(odd)
ec = collections.Counter(even)
ocm = oc.most_common()
ecm = ec.most_common()
if ocm[0][0] != ecm[0][0]:
  print(n - ocm[0][1] - ecm[0][1])
else:
  if ocm[0][1] == n_half and ecm[0][1] == n_half:
    print(n_half)
  elif ocm[0][1] == n_half:
    print(n_half - ecm[1][1])
  elif ecm[0][1] == n_half:
    print(n_half - ocm[1][1])
  else:
    print(min(n - ocm[0][1] - ecm[1][1], n - ocm[1][1] - ecm[0][1]))

Submission status: Accepted

Solution code:

from collections import defaultdict

def calc(data):
    count = defaultdict(int)
    for num in data:
        count[num] += 1

    count_data = []
    for i, j in count.items():
        count_data.append([i, j])

    count_data.sort(key=lambda x: -x[1])
    return count_data


def main():
    num = int(input())
    data = list(map(int, input().split()))

    data_e = data[::2]
    data_o = data[1::2]

    count_e = calc(data_e)
    count_o = calc(data_o)

    if count_e[0][0] != count_o[0][0]:
        ans = num - count_e[0][1] - count_o[0][1]
    elif len(count_e) == 1 and len(count_o) == 1:
        ans = num - count_e[0][1]
    elif len(count_e) == 1:
        ans1 = num - count_o[0][1]
        ans2 = num - count_e[0][1] - count_o[1][1]
        ans = min(ans1, ans2)
    elif len(count_o) == 1:
        ans1 = num - count_e[0][1]
        ans2 = num - count_o[0][1] - count_e[1][1]
        ans = min(ans1, ans2)

    else:
        ans1 = num - count_e[0][1] - count_o[1][1]
        ans2 = num - count_e[1][1] - count_o[0][1]
        ans = min(ans1, ans2)

    print(ans)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(n: int, v: "List[int]"):
    counts = [{}, {}]
    for i in range(n):
        if v[i] in counts[i % 2]:
            counts[i % 2][v[i]] += 1
        else:
            counts[i % 2][v[i]] = 1
    l1 = list(counts[0].items()) + [(-1, 0)]
    l2 = list(counts[1].items()) + [(-1, 0)]
    l1.sort(reverse=True, key=lambda x: x[1])
    l2.sort(reverse=True, key=lambda x: x[1])
    #print(l1)
    #print(l2)
    tmp = 0
    if l1[0][0] == l2[0][0]:
        tmp = max(l1[0][1] + l2[1][1], l1[1][1] + l2[0][1])
    else:
        tmp = l1[0][1] + l2[0][1]
    ret = n - tmp
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    v = [ int(next(tokens)) for _ in range(n) ]  # type: "List[int]"
    solve(n, v)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections , sys



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


c2 = collections.Counter(v[::2])
c1 = collections.Counter(v[1::2])

if len(list(set(v))) == 1:
    print(n//2)
    sys.exit()

ceven = c2.most_common()[0][:]
codd = c1.most_common()[0][:]

if ceven[0] == codd[0]:
    if ceven[1] > codd[1]:
        codd = c1.most_common()[1][:]
    elif ceven[1] < codd[1]:
        ceven = c2.most_common()[1][:]
    else:
        ceven2 = c2.most_common()[1][:]
        codd2 = c1.most_common()[1][:]
        if  ceven2[1] > codd2[1]:
            ceven = ceven2
        else :
            codd = codd2
            
ans = n - codd[1] - ceven[1]

print(ans)