Problem:


Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.



Sample input:
4
3 1 3 2


Sample output:
1

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
v0=[]
v1=[]
m=int(n/2)
for i in range(m):
    v0.append(v[i*2])
    v1.append(v[i*2+1])
v0.sort()
v1.sort()
mv0=[0,0]
mv1=[0,0]
jmv0=[0,0]
jmv1=[0,0]
c0=[0,0]
c1=[0,0]
for i in range(m):
    if c0[0]==v0[i]:
        c0[1]+=1
    else:
        if mv0[1]<=c0[1]:
            jmv0=mv0
            mv0=c0
        elif jmv0[1]<=c0[1]:
            jmv0=c0
        c0=[v0[i],1]

    if c1[0]==v1[i]:
        c1[1]+=1
    else:
        if mv1[1]<=c1[1]:
            jmv1=mv1
            mv1=c1
        elif jmv1[1]<=c1[1]:
            jmv1=c1
        c1=[v1[i],1]

if mv0[1]<=c0[1]:
            jmv0=mv0
            mv0=c0
elif jmv0[1]<=c0[1]:
            jmv0=c0

if mv1[1]<=c1[1]:
            jmv1=mv1
            mv1=c1
elif jmv1[1]<=c1[1]:
            jmv1=c1

if mv0[0]!=mv1[0]:
    print(n-mv0[1]-mv1[1])
else:
    print(min(n-mv0[1]-jmv1[1],n-jmv0[1]-mv1[1]))

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
V = list(map(int, input().split()))
M = 10 ** 5 + 1
V0 = defaultdict(int)
V1 = defaultdict(int)
ma01, ma02 = 0, 0
ma11, ma12 = 0, 0
for i in range(N):
    if i % 2 == 0:
        V0[V[i]] += 1
    else:
        V1[V[i]] += 1

rec0 = []
rec1 = []
for i, k in V0.items():
    rec0.append((i, k))

for i, k in V1.items():
    rec1.append((i, k))

rec0 = sorted(rec0, key=lambda x: x[1], reverse=True)
rec1 = sorted(rec1, key=lambda x: x[1], reverse=True)

if rec0[0][0] != rec1[0][0]:
    print(N - rec0[0][1] - rec1[0][1])
else:
    if len(rec0) == 1 and len(rec1) == 1:
        print(N // 2)
    elif len(rec0) == 1:
        print(N // 2 - rec1[0][1])
    elif len(rec1) == 1:
        print(N // 2 - rec0[0][1])
    else:
        s = rec0[1][1]
        t = rec1[1][1]
        num = min(N - rec0[1][1] - rec1[0][1], N - rec0[0][1] - rec1[1][1])
        print(num)

Submission status: Accepted

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

from collections import Counter


def answer():
    n_2 = int(input())
    assert n_2 % 2 == 0
    n = n_2 // 2
    tmp = input()
    values = [int(w) for w in tmp.split()]

    GATE_KEEPER = -1

    values_odd = values[::2]
    values_even = values[1::2]

    counts = [Counter(values_partial).most_common(2) for values_partial in [values_odd, values_even]]
    if counts[0][0][0] != counts[1][0][0]:
        # just go
        ans = sum([n - count[0][1] for count in counts])
    else:
        # replace
        # set gatekeeper
        for i in range(2):
            counts[i].append((GATE_KEEPER, 0))
        ans = min([(n - counts[0][0][1]) + (n - counts[1][1][1]),
                   (n - counts[0][1][1]) + (n - counts[1][0][1])])
    print(ans)


if __name__ == '__main__':
    answer()

Submission status: Accepted

Solution code:
from collections import Counter


def check(A):
    N = len(A)
    even = Counter()
    odd = Counter()
    for i, a in enumerate(A):
        if i % 2 == 0:
            odd[a] += 1
        else:
            even[a] += 1
    
    even = even.most_common() + [(None, 0)]
    odd = odd.most_common() + [(None, 0)]
    
    if even[0][0] != odd[0][0]:
        return N - even[0][1] - odd[0][1]
    else:
        return min(N - even[0][1] - odd[1][1], N - odd[0][1] - even[1][1])

N = int(raw_input())
A = map(int, raw_input().split())

ans = check(A)
print ans

Submission status: Accepted

Solution code:
from collections import Counter

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

odds = []
evens = []

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

evens_common = Counter(evens).most_common()
odds_common = Counter(odds).most_common()

if evens_common[0][0] != odds_common[0][0]:
  evens_most_count = Counter(evens).most_common()[0][1]
  odds_most_count = Counter(odds).most_common()[0][1]
  print(n - evens_most_count - odds_most_count)
else:
  if len(evens_common) == 1:
    if len(odds_common) == 1:
      print(n//2)
    else:
      print(n - odds_common[1][1])
  else:
    if len(odds_common) == 1:
      print(n - evens_common[1][1])
    else:
      res = min(n - evens_common[1][1] - odds_common[0][1], n - evens_common[0][1] - odds_common[1][1])
      print(res)

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])
s1 = sorted(c1.items(), key=lambda x:x[1], reverse=True)
s2 = sorted(c2.items(), key=lambda x:x[1], reverse=True)

if len(s1) == 1 and len(s2) == 1:
    if s1[0][0] == s2[0][0]:
        ans = int(n / 2)
    else:
        ans = 0
else:
    if s1[0][0] != s2[0][0]:
        ans = n - s1[0][1] - s2[0][1]
    else:
        if s1[0][1] > s2[0][1]:
            ans = n - s1[0][1] - s2[1][1]
        elif s1[0][1] < s2[0][1]:
            ans = n - s1[1][1] - s2[0][1]
        else:
            ans = min(n - s1[0][1] - s2[1][1], n - s1[1][1] - s2[0][1])
print(ans)

Submission status: Accepted

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

if len(odd) == 1 and len(even) == 1:
    if odd == even:
        change = int(n / 2)
else:
    max_e = max(even, key=even.get)
    max_o = max(odd, key=odd.get)
    if max_e != max_o:
        change = n - even[max_e] - odd[max_o]
    else:
        if even[max_e] > even[max_o]:
            max_o2 = min(odd, key=odd.get)
            for o in odd:
                if o != max_o and odd[o] > odd[max_o2]:
                    max_o2 = o
            change = n - even[max_e] - odd[max_o2]
        elif even[max_e] == even[max_o]:
            max_o2 = min(odd, key=odd.get)
            for o in odd:
                if o != max_o and odd[o] > odd[max_o2]:
                    max_o2 = o
            max_e2 = min(even, key=even.get)
            for e in even:
                if e != max_e and even[e] > even[max_e2]:
                    max_e2 = e
            change = min(n - even[max_e] - odd[max_o2], n - odd[max_o] - even[max_e2])
        else:
            max_e2 = min(even, key=even.get)
            for e in even:
                if e != max_e and even[e] > even[max_e2]:
                    max_e2 = e
            change = n - odd[max_o] - even[max_e2]
print(change)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import Counter
N = input()
# t = np.array(map(int, raw_input().split()))
t = np.array(list(map(int, input().split())))

odd = t[0::2]
even = t[1::2]
rest = 0

co = Counter(odd)
ce = Counter(even)

omc = co.most_common()
emc = ce.most_common()

if omc[0][0] == emc[0][0]:
    if len(omc) < 2 and len(emc) < 2:       # same
        rest = len(even)
    elif len(omc) < 2:
        rest = len(even)
    elif len(emc) < 2:
        rest = len(odd)
    else:
        or1 = len(odd) - co.most_common()[0][1]
        or2 = len(odd) - co.most_common()[1][1]
        er1 = len(even) - ce.most_common()[0][1]
        er2 = len(even) - ce.most_common()[1][1]

        if or1 + er2 > or2 + er1:
            rest = or2 + er1
        else:
            rest = or1 + er2
else:
    rest = len(odd) - co.most_common()[0][1] + len(even) - ce.most_common()[0][1]


print(rest)


    # counter.most_common()[0][0]
    # uni = list(set(_k))
    # cnt = np.zeros((len(uni), ))
    # for _i in uni:
    #     cnt[uni.index(_i)] += 1
    # unique = np.unique(odd, return_counts=True)
    # rest += len(_k) - count[np.argmax(count)]

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import Counter
N = input()
t = np.array(map(int, raw_input().split()))

odd = t[0::2]
even = t[1::2]
rest = 0

co = Counter(odd)
ce = Counter(even)

omc = co.most_common()
emc = ce.most_common()

if omc[0][0] == emc[0][0]:
    if len(omc) < 2 and len(emc) < 2:       # same
        rest = len(even)
    elif len(omc) < 2:
        rest = len(even)
    elif len(emc) < 2:
        rest = len(odd)
    else:
        or1 = len(odd) - co.most_common()[0][1]
        or2 = len(odd) - co.most_common()[1][1]
        er1 = len(even) - ce.most_common()[0][1]
        er2 = len(even) - ce.most_common()[1][1]

        if or1 + er2 > or2 + er1:
            rest = or2 + er1
        else:
            rest = or1 + er2
else:
    rest = len(odd) - co.most_common()[0][1] + len(even) - ce.most_common()[0][1]


print rest

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
vvv = list(map(int, input().split()))
cnt1 = Counter(vvv[0::2])
cnt2 = Counter(vvv[1::2])
mc1 = cnt1.most_common(2)
mc2 = cnt2.most_common(2)
if mc1[0][0] == mc2[0][0]:
    sc1 = mc1[1][1] if len(mc1) > 1 else 0
    sc2 = mc2[1][1] if len(mc2) > 1 else 0
    print(min(n - mc1[0][1] - sc2, n - sc1 - mc2[0][1]))
else:
    print(n - mc1[0][1] - mc2[0][1])

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
d1 = {}
d2 = {}

for i in range(0, n, 2):
    if v[i] in d1.keys():
        d1[v[i]] += 1
    else:
        d1[v[i]] = 1
for i in range(1, n, 2):
    if v[i] in d2.keys():
        d2[v[i]] += 1
    else:
        d2[v[i]] = 1


def keys_values(d):
    fk, fv, sk, sv = 0, 0, 0, 0
    for k in d:
        if d[k] > fv:
            sk, sv = fk, fv
            fk, fv = k, d[k]
        elif d[k] > sv:
            sk, sv = k, d[k]
    return fk, fv, sk, sv


ak, av, bk, bv = keys_values(d1)
ck, cv, dk, dv = keys_values(d2)
if ak != ck:
    print(n-av-cv)
else:
    print(min((n-av-dv), n-bv-cv))

Submission status: Accepted

Solution code:
from collections import Counter
c = [Counter(), Counter()]
n = input()
a = map(int, raw_input().split())
for i in range(n):
	c[i & 1][a[i]] += 1
p = [(0, 0)]
q = [(0, 0)]
for i in c[0]:
	p.append((c[0][i], i))
for i in c[1]:
	q.append((c[1][i], i))
p.sort()
q.sort()
if p[-1][1] == q[-1][1]:
	print n - max(p[-1][0] + q[-2][0], p[-2][0] + q[-1][0])
else:
	print n - p[-1][0] - q[-1][0]

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])
s1 = sorted(c1.items(), key=lambda x: x[1], reverse=True)
s2 = sorted(c2.items(), key=lambda x: x[1], reverse=True)
if s1[0][0] != s2[0][0]:
    print(n - s1[0][1] - s2[0][1])
else:
    if len(s1) == 1 and len(s1) == 1:
        ans = min(n - s1[0][1], n - s2[0][1])
    elif len(s1) == 1:
        ans = n - s1[0][1] - s2[1][1]
    elif len(s2) == 1:
        ans = n - s1[1][1] - s2[0][1]
    else:
        ans = min(n - s1[0][1] - s2[1][1], n - s1[1][1] - s2[0][1])
    print(ans)

Submission status: Accepted

Solution code:
maxn=10**5+10
n=int(input())
b=[0]*maxn
c=[0]*maxn
a=input().split()

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

max1=max(b)
maxp=b.index(max1)
del b[maxp]
max2=max(b)

ans=0
for i in range(0,maxn):
    ans=max(ans,c[i]+(max2 if i==maxp else max1))

print(n-ans)

Submission status: Accepted

Solution code:
maxn=10**5+10
n=int(input())
a=[None]*(n+10)
b=[0]*maxn
c=[0]*maxn
a[1:n+1]=input().split()

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

max1=max2=maxp=0
for i in range(1,maxn):
    if b[i]>=max1:
        max2=max1
        maxp=i
        max1=b[i]
    elif b[i]>=max2:
        max2=b[i]

ans=n
for i in range(1,maxn):
    res=((n+1)//2-c[i])
    if i==maxp:
        res+=n//2-max2
    else:
        res+=n//2-max1
    ans=min(ans,res)

print(ans)

Submission status: Accepted

Solution code:
import collections

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

a_odd=list([int(a[i]) for i in range(n) if i%2==1])
a_even=list([int(a[i]) for i in range(n) if i%2==0])

ca_odd=collections.Counter(a_odd)
ca_even=collections.Counter(a_even)

def count_change(a_sublist, i):
    return len([num for num in a_sublist if num!=i])

loop_set_a_odd=set ([i[0] for i in ca_odd.most_common()[0:min(len(ca_odd),2)]])
loop_set_a_even=set( [ i[0] for i in ca_even.most_common()[0:min(len(ca_even),2)]])

temp=n
for i in loop_set_a_odd:
    for j in loop_set_a_even:
        if i!=j:
            c=count_change(a_odd,i)+count_change(a_even,j)
            if temp>c:
                temp=c

if temp==n:
    print(len(a_odd))
else:
    print(temp)

Submission status: Accepted

Solution code:
iipt=lambda:int(input())
miipt=lambda:map(int,input().split(" "))
o=[0]*112345
e=[0]*112345
n=iipt()
A=miipt()
for i,a in enumerate(A):
    if i%2:
        o[a]+=1
    else:
        e[a]+=1
om=-1,0
om2=-1,0
em=-1,0
em2=-1,0
for i,v in enumerate(o):
    if v>=om[1]:
        om2=om
        om=i,v
    elif v>=om2[1]:
        om2=i,v
for i,v in enumerate(e):
    if v>=em[1]:
        em2=em
        em=i,v
    elif v>=em2[1]:
        em2=i,v
if om[0]==em[0]:
    print(min(n-om2[1]-em[1],n-om[1]-em2[1]))
else:
    print(n-om[1]-em[1])

Submission status: Accepted

Solution code:
import collections
import numpy as np

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

#n = 4
npv = np.array( v, dtype=np.int32)
#npv = np.array( [3,1,3,2], dtype=np.int32)
npv_dic = {}
npv_dic['even'] = npv[0::2]
npv_dic['odd']  = npv[1::2]

#print('n = ', n)
#print('npv = ', npv)
#print('npv_dic[\'even\'] = ', npv_dic['even'])
#print('npv_dic[\'odd\']  = ', npv_dic['odd'])

dict0 = collections.Counter(npv_dic['even'])
dict1 = collections.Counter(npv_dic['odd'])

list0 = list(dict0.items())
list1 = list(dict1.items())

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

#print(list0[0] ,list1[0])
#print(list0[1] ,list1[1])

if list0[0][0] == list1[0][0]:
    if (list0[0][1] == list1[0][1]) and \
        (list0.__len__() > 1) and (list0.__len__() > 1):
        if list0[1][1] > list1[1][1]:
            del list0[0]
        else:
            del list1[0]
    elif list0[0][1] > list1[0][1]:
        del list1[0]
    else:
        del list0[0]
#print(list0[0] ,list1[0])
#print(list0[1] ,list1[1])

if list0.__len__() != 0:
    change0 = n//2 - list0[0][1]
else:
    change0 = n//2
if list1.__len__() != 0:
    change1 = n//2 - list1[0][1]
else:
    change1 = n//2
#print('change:', change0, change1)

print(change0 + change1)

Submission status: Accepted

Solution code:
import collections
import numpy as np

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

#n = 4
npv = np.array( v, dtype=np.int32)
#npv = np.array( [3,1,3,2], dtype=np.int32)
npv_dic = {}
npv_dic['even'] = npv[0::2]
npv_dic['odd']  = npv[1::2]

#print('n = ', n)
#print('npv = ', npv)
#print('npv_dic[\'even\'] = ', npv_dic['even'])
#print('npv_dic[\'odd\']  = ', npv_dic['odd'])

dict0 = collections.Counter(npv_dic['even'])
dict1 = collections.Counter(npv_dic['odd'])

list0 = list(dict0.items())
list1 = list(dict1.items())

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

#print(list0[0] ,list1[0])
#print(list0[1] ,list1[1])

if list0[0][0] == list1[0][0]:
    if (list0[0][1] == list1[0][1]) and \
        (list0.__len__() > 1) and (list0.__len__() > 1):
        if list0[1][1] > list1[1][1]:
            del list0[0]
        else:
            del list1[0]
    elif list0[0][1] > list1[0][1]:
        del list1[0]
    else:
        del list0[0]
#print(list0[0] ,list1[0])
#print(list0[1] ,list1[1])

if list0.__len__() != 0:
    change0 = n//2 - list0[0][1]
else:
    change0 = n//2
change1 = n//2 - list1[0][1]
#print('change:', change0, change1)

print(change0 + change1)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
N=int(input())
v=[int(i) for i in input().split()]
A=[]
B=[]
for i in range(N):
    if i%2==0:
        A.append(v[i])
    else:
        B.append(v[i])
S=Counter(A).most_common(2)+[(0,0)]
T=Counter(B).most_common(2)+[(0,0)]
if S[0][0]!=T[0][0]:
    print(N-S[0][1]-T[0][1])
    sys.exit()
a=S[0][1]+T[1][1]
b=S[1][1]+T[0][1]
c=max(a,b)
print(N-c)

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy3


def get_line():
    return input()


def get_tokens():
    return get_line().split()


def get_ints():
    return [int(_) for _ in get_tokens()]


from collections import defaultdict


def main():
    n, = get_ints()
    v = get_ints()

    tot = [0, 0]
    cnt = [defaultdict(int), defaultdict(int)]

    for i in range(n):
        tot[i % 2] += 1
        cnt[i % 2][v[i]] += 1

    p = [[], []]
    for r in [0, 1]:
        p[r] = [(v, k) for k, v in cnt[r].items()] + [(0, -1 - r)]
        p[r].sort()
        p[r] = p[r][-2:]

    ans = n

    for x in p[0]:
        for y in p[1]:
            if x[1] != y[1]:
                ans = min(ans, n - x[0] - y[0])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    n = int(input())
    v = [int(i) for i in input().split()]
    ans = 0
    odd = Counter(v[1::2])
    even = Counter(v[::2])
    odd_mosts = odd.most_common(2)
    even_mosts = even.most_common(2)
    if odd_mosts[0][0] == even_mosts[0][0]:
        if len(odd) == len(even) == 1:
            ans = n//2
        else:
            ans_sel = []
            ans_sel.append(n//2-odd_mosts[0][1]+n//2-even_mosts[1][1])
            ans_sel.append(n//2-odd_mosts[1][1]+n//2-even_mosts[0][1])
            ans = min(ans_sel)
    else:
        ans+=(n//2-odd_mosts[0][1])
        ans+=(n//2-even_mosts[0][1])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

v_odd = []
v_even = []

for i in range(n//2):
    v_odd.append(v[i*2])
    v_even.append(v[i*2+1])

d_odd = defaultdict(int)
d_even = defaultdict(int)

for x in v_odd:
    d_odd[x] += 1
for x in v_even:
    d_even[x] += 1

l_odd = list(d_odd.values())
l_even = list(d_even.values())
item_odd = list(d_odd)
item_even = list(d_even)

max_odd = max(l_odd)
max_even = max(l_even)
fin_max = max_odd + max_even
max_odd_l, max_even_l = [], []

for i,x in enumerate(l_odd):
    if x == max_odd:
        max_odd_l.append(i)

for i,x in enumerate(l_even):
    if x == max_even:
        max_even_l.append(i)

if len(max_odd_l) == 1 and len(max_even_l) == 1:
    if item_odd[max_odd_l[0]] == item_even[max_even_l[0]]:
        #compare both pattern
        l_odd.pop(max_odd_l[0])
        l_even.pop(max_even_l[0])
        second_odd, second_even = 0, 0
        if l_odd:
            second_odd = max(l_odd)
        else:
            second_odd = 0
        if l_even:
            second_even = max(l_even)
        else:
            second_even = 0
        fin_max = max(max_odd+second_even, max_even+second_odd)

print(n - fin_max)

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    r"""
    数列 a1,a2,...,anが以下の条件を満たすとき、 /\/\/\/ と呼ぶ

        各 i=1,2...,n-2 について、ai=ai+2
        数列に現れる数はちょうど 2種類偶数長の数列

    v1,v2,...,vnが与えられます
    要素をいくつか書き換えることでこの数列を /\/\/\/ にしたいです。
    書き換える要素の数は最小でいくつになるか求めてください。

    2 <= n <= 10^5, nは偶数
    1 <= vi <= 10^5, viは整数
    """
    n = int(input())
    *v, = map(int, input().split())

    ans = f(n, v)
    assert ans == simple(n, v)
    print(ans)


def simple(n, v):
    # most_commonを事前に使い短くする
    # 1種類しかないならば存在しない種類を0個で足して条件分岐を潰す
    c1 = Counter(v[::2]).most_common() + [(0, 0)]
    c2 = Counter(v[1::2]).most_common() + [(0, 0)]

    if c1[0][0] == c2[0][0]:
        n_no_change = max(c1[0][1] + c2[1][1],
                          c1[1][1] + c2[0][1])
        return n - n_no_change
    else:
        return n - c1[0][1] - c2[0][1]

def f(n, v):
    """
    3 1 3 2:
        3種類
        a1=a3, a2 != a4
    """
    ans = float("inf")
    v_idx_even = []
    v_idx_odd = []
    for i, x in enumerate(v):
        if i % 2 == 0:
            v_idx_even.append(x)
        else:
            v_idx_odd.append(x)

    ve = Counter(v_idx_even)
    vo = Counter(v_idx_odd)

    if len(ve) == 1 and len(vo) == 1:
        # 両方1種類
        ans = 0
        if ve.most_common()[0][0] == vo.most_common()[0][0]:
            ans = n // 2

    elif len(ve) == 1 or len(vo) == 1:
        # 片方1種類
        idx = 0
        if ve.most_common()[0][0] == vo.most_common()[0][0]:
            idx = 1
        if len(ve) == 1:
            ans = n // 2 - vo.most_common()[idx][1]
        else:
            ans = n // 2 - ve.most_common()[idx][1]

    else:
        # 両方2種類以上
        if ve.most_common()[0][0] != vo.most_common()[0][0]:
            ans = n - ve.most_common()[0][1] - vo.most_common()[0][1]
        else:
            ans = n - \
                max(ve.most_common()[0][1] + vo.most_common()[1][1],
                    ve.most_common()[1][1] + vo.most_common()[0][1])

    return ans


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
w=sorted(a for a in v[0::2])
x=sorted(a for a in v[1::2])
a=[[0,i]for i in range(10**5+1)]
b=[[0,i]for i in range(10**5+1)]
for t in w:
    a[t][0]+=1
for t in x:
    b[t][0]+=1
c=sorted(a)
d=sorted(b)
e=c[len(c)-1][0]
i=c[len(c)-1][1]
f=d[len(d)-1][0]
g=c[len(c)-1][0]
h=d[len(d)-1][0]
for t in range(len(c)):
    if i!=d[len(d)-1][1]:break
    e=c[len(c)-t-1][0]
    i=c[len(c)-t-1][1]
i=d[len(d)-1][1]
for t in range(len(d)):
    if i!=c[len(c)-1][1]:break
    g=d[len(d)-t-1][0]
    i=d[len(d)-t-1][1]
print(min(n-e-f,n-g-h))

Submission status: Accepted

Solution code:
n=int(input())
*v,=map(int,input().split())
a=[[0,i]for i in range(10**5+1)]
b=[[0,i]for i in range(10**5+1)]
for t,u in zip(sorted(v[::2]),sorted(v[1::2])):a[t][0]+=1;b[u][0]+=1
a.sort(reverse=1)
b.sort(reverse=1)
print(n-a[0][0]-[max(a[1][0],b[1][0]),b[0][0]][a[0][1]!=b[0][1]])

Submission status: Accepted

Solution code:
from collections import Counter
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()

# 数字が1つしかない場合に"2番目"をダミーで追加
c1.append((0,0))
c2.append((0,0))

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

Submission status: Accepted

Solution code:
from collections import Counter
from itertools import takewhile

n = int(input())
vvv = list(map(int, input().split()))
cnt1 = Counter(vvv[0::2])
cnt2 = Counter(vvv[1::2])
mc1 = cnt1.most_common()
mc2 = cnt2.most_common()
mcs1 = list(takewhile(lambda x: x[1] == mc1[0][1], mc1))
mcs2 = list(takewhile(lambda x: x[1] == mc2[0][1], mc2))
if len(mcs1) == len(mcs2) and mcs1[0][0] == mcs2[0][0]:
    if len(mc1) > 1:
        if len(mc2) > 1:
            print(min(n - mc1[0][1] - mc2[1][1], n - mc1[1][1] - mc2[0][1]))
        else:
            print(min(n - mc1[0][1], n - mc1[1][1] - mc2[0][1]))
    else:
        if len(mc2) > 1:
            print(min(n - mc1[0][1] - mc2[1][1], n - mc2[0][1]))
        else:
            print(min(n - mc1[0][1], n - mc2[0][1]))
else:
    print(n - mc1[0][1] - mc2[0][1])

Submission status: Accepted

Solution code:
from copy import *
n=int(input())
arr=list(map(int,input().split()))
arr1=[]
arr2=[]
for i in range(1,100001):
	arr1.append([0,i])
	arr2.append([0,i])

for i in range(0,n,2):
	arr1[arr[i]-1][0]+=1
#arr1.sort()
#print(arr1[-1])
for i in range(1,n,2):
	arr2[arr[i]-1][0]+=1
arr1.sort(reverse=True)
arr2.sort(reverse=True)
#print(arr1[-1],arr2[-1])
if(arr1[0][1]==arr2[0][1]):
	print(min(n-arr1[0][0]-arr2[1][0],n-arr1[1][0]-arr2[0][0]))	
else:
	print(n-arr1[0][0]-arr2[0][0])

Submission status: Accepted

Solution code:
def solv(arr):
    cnt = [[0, i] for i in range(100000+1)]
    for x in arr:
        cnt[x][0] += 1
    cnt.sort()
    cnt.reverse()
    return cnt


n = int(input())
v = [int(x) for x in input().split()]
even = []
odd = []
for i in range(len(v)):
    if i % 2 == 0:
        even.append(v[i])
    else:
        odd.append(v[i])
cnt1 = solv(even)
cnt2 = solv(odd)
ans = min(n-cnt1[0][0]-cnt2[1][0], n-cnt1[1][0]-cnt2[0][0]
          ) if cnt1[0][1] == cnt2[0][1] else n - cnt1[0][0] - cnt2[0][0]
print(ans)

Submission status: Accepted

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

cnt_arr = [dict() for _ in range(2)]

for i in range(n):
    cnt_arr[i % 2][v[i]] = cnt_arr[i % 2].get(v[i], 0) + 1

v_even = sorted(list(cnt_arr[0].items()), key=lambda x: x[1], reverse=True)[:2]  # [value, 出現数] * 2（上位2）
v_odd = sorted(list(cnt_arr[1].items()), key=lambda x: x[1], reverse=True)[:2]  # [value, 出現数] * 2（上位2）

keep = 0
flg = False
for even in v_even:  # [0]: 数値, [1]: 出現数
    for odd in v_odd:  # [0]: 数値, [1]: 出現数
        if even[0] != odd[0]:
            flg = True
            keep = max(keep, even[1] + odd[1])

ans = n - keep if flg else n // 2
print(ans)

Submission status: Accepted

Solution code:
#! /usr/bin/python
from collections import Counter
N = int(input())
V = list(map(int, input().split()))
#N = 4
#V = list(map(int, "1 2 3 2 2 2 2 2 1 1 2 3 4 5 2 12 1 1".split()))


def func(l, aa):
    counter = Counter(l)
    if counter[aa] == 0:
        c, count = counter.most_common()[0]
    else:
        c, count = counter.most_common()[1]
    #print(l, c, len(l) - count)
    return (c, len(l) - count)


if len(set(V)) == 1:
    print(N // 2)
else:
    c, count = func(V[::2], -1)
    c2, count2 = func(V[1::2], c)
    a = count + count2
    c2, count2 = func(V[1::2], -1)
    c, count = func(V[::2], c2)
    print(min(a, count + count2))

Submission status: Accepted

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

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

if odd.most_common()[0][0] != even.most_common()[0][0]:
    print(n - (odd.most_common()[0][1] + even.most_common()[0][1]))
else:
    if len(odd) >= 2 and len(even) >= 2:
        print(min(n - (odd.most_common()[0][1] + even.most_common()[1][1]), n - (odd.most_common()[1][1] + even.most_common()[0][1])))
    elif len(odd) >= 2:
        print(n - (odd.most_common()[1][1] + even.most_common()[0][1]))
    elif len(even) >= 2:
        print(n - (even.most_common()[1][1] + odd.most_common()[0][1]))
    else:
        print(n//2)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18

N = input()
v = map(int, raw_input().split())

cnt1 = [0] * 110000
cnt2 = [0] * 110000

for i in range(0, N, 2):
    cnt1[v[i]] += 1
    cnt2[v[i + 1]] += 1

v1, v2 = [], []
for i in range(110000):
    v1.append((cnt1[i], i))
    v2.append((cnt2[i], i))

v1.sort(reverse = True)
v2.sort(reverse = True)

if v1[0][1] != v2[0][1]:
    print N - v1[0][0] - v2[0][0]
else:
    print N - max(v1[1][0] + v2[0][0], v1[0][0] + v2[1][0])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
V = list(map(int, input().split()))
c1 = Counter(V[::2])
c2 = Counter(V[1::2])
c1[0] = 0
c2[0] = 0
(x, cx), (_, cxx) = c1.most_common(2)
(y, cy), (_, cyy) = c2.most_common(2)
if x == y:
    print(n - max(cx + cyy, cy + cxx))
else:
    print(n - cx - cy)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
c1 = Counter(A[::2])
c2 = Counter(A[1::2])
c1[0] = 0
c2[0] = 0
c1m = max(c1.items(), key=lambda x:x[1])
c2m = max(c2.items(), key=lambda x:x[1])
c1c2 = max(c for k,c in c2.items() if c1m[0]!=k)
c2c1 = max(c for k,c in c1.items() if c2m[0]!=k)
print(min(N - c1m[1] - c1c2, N - c2m[1] - c2c1))

Submission status: Accepted

Solution code:
from collections import Counter as C
n = int(input())
v = [int(a) for a in input().split()]

a=C(v[::2])
b=C(v[1::2])
a[0]=b[0]=0
a=a.most_common()
b=b.most_common()
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

def main():
    n = int(input())
    vn = list(map(int, input().split()))
    even = count(vn, n, True)
    odd = count(vn, n, False)
    e1k, e1v, e2k, e2v = largest(even)
    o1k, o1v, o2k, o2v = largest(odd)
    res = solve(e1k, e1v, e2k, e2v, o1k, o1v, o2k, o2v, n)
    print(res)

def solve(e1k, e1v, e2k, e2v, o1k, o1v, o2k, o2v, n):
    if e1k != o1k:
        return n - e1v - o1v
    return min(n - e1v - o2v, n - e2v - o1v)

def count(vn, n, is_even):
    di = {}
    st = 0 if is_even else 1
    for i in range(st, n, 2):
        v = vn[i]
        if v in di:
            di[v] += 1
        else:
            di[v] = 1
    return di

def largest(di):
    li_tu = []
    for k in di:
        tu = (k, di[k])
        li_tu.append(tu)
    li_tu = sorted(li_tu, reverse=True, key=lambda x: x[1])
    assert len(li_tu) >= 1
    lg_key = li_tu[0][0]
    lg_val = li_tu[0][1]
    if len(li_tu) == 1:
        sc_key = 0
        sc_val = 0
    else:
        sc_key = li_tu[1][0]
        sc_val = li_tu[1][1]
    return lg_key, lg_val, sc_key, sc_val

main()

Submission status: Accepted

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

eve = {}
odd = {}

for i in range(len(vs)):
    if i % 2 == 0:
        if vs[i] in eve:
            eve[vs[i]] += 1
        else:
            eve[vs[i]] = 1
    else:
        if vs[i] in odd:
            odd[vs[i]] += 1
        else:
            odd[vs[i]] = 1

es = sorted(eve.items(), key=lambda x: -x[1])
os = sorted(odd.items(), key=lambda x: -x[1])

if es[0][0] == os[0][0]:
    if es[0][1] > os[0][1]:
        del es[0]
        if len(os) > 1 :
            del os[1]
    elif es[0][1] < os[0][1]:
        del os[0]
        if len(es) > 1 :
            del es[1]
    else:
        if len(es) == 1 and len(os) == 1:
            del os[0]
        elif len(es) == 1 and len(os) != 1:
            del es[0]
            del os[1]
        elif len(es) != 1 and len(os) == 1:
            del es[1]
            del os[0]
        else:
            if es[1][1] < os[1][1]:
                del es[0]
                del os[1]
            else:
                del es[1]
                del os[0]
else:
    del os[0]
    del es[0]

trans = 0

for i in range(len(es)):
    trans += es[i][1]
for i in range(len(os)):
    trans += os[i][1]

print(trans)