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()))

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

freq_e = {}
freq_o = {}
for j in range(int(n / 2)):
  if (str(e[j]) in freq_e):
    freq_e[str(e[j])] += 1
  elif not(str(e[j]) in freq_e):
    freq_e[str(e[j])] = 1
  if (str(o[j]) in freq_o):
    freq_o[str(o[j])] += 1
  elif not(str(o[j]) in freq_o):
    freq_o[str(o[j])] = 1

freq_e_sorted = sorted(freq_e.items(), key = lambda x: -x[1])
freq_o_sorted = sorted(freq_o.items(), key = lambda x: -x[1])
if freq_e_sorted[0][0] != freq_o_sorted[0][0]:
  ans = n - freq_e_sorted[0][1] - freq_o_sorted[0][1]
elif len(freq_e_sorted) == 1 and len(freq_o_sorted) == 1:
  ans = freq_e_sorted[0][1]
elif len(freq_e_sorted) == 1 or freq_e_sorted[0][1] + freq_o_sorted[1][1] > freq_e_sorted[1][1] + freq_o_sorted[0][1]:
  ans = n - freq_e_sorted[0][1] - freq_o_sorted[1][1]
else:
  ans = n - freq_e_sorted[1][1] - freq_o_sorted[0][1]
print(ans)

Submission status: Accepted

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

va, ve = v[0::2], v[1::2]
da, de = {}, {}
for a in va:
  da[a] = da.get(a, 0)+1
for e in ve:
  de[e] = de.get(e, 0)+1
add = sorted([(k,v) for k,v in da.items()], key=lambda x:-x[1])+[(0,0)]
even = sorted([(k,v) for k,v in de.items()], key=lambda x:-x[1])+[(0,0)]
if add[0][0] == even[0][0]:
  if add[1][1] <= even[1][1]:
    even.pop(0)
  else:
    add.pop(0)
print(n-add[0][1]-even[0][1])

Submission status: Accepted

Solution code:
#なぜこれが通らないのか分からない
from collections import Counter
N = int(input())
v = list(map(int, input().split()))

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

o = Counter(odd).most_common(2)
e = Counter(even).most_common(2)

o.append((0,0))
e.append((0,0))


ans = N+1
for d1 in o:
    for d2 in e:
        if d1[0]!=d2[0]:
            ans = min(ans, N-d1[1]-d2[1])
print(ans)

Submission status: Accepted

Solution code:
#なぜこれが通らないのか分からない
from collections import Counter
N = int(input())
v = list(map(int, input().split()))

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

o = Counter(odd).most_common(2)
e = Counter(even).most_common(2)

o.append((0,0))
e.append((0,0))

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

Submission status: Accepted

Solution code:
from collections import Counter

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

if len(set(V)) == 1:
    ans = len(V) // 2
else:
    vp_ = [v for i, v in enumerate(V) if i%2 == 0] 
    vn_ = [v for i, v in enumerate(V) if i%2 != 0] 

    vp = Counter(vp_).most_common()
    vn = Counter(vn_).most_common()

    if vp[0][0] != vn[0][0]:
        gp = vp[0][1]
        gn = vn[0][1]
    else:
        if vp[0][1] > vn[0][1]:
            gp = vp[0][1]
            gn = vn[1][1]
        elif vp[0][1] < vn[0][1]:
            gp = vp[1][1]
            gn = vn[0][1]
        else:
            if vp[1][1] >= vn[1][1]:
                gp = vp[1][1]
                gn = vn[0][1]
            else:
                gp = vp[0][1]
                gn = vn[1][1]
 
    ans = n - gp - gn
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
do = {0:0}
de = {0:0}
for i in range(n):
    if i % 2:
        do[v[i]] = do.get(v[i], 0) + 1
    else:
        de[v[i]] = de.get(v[i], 0) + 1
o = max(do, key=do.get)
e = max(de, key=de.get)
if o != e:
    print(n-do[o]-de[e])
else:
    vo = do.pop(o)
    ve = de.pop(e)
    vo2 = max(do.values())
    ve2 = max(de.values())
    print(n - max(vo+ve2, ve+vo2))

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
V=list(map(int,input().split()))
v1=Counter(V[::2]).most_common()
v2=Counter(V[1::2]).most_common()
if len(v1) ==1:
  v1.append([0,0])
if len(v2) == 1:
  v2.append([0,0])
if v1[0][0]==v2[0][0]:
  print(min(n-v1[0][1]-v2[1][1],n-v1[1][1]-v2[0][1]))
else:
  print(n-v1[0][1]-v2[0][1])

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
V=list(map(int,input().split()))
v1=Counter(V[::2]).most_common()
v2=Counter(V[1::2]).most_common()
if len(v1) ==1:
  v1.append([0,0])
if len(v2) == 1:
  v2.append([0,0])
if v1[0][0]==v2[0][0]:
  if v1[1][1]>v2[1][1]:
    print(n-v1[1][1]-v2[0][1])
  else:
    print(n-v1[0][1]-v2[1][1])
else:
  print(n-v1[0][1]-v2[0][1])

Submission status: Accepted

Solution code:
from operator import itemgetter

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

	kinds = len(set(v))
	if kinds == 1:
		print(int(len(v)/2))
		return

	numbers = [{}, {}]
	for i in range(n):
		index = 0
		if i%2 == 1:
			index = 1

		if v[i] not in numbers[index]:
			numbers[index][v[i]] = 1
		else:
			numbers[index][v[i]] += 1

	if len(numbers[0])==1 and len(numbers[1])==1:
		print("0")
		return

	sort_nums = []
	for i in range(2):
		sort_nums.append(sorted(numbers[i].items(), key=itemgetter(1), reverse=True))
	
	maxes = [sort_nums[0][0], sort_nums[1][0]]
	if sort_nums[0][0][0]!=sort_nums[1][0][0]:
		print(n - sort_nums[0][0][1] - sort_nums[1][0][1])
		return

	#from here is the case where sort_nums[0][0][0] == sort_nums[1][0][0]

	b1 = -100
	b2 = -100

	if len(sort_nums[0])==1:
		print(n - sort_nums[0][0][1] - sort_nums[1][1][1])
		return
	if len(sort_nums[1])==1:
		print(n - sort_nums[1][0][1] - sort_nums[0][1][1])
		return


	if len(sort_nums[0])>1 and len(sort_nums[1])>1:
		b1 = n - sort_nums[0][0][1] - sort_nums[1][1][1]
		b2 = n - sort_nums[1][0][1] - sort_nums[0][1][1]
		print(min(b1, b2))


main()

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)
even.append((0,0))
odd.append((0,0))
if even[0][0] != odd[0][0]:
    print(n - even[0][1] - odd[0][1])
else:
    print(min(n - even[0][1] - odd[1][1], n - even[1][1] - odd[0][1]))

Submission status: Accepted

Solution code:
def main():
    inf = 1 << 60
    n = int(input())
    v = list(map(int, input().split()))

    a = [{}, {}]
    for i,vi in enumerate(v):
        ai = i % 2
        if vi in a[ai].keys():
            a[ai][vi] += 1
        else:
            a[ai][vi] = 1

    b = []
    for i in range(0, 2):
        lst = sorted(list(a[i].items()), key=lambda e:e[1], reverse=True)
        b.append(lst)
        #print(lst)

    if b[0][0][0] != b[1][0][0]:
        return n - b[0][0][1] - b[1][0][1]
    b1 = -inf
    b2 = -inf
    if len(b[1]) >= 2:
        b1 = n - b[0][0][1] - b[1][1][1]
    if len(b[0]) >= 2:
        b2 = n - b[0][1][1] - b[1][0][1]
    if b1 > b2:
        if b2 >= 0:
            return b2
    else:
        if b1 >= 0:
            return b1
    if b[0][0][1] > b[1][0][1]:
        return n - b[0][0][1]
    else:
        return n - b[1][0][1]

ans = main()
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/2/
Solved on 2019/2/
@author: shinjisu
"""


# ABC 111 C /\/\/\/
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def getIntLines(n): return [int(input()) for i in range(n)]


def getIntMat(n):
    mat = []
    for i in range(n):
        mat.append(getIntList())
    return mat


def zeros2(n, m): return [zeros(m)]*n


def dmp(x):
    global debug
    if debug:
        print(x)
    return x


def probC():
    N = getInt()
    V = getIntList()
    dmp((N, V))
    odd = {}
    even = {}
    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
    dmp(('even',even))
    dmp(('odd',odd))
    evenList = [[even[k], k] for k in even]
    evenList.sort()
    evenList.reverse()
    dmp(evenList)
    oddList = [[odd[k], k] for k in odd]
    oddList.sort()
    oddList.reverse()
    dmp(oddList)
    if evenList[0][1] == oddList[0][1]:  # 度数最大の値が同一の場合
        if len(evenList) == 1 and len(oddList) == 1:
            evenMaxCount = evenList[0][0]
            oddMaxCount = 0
        elif len(evenList) > 1 and len(oddList) > 1:
            evenMaxCount = evenList[0][0]
            oddMaxCount = max(evenList[1][0], oddList[1][0])
        elif len(evenList) > 1:
            evenMaxCount = evenList[1][0]
            oddMaxCount = oddList[0][0]
        else:
            evenMaxCount = evenList[0][0]
            oddMaxCount = oddList[1][0]
    else:
        evenMaxCount = evenList[0][0]
        oddMaxCount = oddList[0][0]
    dmp(oddMaxCount)
    dmp(evenMaxCount)
    return N - evenMaxCount - oddMaxCount


debug = False  # True False
print(probC())

Submission status: Accepted

Solution code:
n = int(input())
v_ls = [int(i) for i in input().split()]
_1st = v_ls[1::2]
_2nd = v_ls[::2]
_1st_dict = dict()
_2nd_dict = dict()
for i in _1st:
    if _1st_dict.get(i) is None:
        _1st_dict[i] = 1
    else:
        _1st_dict[i] += 1
for i in _2nd:
    if _2nd_dict.get(i) is None:
        _2nd_dict[i] = 1
    else:
        _2nd_dict[i] += 1
if len(_1st_dict) <= 1 and len(_2nd_dict) <= 1:
    if _1st[0] == _2nd[0]:
        print(len(_2nd))
    else:
        print(0)
else:
    _1st_dict = sorted(_1st_dict.items(), key= lambda x:x[1], reverse=True)
    _2nd_dict = sorted(_2nd_dict.items(), key= lambda x:x[1], reverse=True)
    while _1st_dict[0][0] == _2nd_dict[0][0]:
        if _1st_dict[0][1] > _2nd_dict[0][1]:
            _2nd_dict.pop(0)
        elif _1st_dict[0][1] < _2nd_dict[0][1]:
            _1st_dict.pop(0)
        elif _1st_dict[1][1] > _2nd_dict[1][1]:
             _1st_dict.pop(0)
        else:
            _2nd_dict.pop(0)
    _1st_dict = dict(_1st_dict)
    _2nd_dict = dict(_2nd_dict)
    dupli = max(_1st_dict.values()) + max(_2nd_dict.values())
    print(len(v_ls) - dupli)

Submission status: Accepted

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

for i in range(n):
    if i % 2 == 0:
        gu_row.append(v[i])
    elif i % 2 == 1:
        ki_row.append(v[i])

gu_count = collections.Counter(gu_row).most_common()
ki_count = collections.Counter(ki_row).most_common()

if len(gu_count) > 1 and len(ki_count) > 1:
    if gu_count[0][0] != ki_count[0][0]:
        ans = n - gu_count[0][1] - ki_count[0][1]
    elif gu_count[0][0] == ki_count[0][0] and len(gu_count) == 1:
        ans = n - gu_count[0][1] - ki_count[1][1]
    elif gu_count[0][0] == ki_count[0][0] and len(ki_count) == 1:
        ans = n - gu_count[1][1] - ki_count[0][1]
    else:
        ans = n - max(gu_count[0][1] + ki_count[1][1], gu_count[1][1] + ki_count[0][1])
else:
    if gu_count[0][0] != ki_count[0][0]:
        ans = n - gu_count[0][1] - ki_count[0][1]
    else:
        ans = gu_count[0][1]

print(ans)

Submission status: Accepted

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

def solve(n: int, v: "List[int]"):
    half_length = len(v) // 2
    O = [v[i] for i in range(1, n, 2)]
    E = [v[i] for i in range(0, n, 2)]

    co = list(collections.Counter(O).most_common())
    ce = list(collections.Counter(E).most_common())
    o1 = co[0][0]
    e1 = ce[0][0]

    O_ = list(filter(lambda o: o != o1, O))
    E_ = list(filter(lambda e: e != e1, E))
    num_o_1 = len(O_) 
    num_e_1 = len(E_)

    num = 0
    if e1 != o1:
        num = num_o_1 + num_e_1
    else:
        if len(co) == 1 and len(ce) == 1:
            num = half_length
        elif len(co) == 1:
            e2 = ce[1][0]
            num_e_2 = len(list(filter(lambda e: e != e2, E)))
            num = num_o_1 + num_e_2
        elif len(ce) == 1:
            o2 = co[1][0]
            num_o_2 = len(list(filter(lambda o: o != o2, O)))
            num = num_o_2 + num_e_1
        else:
            e2 = ce[1][0]
            o2 = co[1][0]
            num_e_2 = len(list(filter(lambda e: e != e2, E)))
            num_o_2 = len(list(filter(lambda o: o != o2, O)))
            num = min(num_o_1 + num_e_2, num_o_2 + num_e_1)
    print(num)
    return

def len2(l):
    n = 0
    for i in l:
        n += 1
    return n

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

h_even = [[i, 0] for i in range(0, 10**5 + 1)]
h_odd = [[i, 0] for i in range(0, 10**5 + 1)]

for i in range(0, n // 2):
    h_even[v[2 * i]][1] += 1
    h_odd[v[2 * i + 1]][1] += 1
    
h_even.sort(reverse=True, key=lambda k: k[1])
h_odd.sort(reverse=True, key=lambda k:k[1])

if h_even[0][0] == h_odd[0][0]:
    print(min(n - h_even[0][1] - h_odd[1][1], n - h_even[1][1] - h_odd[0][1]))
else:
    print(n - h_even[0][1] - h_odd[0][1])

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
d0={0:0}
for a in A[::2]:d0[a]=d0.get(a,0)+1
d0=sorted([(k,v) for k,v in d0.items()],key=lambda x:-x[1])
d1={0:0}
for a in A[1::2]:d1[a]=d1.get(a,0)+1
d1=sorted([(k,v) for k,v in d1.items()],key=lambda x:-x[1])
if d0[0][0]!=d1[0][0]:print(N-d0[0][1]-d1[0][1])
else:print(min(N-d0[0][1]-d1[1][1],N-d0[1][1]-d1[0][1]))

Submission status: Accepted

Solution code:
n = int(input())

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

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

odd_dict = dict({})
even_dict = dict({})

for i in range(n//2):
    if odd_dict.__contains__(odd[i]):
        odd_dict[odd[i]]+=1
    else:
        odd_dict[odd[i]] = 1
    if even_dict.__contains__(even[i]):
        even_dict[even[i]]+=1
    else:
        even_dict[even[i]] = 1

odd = sorted(odd_dict.items(), key=lambda d:d[1],reverse=True)
even = sorted(even_dict.items(), key=lambda d:d[1],reverse=True)

odd.append((0,0))
even.append((0,0))
if odd[0][0] == even[0][0]:
    print(min(n-odd[0][1]-even[1][1],n-odd[1][1]-even[0][1]))
else:
    print(n-odd[0][1]-even[0][1])

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc111/tasks/arc103_a

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

f_l = v[::2]
s_l = v[1::2]


def count_and_sort(l):
    dict = {}
    for f in l:
        if f not in dict:
            dict[f] = 1
        else:
            dict[f] += 1
    return sorted(dict.items(), key=lambda x: -x[1])


f_sorted = count_and_sort(f_l)
s_sorted = count_and_sort(s_l)

f1 = f_sorted[0]
s1 = s_sorted[0]

f2 = f_sorted[1] if len(f_sorted) > 1 else (0, 0)
s2 = s_sorted[1] if len(s_sorted) > 1 else (0, 0)

if f1[0] == s1[0]:  # 同じ値だったら使えない
    print(min(n - f1[1] - s2[1], (n - f2[1] - s1[1])))
else:
    print(n - f1[1] - s1[1])

Submission status: Accepted

Solution code:
n = int(input())
V = input().split()

d0, d1 = {}, {}
for i in range(n//2):
    v = V[2*i]
    if v in d0:
        d0[v] += 1
    else:
        d0[v] = 1

    v = V[2*i+1]
    if v in d1:
        d1[v] += 1
    else:
        d1[v] = 1

s0 = sorted(d0.items(), key=lambda x: -x[1])
k0_0, v0_0 = s0[0]
s1 = sorted(d1.items(), key=lambda x: -x[1])
k1_0, v1_0 = s1[0]

if k0_0 != k1_0:
    print(n - v0_0 - v1_0)
else:
    k0_1, v0_1 = s0[1] if len(s0)>1 else ('0',0)
    k1_1, v1_1 = s1[1] if len(s1)>1 else ('0',0)
    if v0_0+v1_1 <= v0_1+v1_0:
        print(n - v1_0 - v0_1)
    else:
        print(n - v0_0 - v1_1)

Submission status: Accepted

Solution code:
import collections as col
from functools import reduce
from operator import add
n = int(input())

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

if len(set(v)) == 1:
    print(int(n / 2))
    exit()

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

co = col.Counter(odd).most_common()
ce = col.Counter(even).most_common()

if co[0][0] != ce[0][0]:
    print(n - co[0][1] - ce[0][1])
else:
    # a1 = reduce(add, coli[1:], 0)
    # b1 = reduce(add, celi[2:], 0)
    # b1 = celi[0] + b1

    # a2 = reduce(add, coli[2:], 0)
    # b2 = reduce(add, celi[1:], 0)
    # a2 = a2 + coli[0]
    if len(co) <= 1:
        co.append((0, 0))
    if len(ce) <= 1:
        ce.append((0, 0))

    ans1 = n - co[1][1] - ce[0][1]
    ans2 = n - co[0][1] - ce[1][1]

    print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import heapq

def main():
    n = int(input())
    vList = list(map(int, input().split()))
    vDict1 = {}
    vDict2 = {}
    for i in range(len(vList)):
        if i%2 == 0:
            vDict1[vList[i]] = vDict1.get(vList[i], 0) + 1
        else:
            vDict2[vList[i]] = vDict2.get(vList[i], 0) + 1
    v1List = list(vDict1.items())
    v2List = list(vDict2.items())
    if len(vDict1) == 1 and len(vDict2) == 1:
        if v1List[0][0] == v2List[0][0]:
            print(n//2)
        else:
            print(0)
        return
    if len(v1List) == 1:
        v1Max= heapq.nlargest(1, v1List, key=lambda v1: v1[1])
    else:
        v1Max, v1snd = heapq.nlargest(2, v1List, key=lambda v1: v1[1])
    if len(v2List) == 1:
        v2Max = heapq.nlargest(1, v2List, key=lambda v2: v2[1])
    else:
        v2Max, v2snd = heapq.nlargest(2, v2List, key=lambda v2: v2[1])
    if len(vDict1) == 1:
        if v2Max[0] == v1Max[0]:
            print(n//2-v2snd[1])
        else:
            print(n//2-v2Max[1])  
        return   
    if len(vDict2) == 1:
        if v1Max[0] == v2Max[0]:
            print(n//2-v1snd[1])
        else:
            print(n//2-v1Max[1]) 
        return
    if v1Max[0] == v2Max[0]:
        if v1snd[1] <= v2snd[1]:
            print(n-v1Max[1]-v2snd[1])
        else:
            print(n-v1snd[1]-v2Max[1])
        return
    print(n-v1Max[1]-v2Max[1])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

odd = defaultdict(lambda: 0)
even = defaultdict(lambda: 0)
odd[None] = 0
even[None] = 0
for i in v[0::2]:
    odd[i] += 1
for i in v[1::2]:
    even[i] += 1

odd = sorted(odd.items(), key=lambda x: x[1], reverse=True)
even = sorted(even.items(), key=lambda x: x[1], reverse=True)
if odd[0][0] != even[0][0]:
    count = n - odd[0][1] - even[0][1]
else:
    count = n - max(odd[0][1]+even[1][1], odd[1][1]+even[0][1])

print(count)

Submission status: Accepted

Solution code:
n = int(input())
v = [0] + list(map(int,input().split())) #index調整
odds = [0] * (10**5+1)
evens = [0] * (10**5+1)
oddmax, evenmax = 0, 0
oddindex, evenindex = 0, 0
for i in range(1,n+1):
    if i % 2 == 1:
        odds[v[i]] += 1
    else:
        evens[v[i]] += 1
for i in range(1,10**5+1):
    if oddmax < odds[i]:
        oddmax = odds[i]
        oddindex = i
    if evenmax < evens[i]:
        evenmax = evens[i]
        evenindex = i
if oddindex != evenindex:
    print(n-oddmax-evenmax)
else:
    odds.sort()
    odds.reverse()
    evens.sort()
    evens.reverse()
    ans = min(n-odds[0]-evens[1], n-odds[1]-evens[0])
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
input()
V = list(map(int, input().split()))
odd = Counter(V[::2])
even = Counter(V[1::2])
odd_max = max(odd.items(), key=lambda x: x[1])
even_max = max(even.items(), key=lambda x: x[1])
if odd_max[0] != even_max[0]:
    print(len(V) - odd_max[1] - even_max[1])
    exit()
odd_second = odd.most_common(2)
if len(odd_second) >= 2:
    odd_second = odd_second[1][1]
else:
    odd_second = 0
even_second = even.most_common(2)
if len(even_second) >= 2:
    even_second = even_second[1][1]
else:
    even_second = 0
if odd_second >= even_second:
    first = even_max[1]
    second = odd_second
else:
    first = odd_max[1]
    second = even_second
print(len(V) - first - second)

Submission status: Accepted

Solution code:
# C
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
a=Counter(v[0::2]).most_common()
b=Counter(v[1::2]).most_common()
a.append([0,0])
b.append([0,0])
if a[0][0]!=b[0][0]:
    print(n-(a[0][1]+b[0][1]))
else:
    print(min(n-(a[1][1]+b[0][1]),n-(a[0][1]+b[1][1])))

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter

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

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

even_count = Counter(v_even)
values_even, counts_even = zip(*even_count.most_common())
odd_count = Counter(v_odd)
values_odd, counts_odd = zip(*odd_count.most_common())

if(values_even[0]!=values_odd[0]): #最も頻度が高いものが偶数番目と機数番目で違う
    print(int(np.sum(counts_odd[1:]) + np.sum(counts_even[1:])))

else: #最も頻度が高いものが偶数番目と機数番目で同じ
    if(len(counts_odd)==1 and len(counts_even)==1):
        print(len(v_odd))
    else:
        if(counts_odd[1] >= counts_even[1]):
            print(int(counts_odd[0] + np.sum(counts_odd[2:]) + np.sum(counts_even[1:])))
        else:
            print(int(counts_even[0] + np.sum(counts_even[2:]) + np.sum(counts_odd[1:])))

Submission status: Accepted

Solution code:
from collections import Counter


def main() -> None:
    n = int(input())
    p = [int(i) for i in input().split()]

    # 奇数番目の配列
    l1 = [int(p[i]) for i in range(0, n, 2)]
    # 偶数盤面の配列
    l2 = [int(p[i]) for i in range(1, n, 2)]

    if len(set(p)) == 1:
        # ゾロ目判定
        print(n // 2)
    else:
        lc1 = Counter(l1).most_common(2)
        lc2 = Counter(l2).most_common(2)

        if lc1[0][0] == lc2[0][0]:
            # 最頻値がかぶってしまった場合
            print(n - max(lc1[0][1] + lc2[1][1], lc1[1][1] + lc2[0][1]))
        else:
            # かぶっていなかったら最頻値を引く
            print(n - lc1[0][1] - lc2[0][1])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
ans = []
L = list(map(int,input().split()))
if len(list(filter(lambda x:x==L[0] ,L))) == len(L):
    print(n//2)
    quit()


import collections as col
L1 = L[::2]
L2 = L[1::2]
c1 = sorted(list(col.Counter(L1).items()),key=lambda x:x[1])[::-1]
c2 =sorted(list(col.Counter(L2).items()),key=lambda x:x[1])[::-1]

for i in range(3):
    for j in range(3):
        try:
            tmp1 = c1[i][0]
            tmp2 = c2[j][0]
            if tmp1 == tmp2 :
                continue
            a1 = n//2 - c1[i][1]
            a2 = n//2 - c2[j][1]
            ans.append(a1+a2)
        except:
            pass
print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
l1 = []
l2 = []
for i,v_ in enumerate(v):
    if i%2==0:
        l1.append(v_)
    else:
        l2.append(v_)

from collections import Counter
if len(set(v))==1:
    print(n//2)
else:
    c1 = Counter(l1).most_common(2)
    c2 = Counter(l2).most_common(2)
    if c1[0][0]==c2[0][0]:
        print(n-max(c1[0][1]+c2[1][1], c1[1][1]+c2[0][1]))
    else:
        print(n-c1[0][1]-c2[0][1])

Submission status: Accepted

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

if len(set(V)) == 1:
    print(N//2)
    
else:
    v1 = Counter(V[0::2]).most_common(2)
    v2 = Counter(V[1::2]).most_common(2)
    if v1[0][0] == v2[0][0]:
        print(N - max(v1[0][1] + v2[1][1], v1[1][1] + v2[0][1]))
    else:
        print(N - v1[0][1] - v2[0][1])

Submission status: Accepted

Solution code:
from collections import Counter
# counter（l）.most_common()：リストについて出現回の多い順に（要素、要素数）で返す
a=int(input())
A=list(map(int,input().split()))
b=[A[i] for i in range(a) if i%2==0]
c=[A[i] for i in range(a) if i%2==1]
#　全部同じ数の時
if b==c and len(set(b))==1 and len(set(c))==1:
    print(a//2)
else:
    bc=Counter(b).most_common()
    cc=Counter(c).most_common()
    if bc[0][0]==cc[0][0]:
        print(a-max(bc[1][1]+cc[0][1],bc[0][1]+cc[1][1]))
    else: print(a-bc[0][1]-cc[0][1])

Submission status: Accepted

Solution code:
from collections import Counter
a=int(input())
A=list(map(int,input().split()))
b=[A[i] for i in range(a) if i%2==0]
c=[A[i] for i in range(a) if i%2==1]
if b==c and len(set(b))==1 and len(set(c))==1:
    print(a//2)
else:
    bc=Counter(b).most_common()
    cc=Counter(c).most_common()
    if bc[0][0]==cc[0][0]:
        print(a-max(bc[1][1]+cc[0][1],bc[0][1]+cc[1][1]))
    else: print(a-bc[0][1]-cc[0][1])

Submission status: Accepted

Solution code:
import sys
from collections import Counter

def most_key(d, f=None):
  k = 0
  num = 0
  for key in d:
    if key == f:
      continue
    if d[key] >= num:
      k = key
      num = d[key]
  return k


def main():
  input = sys.stdin.readline
  n = int(input())
  V = [v for v in map(int, input().split())]
  Vo = [V[i] for i in range(1, n, 2)]
  Ve = [V[i] for i in range(0, n, 2)]
  
  odd = Counter(Vo)
  even = Counter(Ve)
  
  odd_num = most_key(odd)
  even_num = most_key(even)
  
  if odd_num == even_num:
    odd_sec = most_key(odd, odd_num)
    even_sec = most_key(even, even_num)
    
    odd_replace = len(Vo) - odd[odd_num]
    odd_replace_sec = len(Vo) - odd[odd_sec]
    even_replace = len(Ve) - even[even_num]
    even_replace_sec = len(Ve) - even[even_sec]
    
    patern1 = odd_replace + even_replace_sec
    patern2 = odd_replace_sec + even_replace
    
    print(min(patern1, patern2))
  
  else:
    odd_replace = len(Vo) - odd[odd_num]
    even_replace = len(Ve) - even[even_num]
    print(odd_replace + even_replace)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
N = int(input())
V = list(map(int,input().split()))
even = [0]*(10**5+10)
odd = [0]*(10**5+10)
for v in V[::2]:
  odd[v] += 1
for v in V[1::2]:
  even[v] += 1
omax = max(odd)
emax = max(even)
if(even.index(emax) != odd.index(omax)):
  print(N - emax - omax)
else:
  even.sort()
  odd.sort()
  print(min(N - even[-1] - odd[-2], N - even[-2] - odd[-1]))

Submission status: Accepted

Solution code:
import collections

n=int(input())


X=list(map(int,input().split()))
odd=[]
even=[]
for i in range(int(n/2)):
    odd.append(X[2*i+1])
    even.append(X[2*i])

odd_counter=collections.Counter(odd) #登場回数とその値の辞書型
even_counter=collections.Counter(even)

odd_count=sorted(odd_counter.values(),reverse=True) #登場回数の多い順に並べた配列
even_count=sorted(even_counter.values(),reverse=True)

max_odd_value=max(odd_counter,key=odd_counter.get) #登場回数が最大の値
max_even_value=max(even_counter,key=even_counter.get)

if max_odd_value!=max_even_value:
    print(n-odd_count[0]-even_count[0])
elif len(set(odd))!=1 and len(set(even))!=1:
    print(min(n-odd_count[0]-even_count[1],n-odd_count[1]-even_count[0]))
elif len(set(odd))==1 and len(set(even))==1:
    print(int(n/2))
elif len(set(odd))==1:
    print(int(n/2)-even_count[1])
elif len(set(even))==1:
    print(int(n/2)-odd_count[1])

Submission status: Accepted

Solution code:
import heapq
from collections import defaultdict

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

odds = defaultdict(int)
evens = defaultdict(int)
i = 0
while i < n-1:
    odds[vs[i]] += 1
    evens[vs[i+1]] += 1
    i += 2

odd_tops = heapq.nlargest(2, odds.items(), key=lambda x: x[1])
even_tops = heapq.nlargest(2, evens.items(), key=lambda x: x[1])

odd_top_val = odd_tops[0][0]
even_top_val = even_tops[0][0]

if len(odd_tops) == 1:
    odd_tops.append((None, 0))
if len(even_tops) == 1:
    even_tops.append((None, 0))

if odd_top_val == even_top_val:
    a = odd_tops[0][1]
    b = odd_tops[1][1]
    c = even_tops[0][1]
    d = even_tops[1][1]
    if a+d > b+c:
        print(n - a - d)
    else:
        print(n - b - c)
else:
    print(n - odd_tops[0][1] - even_tops[0][1])

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
v = [int(i) for i in input().split()]
v_even = [v[i] for i in range(len(v)) if i % 2 == 0]
v_odd = [v[i] for i in range(len(v)) if i % 2 == 1]

even_c = Counter(v_even)
odd_c = Counter(v_odd)
#print(odd_c.most_common(), even_c.most_common())

if odd_c.most_common()[0][0] != even_c.most_common()[0][0]:  # 最頻値の値が違うなら
    output = N-(odd_c.most_common()[0][1] + even_c.most_common()[0][1])
elif N - (odd_c.most_common()[0][1] + even_c.most_common()[0][1]) == 0:  # すべてのvが同じ
    output = N // 2
else:
    if len(even_c) != even_c.most_common()[0][1]:
        frag1 = True
        output1 = N - (odd_c.most_common()[0][1] + even_c.most_common()[1][1])
    else:
        frag1 = False
    if len(odd_c) != odd_c.most_common()[0][1]:
        frag2 = True
        output2 = N - (odd_c.most_common()[1][1] + even_c.most_common()[0][1])
    else:
        frag2 = False
    if frag1 and frag2:
        output = min(output1, output2)
    elif frag1:
        output = output1
    elif frag2:
        output = output2
print(output)

Submission status: Accepted

Solution code:
def sortcounts(c: dict):
    a = [(v, k) for k, v in c.items()]
    return sorted(a, reverse=True)


def solve(a):
    m = len(a)//2

    o = [a[i] for i in range(len(a)) if i%2==0]
    e = [a[i] for i in range(len(a)) if i%2==1]

    # print(o)
    # print(e)

    import collections
    co = sortcounts(collections.Counter(o))
    ce = sortcounts(collections.Counter(e))

    # print(co)
    # print(ce)

    no, vo = co[0]
    ne, ve = ce[0]

    if vo != ve:
        ans = (m-no) + (m-ne)
    else:
        anse = (m-no) + m-(0 if len(co)<2 else co[1][0])
        anso = (m-ne) + m-(0 if len(ce)<2 else ce[1][0])
        ans = min(anse, anso)

    return ans


def main():
    _=input()
    a = list(map(int, input().split()))
    print(solve(a))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
cnt = [[0 for j in range(10**5)] for i in range(2)]
for i in range(n):
  cnt[i%2][v[i]-1] += 1
maxce = max(cnt[0])
maxco = max(cnt[1])
maxve = -1
maxvo = -1
for i in range(len(cnt[0])):
  if(cnt[0][i] == maxce):
    maxve = i
    break
for i in range(len(cnt[1])):
  if(cnt[1][i] == maxco):
    maxvo = i
    break
if(maxvo != maxve):
  print((n//2 - maxco) + (n - n//2 - maxce))
else:
  cnt[0].pop(maxve)
  cnt[1].pop(maxvo)
  maxce2 = max(cnt[0])
  maxco2 = max(cnt[1])
  print(min((n//2 - maxco2) + (n - n//2 - maxce), (n//2 - maxco) + (n - n//2 - maxce2)))