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 sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import Counter

n = ni()
a = list(li())

even = Counter(a[::2])
odd = Counter(a[1::2])

es = even.most_common(2)
os = odd.most_common(2)

es = es + [(0,0)]
os = os + [(0,0)]

if es[0][0] != os[0][0]:
    print((n//2-es[0][1]) + (n//2-os[0][1]))
else:
    print(min((n//2-es[1][1]) + (n//2-os[0][1]),
              (n//2-es[0][1]) + (n//2-os[1][1])))

Submission status: Accepted

Solution code:
N=int(input())
v=[int(i) for i in input().split()]
if N==2:
    if v[0]==v[1]:
        print(1)
        exit()
    else:
        print(0)
        exit()
A=dict()
B=dict()
L=0
R=0
def inc(D,k):
    if k in D:
        D[k]+=1
    else:
        D[k]=1
for i in range(N):
    if i%2==0:
        L+=1
        inc(A,v[i])
    else:
        R+=1
        inc(B,v[i])
X=[(L,-1)]
Y=[(R,-2)]
for i in A:
    X.append((L-A[i],i))
for i in B:
    Y.append((R-B[i],i))
X.sort(reverse=True)
Y.sort(reverse=True)
if X[-1][1]!=Y[-1][1]:
    print(X[-1][0]+Y[-1][0])
else:
    vl=X[-1][0]+Y[-2][0]
    vr=X[-2][0]+Y[-1][0]
    print(min(vl,vr))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input().strip())
vs = [int(i) for i in input().split(' ')]
vs1 = vs[::2]
vs2 = vs[1::2]
a = Counter(vs1).most_common()
b = Counter(vs2).most_common()

if a[0][0] != b[0][0]:
    print(n - a[0][1] - b[0][1])
else:
    candidates = []
    if len(a) != 1:
        candidates.append(n - a[1][1] - b[0][1])
    if len(b) != 1:
        candidates.append(n - a[0][1] - b[1][1])
    if len(candidates) != 0:
        print(min(candidates))
    else:
        print(n//2)

Submission status: Accepted

Solution code:
from collections import Counter

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

A = V[::2]
B = V[1::2]

resA = Counter(A).most_common()
resB = Counter(B).most_common()

if resA[0][0] == resB[0][0]:

    ans1 = sum([num for x, num in resA[1:]]) + resB[0][1] + sum([num for x, num in resB[2:]])
    ans2 = sum([num for x, num in resB[1:]]) + resA[0][1] + sum([num for x, num in resA[2:]])
    print(min(ans1, ans2))

else:
    if len(resA) == 1 and len(resB) == 1:
        print(0)
    else:
        sumA = sum([num for x, num in resA[1:]])
        sumB = sum([num for x, num in resB[1:]])
        print(sumA + sumB)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
l1 = [li for i, li in enumerate(l) if i % 2 == 0]
l2 = [li for i, li in enumerate(l) if i % 2 == 1]
def cnt(l):
    d = {}
    for li in l:
        if li in d:
            d[li] += 1
        else:
            d[li] = 1
    d = [(li, cnt) for li, cnt in d.items()]
    return sorted(d, key=lambda di: di[1], reverse=True)

k1 = cnt(l1)[:2]
k2 = cnt(l2)[:2]
ans = 10000000
if k1[0][0] != k2[0][0]:
    ans = n - k1[0][1] - k2[0][1]
if len(k1) == 2:
    ans = min(ans, n - k1[1][1] - k2[0][1])
if len(k2) == 2:
    ans = min(ans, n - k1[0][1] - k2[1][1])
if ans == 10000000:
    ans = n - max(k1[0][1], k2[0][1])
print(ans)

Submission status: Accepted

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

v_e_cnt = [0 for i in range(10**6)]
v_o_cnt = [0 for i in range(10**6)]
for x in range(n):
	if x % 2 == 0:
		v_e_cnt[v[x]] += 1
	else:
		v_o_cnt[v[x]] += 1

max_e = [(0,0),(0,0)]
max_o = [(0,0),(0,0)]
for i in range(10**5+1):
	if max_e[1][0] < v_e_cnt[i]:
		if max_e[0][0] < v_e_cnt[i]:
			max_e[1] = max_e[0]
			max_e[0] = (v_e_cnt[i], i)
		else:
			max_e[1] = (v_e_cnt[i], i)
	if max_o[1][0] < v_o_cnt[i]:
		if max_o[0][0] < v_o_cnt[i]:
			max_o[1] = max_o[0]
			max_o[0] = (v_o_cnt[i], i)
		else:
			max_o[1] = (v_o_cnt[i], i)

ans = n - max_e[0][0] - max_o[0][0]
if max_e[0][1] == max_o[0][1]:
	ans = n - max(max_e[0][0] + max_o[1][0], max_e[1][0] + max_o[0][0])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import numpy


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

even = a[0::2]
odd= a[1::2]
even_counter = Counter(even)
odd_counter = Counter(odd)
#print(len(even_counter.most_common()))
#print(odd_counter.most_common()[0][0])
c = even_counter.most_common()[0][0]
d = odd_counter.most_common()[0][0]

cnt = 0
if even_counter.most_common()[0][0] == odd_counter.most_common()[0][0]:
	if len(even_counter.most_common())==1 and len(odd_counter.most_common())==1:
		print(int(n/2))
		quit()
	elif len(even_counter.most_common())!=1 and len(odd_counter.most_common())==1:
		c = even_counter.most_common()[1][0]
	elif len(even_counter.most_common())==1 and len(odd_counter.most_common())!=1:
		d = odd_counter.most_common()[1][0]
	else:
		if len(even_counter.most_common())<len(odd_counter.most_common()):
			c = even_counter.most_common()[1][0]
		else:
			d  = odd_counter.most_common()[1][0]
            
    	
for i in range(int(n/2)):
	if a[i*2] != c:
		cnt = cnt+1
	if a[i*2+1] != d:
		cnt = cnt+1
#print(c)
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

odds = Counter(v[::2])
evens = Counter(v[1::2])

least_odds = odds.most_common(2)
least_evens = evens.most_common(2)
if len(least_odds) == 1:
    least_odds.append((-1,0))
if len(least_evens) == 1:
    least_evens.append((-1,0))

if least_odds[0][0] == least_evens[0][0]:
    keep = max(least_odds[0][1]+least_evens[1][1], least_odds[1][1]+least_evens[0][1])
else:
    keep = least_odds[0][1]+least_evens[0][1]

print(n-keep)

Submission status: Accepted

Solution code:
import sys
import socket
from collections import Counter

if socket.gethostname() in ['N551J', 'F551C']:
    sys.stdin = open('c1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_int():
    return int(input())


def read_str_list():
    return input().split()


def read_str():
    return input()


def solve():
    n = read_int()
    a = read_int_list()
    c = [Counter(a[k::2]) for k in range(2)]
    a, b = [c[k].most_common(1)[0] for k in range(2)]

    if a[0] != b[0]:
        return n - a[1] - b[1]
    if a[1] == n // 2 and b[1] == n // 2:
        return n // 2

    res = n
    if a[1] < n // 2:
        p, q = c[0].most_common(2)
        res = min(res, n - q[1] - b[1])
    if b[1] < n // 2:
        p, q = c[1].most_common(2)
        res = min(res, n - a[1] - q[1])

    return res


def main():
    res = solve()
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
v_odd = v[0::2]
v_even = v[1::2]
chars_odd = list(set(v_odd))
cnt_odd = dict()
for char in chars_odd:
    cnt_odd[char] = 0
for char in v_odd:
    cnt_odd[char] += 1
odd_cnt = sorted(cnt_odd.items(), key=lambda x:x[1], reverse=True)
odd_cnt.append(('_', 0))

chars_even = list(set(v_even))
cnt_even = dict()
for char in chars_even:
    cnt_even[char] = 0
for char in v_even:
    cnt_even[char] += 1
even_cnt = sorted(cnt_even.items(), key=lambda x:x[1], reverse=True)
even_cnt.append(('_', 0))

if odd_cnt[0][0] == even_cnt[0][0]:
    res = max(odd_cnt[0][1] + even_cnt[1][1], odd_cnt[1][1] + even_cnt[0][1])
else:
    res = odd_cnt[0][1] + even_cnt[0][1]
print(n - res)

Submission status: Accepted

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

v1 = {}
v2 = {}
for i in range(n):
    if(i % 2 == 0):
        if((v[i] in v1) == True):
            v1[v[i]] += 1
        else:
            v1[v[i]] = 1
    else: 
        if((v[i] in v2) == True):
            v2[v[i]] += 1
        else:
            v2[v[i]] = 1

max_k1 = max(v1, key=v1.get)
max_k2 = max(v2, key=v2.get)

ans1 = 1e9
ans2 = 1e9
if(max_k1 == max_k2):
    tmp = v1[max_k1]
    v1[max_k1] = 0
    max_k1 = max(v1, key=v1.get)
    ans1 = n - v1[max_k1] - v2[max_k2]
    v1[max_k1] = tmp
    max_k1 = max(v1, key=v1.get)

    v2[max_k2] = 0
    max_k2 = max(v2, key=v2.get)
    ans2 = n - v1[max_k1] - v2[max_k2]
else: ans1 = n - v1[max_k1] - v2[max_k2]

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 
import math
import sys
import itertools
import numpy as np


# いっこ入力
n =int(input())

# 空白区切りの数列をリストに代入
v = [int(i) for i in input().split()]

a = {v[0]:1}
b = {v[1]:1}
for i in range(2,n,2):
#    print(a)
    if v[i] in a:
        a[v[i]] += 1
    else:
        a[v[i]] = 1
    if v[i+1] in b:
        b[v[i+1]] += 1
    else:
        b[v[i+1]] = 1
    
#print(a,b)

max_a = max(a, key = a.get)
a1 = max(a.values())
max_b = max(b, key = b.get)
b1 = max(b.values())

if a1 == n // 2:
    a2=0
else:
    a[max_a]=0
    a2 = max(a.values())

if b1 == n // 2:
    b2=0
else:
    b[max_b]=0
    b2 = max(b.values())

c = a1+b2
d = a2+b1

if max_a == max_b:
    print(n-max(c,d))
else:
    print(n-a1-b1)

Submission status: Accepted

Solution code:
from collections import Counter

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


def main():
    A = V[::2]
    B = V[1::2]

    resA = Counter(A).most_common()
    resB = Counter(B).most_common()

    if resA[0][0] == resB[0][0]:

        if resA[0][1] > resB[0][1]:
            sumA = sum([num for x, num in resA[1:]])
            sumB = resB[0][1] + sum([num for x, num in resB[2:]])
            print(sumA + sumB)
        elif resA[0][1] < resA[0][1]:
            sumB = sum([num for x, num in resB[1:]])
            sumA = resA[0][1] + sum([num for x, num in resA[2:]])
            print(sumA + sumB)
        else:
            if len(resA) == 1 and len(resB) == 1:
                print(n // 2)
            else:
                if resA[1][1] > resB[1][1]:
                    sumB = sum([num for x, num in resB[1:]])
                    sumA = resA[0][1] + sum([num for x, num in resA[2:]])
                    print(sumA + sumB)
                else:
                    sumA = sum([num for x, num in resA[1:]])
                    sumB = resB[0][1] + sum([num for x, num in resB[2:]])
                    print(sumA + sumB)
    else:
        if len(resA) == 1 and len(resB) == 1:
            print(0)
        else:
            sumA = sum([num for x, num in resA[1:]])
            sumB = sum([num for x, num in resB[1:]])
            print(sumA + sumB)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

v1 = [v[2 * i] for i in range(len(v) // 2)]
v2 = [v[2 * i + 1] for i in range(len(v) // 2)]

v1in = {}
v2in = {}

for i in range(len(v1)):
    if v1[i] in v1in:
        v1in[v1[i]] += 1
    else:
        v1in[v1[i]] = 1

    if v2[i] in v2in:
        v2in[v2[i]] += 1
    else:
        v2in[v2[i]] = 1

if (len(v1in) == 1) and (len(v2in) == 1):
    if v[0] == v[1]:
        print(len(v1))
    else:
        print(0)
elif (len(v1in) == 1) and (len(v2in) != 1):
    max_k = max(v2in, key=v2in.get)
    if max_k == v[0]:
        del v2in[max_k]
        print(len(v2) - max(v2in.values()))
    else:
        print(len(v2) - v2in[max_k])
elif (len(v1in) != 1) and (len(v2in) == 1):
    max_k = max(v1in, key=v1in.get)
    if max_k == v[1]:
        del v1in[max_k]
        print(len(v1) - max(v1in.values()))
    else:
        print(len(v1) - v1in[max_k])
else:
    v1max_k = max(v1in, key=v1in.get)
    v2max_k = max(v2in, key=v2in.get)

    if v1max_k == v2max_k:
        v1max = v1in[v1max_k]
        del v1in[v1max_k]
        v1max2 = max(v1in.values())

        v2max = v2in[v2max_k]
        del v2in[v2max_k]
        v2max2 = max(v2in.values())

        print(min(len(v1) - v1max + len(v2) - v2max2,
                  len(v1) - v1max2 + len(v2) - v2max))
    else:
        print(len(v1) - v1in[v1max_k] + len(v2) - v2in[v2max_k])

Submission status: Accepted

Solution code:
n=int(input())
v=list(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 in sorted(v[0::2]):a[t][0]+=1
for t in sorted(v[1::2]):b[t][0]+=1
c=sorted(a)[::-1]
d=sorted(b)[::-1]
print(n-c[0][0]-[max(c[1][0],d[1][0]),d[0][0]][c[0][1]!=d[0][1]])

Submission status: Accepted

Solution code:
from sys import stdin
il = lambda func=int: list(map(func, stdin.readline().rstrip().split()))
ils = lambda n, s="int(input())": [eval(s) for _ in range(n)]

n = int(input())
v = il()
a = dict()
b = dict()
for i in range(n):
    if i&1:
        if v[i] in a:
            a[v[i]] += 1
        else:
            a[v[i]] = 1
    else:
        if v[i] in b:
            b[v[i]] += 1
        else:
            b[v[i]] = 1
a = sorted(a.items(), key=lambda x:x[1])
b = sorted(b.items(), key=lambda x:x[1])

h = a[-1][0]
ans = n//2 - a[-1][1]
if b[-1][0] == h:
    if len(b) == 1:
        ans += n//2
    else:
        ans += n//2 - b[-2][1]
else:
    ans += n//2 - b[-1][1]

hoge = ans 

h = b[-1][0]
ans = n//2 - b[-1][1]
if a[-1][0] == h:
    if len(a) == 1:
        ans += n//2
    else:
        ans += n//2 - a[-2][1]
else:
    ans += n//2 - a[-1][1]

print(min(ans, hoge))

Submission status: Accepted

Solution code:
from collections import defaultdict

_ = input()
ns = list(map(int, input().split()))

# ns = [4, 1, 3, 2]

xs = ns[0::2]
ys = ns[1::2]

ans = 0
counts = defaultdict(int)
for x in xs:
    counts[x] += 1

max_numx = -1
max_x = -1
for key in counts.keys():
    if counts[key] > max_numx:
        max_numx = counts[key]
        max_x = key

del counts[max_x]
if counts:
    max_numx2 = max(counts.values())
else:
    max_numx2 = 0

counts = defaultdict(int)
for y in ys:
    counts[y] += 1
max_numy = -1
max_y = -1
for key in counts.keys():
    if counts[key] > max_numy:
        max_numy = counts[key]
        max_y = key

del counts[max_y]
if counts:
    max_numy2 = max(counts.values())
else:
    max_numy2 = 0

if max_x != max_y:
    print(len(ns) - max_numx - max_numy)
else:
    print(min(len(ns) - max_numx - max_numy2, len(ns) - max_numx2 - max_numy))

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
v = list(map(int, input().split()))
rec0 = defaultdict(int)
rec1 = defaultdict(int)
for i in range(n):
    if i % 2 == 0:
        rec0[v[i]] += 1
    else:
        rec1[v[i]] += 1

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


rec00 = sorted(rec00, key=lambda x: x[1], reverse=True)
rec11 = sorted(rec11, key=lambda x: x[1], reverse=True)
m = n // 2
if rec00[0][0] != rec11[0][0]:
    print(abs(m - rec00[0][1]) + abs(m - rec11[0][1]))
else:
    if len(rec00) == 1 and len(rec11) == 1:
        print(m)
    elif len(rec00) == 1:
        print(abs(m - rec11[0][1]))
    elif len(rec11) == 1:
        print(abs(m - rec00[0][1]))
    else:
        a = abs(m - rec00[1][1])
        b = abs(m - rec11[1][1])
        num = min(a + abs(m - rec11[0][1]), abs(m - rec00[0][1]) + b)
        print(num)

Submission status: Accepted

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

odd = [0 for i in range(10**5)]
even = [0 for i in range(10**5)]

for i in range(n):
    if i % 2 == 0:
        odd[a[i]-1] += 1
    else:
        even[a[i]-1] += 1
        
oddmax = 0
evenmax = 0

oddfirst = 0
oddsecond = 0
evenfirst = 0
evensecond = 0

for i in range(10**5):
    if odd[i] >= oddfirst:
        oddsecond = oddfirst
        oddfirst = odd[i]
        oddmax = i
    elif odd[i] >= oddsecond:
        oddsecond = odd[i]
        
    if even[i] >= evenfirst:
        evensecond = evenfirst
        evenfirst = even[i]
        evenmax = i
    elif even[i] >= evensecond:
        evensecond = even[i]

a = 0
if oddmax == evenmax:
    a = min(oddfirst - oddsecond, evenfirst - evensecond)
        
ans = n - oddfirst - evenfirst + a
print(ans)

Submission status: Accepted

Solution code:
import sys,collections,math;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

n = I()
t = Is()
od,ev = [t[i] for i in range(0,n,2)], [t[i] for i in range(1,n,2)]
odm = collections.Counter(od).most_common()[:2]
evm = collections.Counter(ev).most_common()[:2]
if odm[0][0] != evm[0][0]:
    ans = n - odm[0][1] - evm[0][1]
elif len(odm) == len(evm) == 1:
    ans = n//2
elif len(odm) == 1:
    ans = n - odm[0][1] - evm[1][1]
elif len(evm) == 1:
    ans = n - odm[1][1] - evm[0][1]
else:
    ans = n - max(odm[0][1]+evm[1][1], odm[1][1]+evm[0][1])
print(ans)

Submission status: Accepted

Solution code:
from sys import stderr, setrecursionlimit
from functools import reduce
from operator import add
from collections import Counter
from collections import Counter
setrecursionlimit(2147483647)
def f(): return [int(i) for i in input().split()]
def yes(n): print("Yes" if n else "No")
def debug(*x, sep=" ", end="\n"):
    for item in x:
        stderr.write(str(item))
        stderr.write(sep)
    stderr.write(end)


n = int(input())
v = f()
debug(v)
ans = 0
even = Counter(v[0::2])
odd = Counter(v[1::2])

most_even = even.most_common(2)
most_odd = odd.most_common(2)

# 数字は2種類じゃないとダメ
if most_even[0][0] == most_odd[0][0]:
    if most_even[0][1] == most_odd[0][1]:
        # 2つ目が多い方を2つ目に  
        if len(most_even) != 1 and len(most_even) != 1:
            if most_even[1][1] < most_odd[1][1]:
                ans = (n//2 - most_even[0][1]) +  (n//2 - most_odd[1][1])
            else:
                ans = (n//2 - most_odd[0][1]) +  (n//2 - most_even[1][1])
        elif len(most_even) == 1 and len(most_odd) != 1:
            # most_evenの2番目は'0'
            ans = (n//2 - most_even[0][1]) + (n//2 - most_odd[1][1])
        elif len(most_even) != 1 and len(most_odd) == 1:
            # most_oddの2番目は'0'
            ans = (n//2 - most_even[1][1]) +  (n//2 - most_odd[0][1])
        else:
            ans = (n//2 - most_even[0][1]) + (n//2)
    # 総合結果から判断
    else:
        if len(most_even) != 1 and len(most_odd) != 1:
            # そのまま計算
            if most_even[0][1] + most_odd[1][1] < most_even[1][1] + most_odd[0][1]:
                ans = (n//2 - most_even[1][1]) +  (n//2 - most_odd[0][1])
            else:
                ans = (n//2 - most_even[0][1]) +  (n//2 - most_odd[1][1])
        elif len(most_even) == 1 and len(most_odd) != 1:
            # most_evenの2番目は"0"
            if most_even[0][1] + most_odd[1][1] < (n//2 - most_even[0][1]) + most_odd[0][1]:
                ans = most_even[0][1] + (n//2 - most_odd[0][1])
            else:
                ans = (n//2 - most_even[0][1]) + (n//2 - most_odd[1][1])
        elif len(most_even) != 1 and len(most_odd) == 1:
            # most_oddの2番目は"0"
            if most_even[0][1] + (n//2 - most_odd[0][1]) < most_even[1][1] + most_odd[0][1]:
                ans = (n//2 - most_even[1][1]) + (n//2 - most_odd[0][1])
            else:
                ans = (n//2 - most_odd[0][1]) + most_even[0][1]
        else:
            # どっちも2番目は"0"
            if most_even[0][1] < most_odd[0][1]:
                ans = (n//2 - most_odd[0][1]) + n//2
            else:
                ans = n//2 + (n//2 - most_even[0][1])
else:
    ans = n - most_even[0][1] - most_odd[0][1]

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

def main():
	n = int(input())
	v = [int(i) for i in input().split()]
	v1 = [0] * 100020
	v2 = [0] * 100020
	v11 = 0
	v21 = 0
	v12 = 0
	v22 = 0
	ans = 0
	for i in range(n):
		if i % 2 == 0:
			v1[v[i]] += 1
		else:
			v2[v[i]] += 1
	for i in range(10**5 + 10):
		if v1[i] >= v1[v11]:
			v12, v11 = v11, i
		elif v1[i]>=v1[v12]:
			v12 = i
		if v2[i] >= v2[v21]:
			v22, v21 = v21, i
		elif v2[i]>=v2[v22]:
			v22 = i
	if v11 == v21:
		ans = min(n-v1[v11] - v2[v22], n-v1[v12]-v2[v21])
	else:
		ans = n - v1[v11] - v2[v21]
	print(ans)

main()

Submission status: Accepted

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

f = {}
b = {}

fcnt = 0
bcnt = 0
for v in vv:
    if fcnt == bcnt:
        if v in f:
            f[v] += 1
        else:
            f[v] = 1
        fcnt += 1
    else:
        if v in b:
            b[v] += 1
        else:
            b[v] = 1
        bcnt += 1


ff = sorted(list(map(lambda x: tuple(reversed(x)), f.items())), reverse = True)
bb = sorted(list(map(lambda x: tuple(reversed(x)), b.items())), reverse = True)

if ff[0][1] != bb[0][1]:
    print(n - ff[0][0] - bb[0][0])
else:
    ans = 1145141919
    if len(bb) == 1:
        ans = min(ans, n - ff[0][0])
    else:
        ans = min(ans, n - ff[0][0] - bb[1][0])
    
    if len(ff) == 1:
        ans = min(ans, n - bb[0][0])
    else:
        ans = min(ans, n - bb[0][0] - ff[1][0])
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

even = Counter(v[::2])
odd = Counter(v[1::2])
even_most_common = even.most_common(2)
odd_most_common = odd.most_common(2)


ans = 0
if even_most_common[0][0] == odd_most_common[0][0]:
    if len(even_most_common) == 1 and len(odd_most_common) == 1:
        ans += n // 2
    elif len(even_most_common) == 1:
        ans += n // 2 - odd_most_common[1][1]
    elif len(odd_most_common) == 1:
        ans += n // 2 - even_most_common[1][1]
    elif even_most_common[0][1] == odd_most_common[0][1]:
        if even_most_common[1][1] < odd_most_common[1][1]:
            ans += n // 2 - even_most_common[0][1]
            ans += n // 2 - odd_most_common[1][1]
        else:
            ans += n // 2 - even_most_common[1][1]
            ans += n // 2 - odd_most_common[0][1]
    elif even_most_common[0][1] > odd_most_common[0][1]:
        ans += n // 2 - even_most_common[0][1]
        ans += n // 2 - odd_most_common[1][1]
    else:
        ans += n // 2 - even_most_common[1][1]
        ans += n // 2 - odd_most_common[0][1]
else:
    ans += n // 2 - even_most_common[0][1]
    ans += n // 2 - odd_most_common[0][1]

print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int,input().split()))
a = v[0::2]
b = v[1::2]
a = sorted(a)
b = sorted(b)
a = collections.Counter(a).most_common()
b = collections.Counter(b).most_common()

p,q = [],[]
r,s = n//2,n//2
for (c,d) in a:
    if d==a[0][1]:
        p.append(c)
    else:
        r = d
        break
for (c,d) in b:
    if d==b[0][1]:
        q.append(c)
    else:
        s = d
        break

if (len(a),len(b))==(1,1):
    if a==b:
        print(n//2)
    else:
        print(0)
elif len(a)==1:
    print(n//2-b[0][1])
elif len(b)==1:
    print(n//2-a[0][1])
elif (len(p),len(q))==(1,1) and p==q:
    print(min(n-a[0][1]-s,n-b[0][1]-r))
else:
    print(n-a[0][1]-b[0][1])

Submission status: Accepted

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

a = []
b = []
for i in range(n):
    if i%2 == 0:
        a.append(v[i])
    else:
        b.append(v[i])
    
ac = collections.Counter(a)
bc = collections.Counter(b)

atemp = []
for i in ac:
    atemp.append(ac[i])
btemp = []
for i in bc:
    btemp.append(bc[i])

mxma = max(atemp)
mxmb = max(btemp)
for i in ac:
    if ac[i] == mxma:
        mxma = i
        break
for i in bc:
    if bc[i] == mxmb:
        mxmb = i
        break

atemp.sort()
btemp.sort()

if mxma != mxmb:
    print(n-max(atemp)-max(btemp))
else:
    if len(atemp) == 1 and len(btemp) == 1:
        print(n-max(atemp))
    else:
        temp = [atemp[-2]+btemp[-1],atemp[-1]+btemp[-2]]
        print(n-max(temp))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try: from typing import Any, Union, List, Tuple, Dict
except ImportError: pass
sys.setrecursionlimit(10**6)
def debug(*args): print(*args, file=sys.stderr)
def exit(): sys.exit(0)

from collections import Counter

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

c = Counter(v)
if len(c) == 1:
    print(n//2)
    exit()
# fc = c.most_common(2)
# count1 = fc[0][1]
# count2 = fc[1][1]
# ok = 0
# debug(fc)

# if count1 <= n//2:
#     ok += count1
# else:
#     ok += n//2
# ok += count2


# print(n-ok)

odd = [v[2*i+1] for i in range(n//2)]
even = [v[2*i] for i in range(n//2)]
# odd_count = Counter(odd).most_common(1)[0][1]
# even_count = Counter(even).most_common(1)[0][1]

oc = Counter(odd).most_common()
ec = Counter(even).most_common()

if oc[0][0] != ec[0][0]:
    odd_count = oc[0][1]
    even_count = ec[0][1]
    both_count = odd_count + even_count
else:
    # if oc[0][1] > ec[0][1]:
    #     odd_count = oc[0][1]
    #     even_count = ec[1][1]
    # else:
    #     odd_count = oc[1][1]
    #     even_count = ec[0][1]
    both1 = oc[0][1] + ec[1][1]
    both2 = oc[1][1] + ec[0][1]
    both_count = max(both1, both2)

print(n-both_count)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

ODD = defaultdict(int)
EVEN = defaultdict(int)
NUMBER = set()

for i in range(n):
    if i%2==0:
        EVEN[V[i]] += 1
    else:
        ODD[V[i]] += 1
    NUMBER.add(V[i])
    
    
ODD[999999] = 0
EVEN[999999] = 0
if len(NUMBER)==1:
    print(n//2)
else:
    flag = True
    max_ODD = max(ODD, key=ODD.get)
    max_EVEN = max(EVEN, key=EVEN.get)
    O = [ODD[max_ODD]]
    E = [EVEN[max_EVEN]]
    if max_ODD != max_EVEN:
        print(n-O[0]-E[0])
    else:
        ODD.pop(max_ODD)
        EVEN.pop(max_EVEN)
        max_ODD = max(ODD, key=ODD.get)
        max_EVEN = max(EVEN, key=EVEN.get)
        O.append(ODD[max_ODD])
        E.append(EVEN[max_EVEN])
        print(min([n-O[0]-E[1],n-O[1]-E[0]]))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v_list = [int(i) for i in input().split()]
odd_v = v_list[::2]
even_v = v_list[1::2]

cnt_odd = Counter(odd_v).most_common()+[(0, 0)]
cnt_even = Counter(even_v).most_common()+[(0, 0)]

if cnt_odd[0][0] == cnt_even[0][0]:
    print(min(n-cnt_odd[0][1]-cnt_even[1][1], \
              n-cnt_odd[1][1]-cnt_even[0][1]))
else:
    print(n-cnt_odd[0][1]-cnt_even[0][1])

Submission status: Accepted

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

odv = []
evv = []

for i in range(n):
    if i%2 == 1:
        odv.append(v[i])
    else:
        evv.append(v[i])

odvc=collections.Counter(odv)
evvc=collections.Counter(evv)

odvs=len(odv)
evvs=len(evv)

odv_kind=len(odvc.most_common())
evv_kind=len(evvc.most_common()) 

if evvc.most_common()[0][0] != odvc.most_common()[0][0]:
    print( odvs-odvc.most_common()[0][1] + evvs-evvc.most_common()[0][1] )
else:
    if odv_kind > 1 and evv_kind > 1:
        print( min(odvs-odvc.most_common()[0][1] + evvs-evvc.most_common()[1][1] ,
                   odvs-odvc.most_common()[1][1] + evvs-evvc.most_common()[0][1] ) )
    else:
        if odv_kind > 1:
            print( odvs-odvc.most_common()[0][1] )
        elif evv_kind > 1:
            print( evvs-evvc.most_common()[0][1] )
        else:
            print(min(odvs,evvs))

Submission status: Accepted

Solution code:
n = int(input())
vi = list(map(int,input().split()))
odd = [vi[k] for k in range(0,n,2)]
even = [vi[k+1] for k in range(0,n,2)]
odd.sort()
even.sort()

odd.append(-1)
even.append(-1)

oddset = []
oddcounter = []
pos = 0
while pos<n/2:
    oddset.append(odd[pos])
    counter = 1
    now = odd[pos]
    pos += 1
    while now == odd[pos]:
        counter +=1
        pos += 1
    oddcounter.append([counter,now])

evenset = []
evencounter = []
pos = 0
while pos<n/2:
    evenset.append(even[pos])
    counter = 1
    now = even[pos]
    pos += 1
    while now == even[pos]:
        counter +=1
        pos += 1
    evencounter.append([counter,now])

oddcounter.sort(key=lambda x:x[0],reverse=True)
evencounter.sort(key=lambda x:x[0],reverse=True)

oc = oddcounter
ec = evencounter

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

Submission status: Accepted

Solution code:
N = int(input())
v = list(map(int, input().split()))
d0 = {0: 0}
d1 = {0: 0}
for j, i in enumerate(v):
    if j % 2 == 0:
        if i in d0:
            d0[i] += 1
        else:
            d0[i] = 1
    else:
        if i in d1:
            d1[i] += 1
        else:
            d1[i] = 1

l0 = sorted(d0.items(), key=lambda x: -x[1])
l1 = sorted(d1.items(), key=lambda x: -x[1])

if l0[0][0] == l1[0][0]:
    a = min(N - l0[0][1] - l1[1][1], N - l0[1][1] - l1[0][1])
    print(a)
else:
    print(N - l0[0][1] - l1[0][1])

Submission status: Accepted

Solution code:
import numpy as np
import collections

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

e=np.array([v[i] for i in range(0,n,2)])
o=np.array([v[i] for i in range(1,n,2)])

se=set(e)
so=set(o)


lle=collections.Counter(e)
llo=collections.Counter(o)
le=[]
for k,v in lle.items():
    le.append([n//2-v,k])
lo=[]
for k,v in llo.items():
    lo.append([n//2-v,k])
le.sort()
lo.sort()

if(len(lle)==1 and len(llo)==1):
    if(e[0]==o[0]):print(n//2)
    else:print(0)
    exit()




if(len(se)==1):
    if(lo[0][1]!=le[0][1]):print(lo[0][0])
    else:print(lo[1][0])
elif(len(so)==1):
    if(lo[0][1]!=le[0][1]):print(le[0][0])
    else:print(le[1][0])
else:
    if(lo[0][1]!=le[0][1]):print(le[0][0]+lo[0][0])
    else:print(min(le[1][0]+lo[0][0],le[0][0]+lo[1][0]))