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

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

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

no_change = 0
no_change1 = 0
no_change2 = 0

no_change1 += v1[0][1]
for i in v2:
    if i[0] != v1[0][0]:
        no_change1 += i[1]
        break

no_change2 += v2[0][1]
for i in v1:
    if i[0] != v2[0][0]:
        no_change2 += i[1]
        break

no_change = max(no_change1, no_change2)

print(n-no_change)

Submission status: Accepted

Solution code:
import heapq

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

one = V[::2]
two = V[1:][::2]

one,two

one_count={}
one_len=len(one)
for o in one:
    if o in one_count:
        one_count[o] += 1
    else:
        one_count[o] = 1

for k,v in one_count.items():
    one_count[k] = one_len-v
one_count[10**6]=one_len
temp = [[k,v] for k,v in one_count.items()]
one = heapq.nsmallest(3,temp, key=lambda x: x[1])


two_len=len(two)
two_count={}

for t in two:
    if t in two_count:
        two_count[t] += 1
    else:
        two_count[t] = 1

for k,v in two_count.items():
    two_count[k] = two_len-v
two_count[10**6]=two_len
temp = [[k,v] for k,v in two_count.items()]
two = heapq.nsmallest(3,temp, key=lambda x: x[1])

mins = 1000000

for [one_key, one_value] in one:
    for [two_key, two_value] in two:
        if one_key != two_key:
            if (one_value + two_value) < mins:
                mins = one_value + two_value
print(mins)

Submission status: Accepted

Solution code:
n = int(input())
vl = list(map(int,input().split()))
v1 = vl[::2]
v2 = vl[1::2]

def count(a):
  d = dict()
  for i in a:
    if i not in d:
      d[i] = 1
    else:
      d[i] += 1
  l = []
  for i in d:
    l.append([d[i],i])
  l.append([0,0])
  l.sort(reverse=1)
  return l

v1 = count(v1)
v2 = count(v2)
if v1[0][1] != v2[0][1]:
  print(n-v1[0][0]-v2[0][0])
elif v1[1][0]+v2[0][0] > v1[0][0]+v2[1][0]:
  print(n-v1[1][0]-v2[0][0])
else:
  print(n-v1[0][0]-v2[1][0])

Submission status: Accepted

Solution code:
n = int(input())
vl = list(map(int,input().split()))
v1 = vl[::2]
v2 = vl[1::2]

def count(a):
  d = dict()
  for i in a:
    if i not in d:
      d[i] = 1
    else:
      d[i] += 1
  l = []
  for i in d:
    l.append([d[i],i])
  l.append([0,0])
  l.sort(reverse=1)
  return l

v1 = count(v1)
v2 = count(v2)
if v1[0][0] == v1[1][0] or v2[0][0] ==v2[1][0]:
  print(n-v1[0][0]-v2[0][0])
elif v1[0][1] != v2[0][1]:
  print(n-v1[0][0]-v2[0][0])
elif v1[1][0]+v2[0][0] > v1[0][0]+v2[1][0]:
  print(n-v1[1][0]-v2[0][0])
else:
  print(n-v1[0][0]-v2[1][0])

Submission status: Accepted

Solution code:
from collections import Counter

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

odd = []
even = []

for m in range(0,n, 2):
    odd.append(V[m])
for m in range(1,n, 2):
    even.append(V[m])

odd_c = Counter(odd)
even_c = Counter(even)

odd_c_sort = sorted(odd_c.items(), key = lambda x:x[1], reverse = True)
even_c_sort = sorted(even_c.items(), key = lambda x:x[1], reverse = True)

if len(odd_c_sort) == len(even_c_sort) == 1:
    if odd_c_sort[0][0] == even_c_sort[0][0]:
        ans = n/2
    else:
        ans = 0
elif len(odd_c_sort) == 1:
    if odd_c_sort[0][0] == even_c_sort[0][0]:
        ans = n/2 - even_c_sort[1][1]
    else:
        ans = n/2 - even_c_sort[0][1]
elif len(even_c_sort) == 1:
    if even_c_sort[0][0] == odd_c_sort[0][0]:
        ans = n/2 - odd_c_sort[1][1]
    else:
        ans = n/2 - odd_c_sort[0][1]
else:
    if odd_c_sort[0][0] != even_c_sort[0][0]:
        ans = n - odd_c_sort[0][1] - even_c_sort[0][1]
    else:
        ans = min((n - odd_c_sort[0][1] - even_c_sort[1][1]), (n - odd_c_sort[1][1] - even_c_sort[0][1]))

print(int(ans))

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:
		v1.append(v[i])
	else:
		v2.append(v[i])
v1.sort()
v2.sort()

cnt1 = []
cnt2 = []
now = v1[0]
con = 1
for i in range(1, len(v1)):
	if now == v1[i]:
		con += 1
	else:
		cnt1.append([con, now])
		now = v1[i]
		con = 1
cnt1.append([con, now])
now = v2[0]
con = 1
for i in range(1, len(v2)):
	if now == v2[i]:
		con += 1
	else:
		cnt2.append([con, now])
		now = v2[i]
		con = 1
cnt2.append([con, now])

cnt1.sort()
cnt2.sort()
if cnt1[len(cnt1)-1][1] != cnt2[len(cnt2)-1][1]:
	p1 = [cnt1[i][0] for i in range(len(cnt1))]
	p2 = [cnt2[i][0] for i in range(len(cnt2))]
	print(sum(p1[:len(p1)-1])+sum(p2[:len(p2)-1]))
else:
	p1 = [cnt1[i][0] for i in range(len(cnt1))]
	p2 = [cnt2[i][0] for i in range(len(cnt2))]
	ans = min(sum(p1[:len(p1)-2])+p1[len(p1)-1]+sum(p2[:len(p2)-1]), sum(p1[:len(p1)-1])+p2[len(p2)-1]+sum(p2[:len(p2)-2]))
	print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
if len(set(l))==1:
    print(n//2)
    exit()
even=[l[i] for i in range(n) if i%2==0]
odd=[l[i] for i in range(n) if i&1]
from collections import Counter 
c1=Counter(even)
c2=Counter(odd)
a=[i for i in c1]
b=[i for i in c2]
maxi=max(c1.values())
maxi=max(maxi,max(c2.values()))
a.sort(key=lambda x:-c1[x])
b.sort(key=lambda x:-c2[x])
for a1 in a: 
    for b1 in b: 
        if a1==b1:
            continue 
        maxi=max(maxi,c1[a1]+c2[b1])
        break 
print(n-maxi)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    v = list(map(int,input().split()))
    odd={}
    even={}
    for i in range(n):
        if i%2==0:
            if v[i] not in odd:
                odd[v[i]]=1
            else:
                odd[v[i]]+=1
        else:
            if v[i] not in even:
                even[v[i]]=1
            else:
                even[v[i]]+=1
    cnt_o=[]
    cnt_e=[]
    for i in odd:
        cnt_o.append([odd[i],i])
    for i in even:
        cnt_e.append([even[i],i])
    cnt_o.append([0,10**6])
    cnt_e.append([0,10**6])
    cnt_o.sort(reverse=True)
    cnt_e.sort(reverse=True)
    if cnt_o[0][1]!=cnt_e[0][1]:
        print(n-cnt_o[0][0]-cnt_e[0][0])
        return
    else:
        print(min(n-cnt_o[1][0]-cnt_e[0][0],n-cnt_o[0][0]-cnt_e[1][0]))
        return
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections


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

V1 = V[::2]
V2 = V[1::2]

V1_mosts = collections.Counter(V1).most_common()
V2_mosts = collections.Counter(V2).most_common()

if V1_mosts[0][0] != V2_mosts[0][0]:
    print(N - V1_mosts[0][1] - V2_mosts[0][1])

else:
    costs = []

    costs.append(N - V1_mosts[0][1] - V2_mosts[1][1] if len(V2_mosts) >= 2 else N // 2)
    costs.append(N - V2_mosts[0][1] - V1_mosts[1][1] if len(V1_mosts) >= 2 else N // 2)

    print(min(costs))

Submission status: Accepted

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

    ans = 0
    odic = {"0": 0}
    edic = {"0": 0}

    for i in range(int(n/2)):
        if str(v[i*2]) in odic:
            odic[str(v[i*2])] = odic[str(v[i*2])] + 1
        else:
            odic[str(v[i*2])] = 1
        if str(v[i*2 + 1]) in edic:
            edic[str(v[i*2 + 1])] = edic[str(v[i*2 + 1])] + 1
        else:
            edic[str(v[i*2 + 1])] = 1
    
    ols = sorted(odic.values(), reverse=True)
    els = sorted(edic.values(), reverse=True)


    if max(odic, key=odic.get) != max(edic, key=edic.get):
        ans = n - ols[0] - els[0]
    else:
        if ols[0]+els[1] < ols[1]+els[0]:
            ans = n - ols[1] - els[0]
        else:
            ans = n - ols[0] - els[1]

    print(ans)    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

even = V[0::2]
odd = V[1::2]
even_counter = Counter(even).most_common(2)
odd_counter = Counter(odd).most_common(2)

# if even_max_cnt[0][0] != odd_max_cnt[0][0]:
#     ans = len(V) - even_max_cnt[0][1] - odd_max_cnt[0][1]
# else:
#     if len(even_max_cnt) == 1 or len(odd_max_cnt) == 1:
#         ans = len(V) // 2
#     else:
#         ans = len(V) - max(even_max_cnt[0][1]+odd_max_cnt[1][1],
#                            even_max_cnt[1][1]+odd_max_cnt[0][1])
#
# print(ans)

if even_counter[0][0] != odd_counter[0][0]:
    ans = len(V) - even_counter[0][1] - odd_counter[0][1]
else:
    if len(even_counter) == 1 and len(odd_counter) == 1:
        ans = len(V) // 2
    else:
        ans = len(V) - max(even_counter[0][1]+odd_counter[1][1],
                           even_counter[1][1]+odd_counter[0][1])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

even = V[0::2]
odd = V[1::2]
even_max_cnt = Counter(even).most_common(2)
odd_max_cnt = Counter(odd).most_common(2)

if even_max_cnt[0][0] != odd_max_cnt[0][0]:
    ans = len(V) - even_max_cnt[0][1] - odd_max_cnt[0][1]
else:
    if len(even_max_cnt) == 1 or len(odd_max_cnt) == 1:
        ans = len(V) // 2
    else:
        ans = len(V) - max(even_max_cnt[0][1]+odd_max_cnt[1][1],
                           even_max_cnt[1][1]+odd_max_cnt[0][1])

print(ans)

Submission status: Accepted

Solution code:
import collections as coll

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

oddc = coll.Counter(odd).most_common()[:2]
evenc = coll.Counter(even).most_common()[:2]

if(len(oddc) == 1):
	oddc.append((0,0))
if(len(evenc) == 1):
	evenc.append((0,0))


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

Submission status: Accepted

Solution code:
import collections as coll

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

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

oddc = coll.Counter(odd).most_common()[:2]
evenc = coll.Counter(even).most_common()[:2]

if(len(oddc) == 1):
	oddc.append((0,0))
if(len(evenc) == 1):
	evenc.append((0,0))


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

Submission status: Accepted

Solution code:
from collections import Counter

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

o = v[::2]
e = v[1::2]
co = list(Counter(o).items()) + [(-1, 0)]
ce = list(Counter(e).items()) + [(-2, 0)]
co.sort(key=lambda x: x[1], reverse=True)
ce.sort(key=lambda x: x[1], reverse=True)

if co[0][0] == ce[0][0] :
    print(min(n - co[0][1] - ce[1][1], n - co[1][1] - ce[0][1]))
else :
    print(n - co[0][1] - ce[0][1])

Submission status: Accepted

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    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])))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
V = [int(el) for el in input().split(' ')]
 
a = Counter(V[0::2]).most_common() + [(0,0)]
b = Counter(V[1::2]).most_common() + [(0,0)]

#print(a)
#print(b)
 
if a[0][0]!=b[0][0]:
    print(n-a[0][1]-b[0][1])
else:
    print(n-max(a[1][1]+b[0][1],a[0][1]+b[1][1]))

Submission status: Accepted

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

p=[0]+sorted(v[0:n-1:2])+[0]
l=r=o1=o2=0
for i in range(1,n//2+2):
  if p[i-1] != p[i]:
    if l>=o1:
      o2,o1,r=o1,l,p[i-1]
    elif l>o2:
      o2=l
    l=1
  else:
    l+=1

pr = r
po1 = o1
po2 = o2

p=[0]+sorted(v[1:n:2])+[0]
l=r=o1=o2=0
for i in range(1,n//2+2):
  if p[i-1] != p[i]:
    if l>=o1:
      o2,o1,r=o1,l,p[i-1]
    elif l>o2:
      o2=l
    l=1
  else:
    l+=1

if pr != r:
  print(n-po1-o1)
else:
  print(min(n-po1-o2,n-o1-po2))

Submission status: Accepted

Solution code:
import collections


n = int(input())
odd = []
even = []
for i,j in enumerate(map(int,input().split())):
    if i % 2 == 0:
        even += [j]
    else:
        odd += [j]
odd = sorted(collections.Counter(odd).items(),key=lambda x:(x[1]),reverse=True)
even = sorted(collections.Counter(even).items(),key=lambda x:(x[1]),reverse=True)

if len(odd) == 1 and len(even) == 1:
    if odd[0][0] == even[0][0]:
        print(n//2)
        exit()
    else:
        print(0)
        exit()

if odd[0][0] == even[0][0]:
    if len(odd) >= 2 and len(even) >= 2:
        if odd[0][1] + even[1][1] > odd[1][1] + even[0][1]:
            print((n//2 - odd[0][1]) + (n//2 - even[1][1]))
        else:
            print((n//2 - odd[1][1]) + (n//2 - even[0][1]))
    else:
        if len(odd) == 1:
            print((n//2 - odd[0][1]) + (n//2 - even[1][1]))
        else:
            print((n//2 - odd[1][1]) + (n//2 - even[0][1]))
else:
    print((n//2 - odd[0][1]) + (n//2 - even[0][1]))

Submission status: Accepted

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

c0mc = Counter(V[::2]).most_common(2)
c1mc = Counter(V[1::2]).most_common(2)
# [0] is error avoidance
if len(c0mc) == 1:
    c0mc.append((0, 0))
if len(c1mc) == 1:
    c1mc.append((0, 0))

print(N - max(a[1] + b[1] for (a, b) in product(c0mc, c1mc) if a[0] != b[0]))

Submission status: Accepted

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

dict_odd = dict()
dict_even = dict()

for i in range(N):
	if i % 2 == 1:
		if V[i] in dict_odd:
			dict_odd[V[i]] += 1
		else:
			dict_odd[V[i]] = 1
			
	else:
		if V[i] in dict_even:
			dict_even[V[i]] += 1
		else:
			dict_even[V[i]] = 1
			
odd_sorted = sorted(dict_odd.items(), key=lambda x:x[1], reverse = True)
even_sorted = sorted(dict_even.items(), key=lambda x:x[1], reverse = True)

if odd_sorted[0][0] != even_sorted[0][0]:
	print(N - odd_sorted[0][1] - even_sorted[0][1])
else:
	if len(odd_sorted) == 1 and len(even_sorted) == 1:
		print(int(N / 2))
	elif len(odd_sorted) == 1:
		print(N - odd_sorted[0][1] - even_sorted[1][1])
	elif len(even_sorted) == 1:
		print(N - even_sorted[0][1] - odd_sorted[1][1])
	else:
		print(N - max(odd_sorted[0][1] + even_sorted[1][1], odd_sorted[1][1] + even_sorted[0][1]))

Submission status: Accepted

Solution code:
import collections

n = int(input())
odd = []
even = []
for i, j in enumerate(map(int, input().split())):
    if i % 2 == 0:
        even += [j]
    else:
        odd += [j]
odd = sorted(collections.Counter(odd).items(), reverse=True, key=lambda x: x[1])
even = sorted(collections.Counter(even).items(), reverse=True, key=lambda x: x[1])

if len(odd) == 1 and len(even) == 1 and odd[0][0] == even[0][0]:
    print(n // 2)
elif odd[0][0] == even[0][0]:
    if odd[0][1] == even[0][1]:
        for i in range(2):
            if odd[i][1] == even[i][1]:
                continue
            elif odd[i][1] > even[i][1]:
                print((n // 2 - odd[1][1]) + (n // 2 - even[0][1]))
                break
            elif odd[i][1] < even[i][1]:
                print((n // 2 - odd[0][1]) + (n // 2 - even[1][1]))
                break
        else:
            print((n // 2 - odd[0][1]) + (n // 2 - even[1][1]))
else:
    print((n // 2 - odd[0][1]) + (n // 2 - even[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter

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

s1 = Counter(V).most_common(2)[0]
s2 = Counter(V).most_common(2)[0]
V1 = V[0::2]
V2 = V[1::2]
c1 = c2 = c3 = c4 = 0
Z1 = Counter(V1)
Z2 = Counter(V2)
x1, c1 = Z1.most_common(1)[0]
x2, c2 = Z2.most_common(1)[0]
if len(Z1) > 1:
    x3, c3 = Counter(V1).most_common(2)[1]
if len(Z2) > 1:
    x4, c4 = Counter(V2).most_common(2)[1]
if x1 == x2:
    a = max(c1+c4, c2+c3)
    print(N-a)
else:
    print(N - c1 - c2)

Submission status: Accepted

Solution code:
from collections import Counter

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

s1 = Counter(V).most_common(2)[0]
s2 = Counter(V).most_common(2)[0]
V1 = V[0::2]
V2 = V[1::2]
x1, c1 = Counter(V1).most_common(1)[0]
x2, c2 = Counter(V2).most_common(1)[0]
if x1 == x2:
    if c1 > c2:
        x2, c2 = Counter(V2).most_common(2)[1]
    elif c1 < c2:
        x1, c1 = Counter(V1).most_common(2)[1]
    elif c1 == c2 == N // 2:
        c1, c2 = N//2, 0
    else:

        x2, c2 = Counter(V2).most_common(2)[1]
        x3, c3 = Counter(V1).most_common(2)[1]
        c2 = max(c2, c3)

print(N - c1 - c2)

Submission status: Accepted

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

dE={};dO={}
for i in range(n):
    dX=dE if i%2==0 else dO
    if v[i] in dX:
        dX[v[i]]+=1
    else:
        dX[v[i]]=1

lE=sorted(list(dE.items()),key=lambda x:x[1],reverse=True)
lO=sorted(list(dO.items()),key=lambda x:x[1],reverse=True)

me=lE[0][1];mo=lO[0][1]
ae=lE[0][0];ao=lO[0][0]

se=0 if len(lE)==1 else lE[1][1]
so=0 if len(lO)==1 else lO[1][1]

if ae==ao:
    ans=n-max(me+so,mo+se)
else:
    ans=n-(me+mo)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
V=list(map(int,input().split()))
A=[0]*(10**5+1)
B=[0]*(10**5+1)
for i in range(N):
  if(i%2==0):
    A[V[i]]+=1
  else:
    B[V[i]]+=1
MA=0
NA=A[0]
MB=0
NB=B[0]
for i in range(10**5+1):
  if(A[i]>=NA):
    NA=A[i]
    MA=i
  if(B[i]>=NB):
    NB=B[i]
    MB=i
A.sort(reverse=True)
B.sort(reverse=True)
if(MA!=MB):
  print(N-NA-NB)
else:
  print(min(N-A[0]-B[1],N-A[1]-B[0]))

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
odds=[[0,i]  for i in range(10**5+1)]
even=[[0,i]  for i in range(10**5+1)]
for i in range(n):
    if i%2==0:
        even[v[i]][0]+=1
    else:
        odds[v[i]][0]+=1
even.sort(reverse=True)
odds.sort(reverse=True)
if even[0][1]==odds[0][1]:
    m=max(even[0][0]+odds[1][0],even[1][0]+odds[0][0])
    print(n-m)
else:
    print(n-even[0][0]-odds[0][0])

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
even = dict()
odd = dict()
even[0] = 0
odd[0] = 0
for i in range(n//2):
  a = v[2*i]
  if a in even:
    even[a] += 1
  else:
    even[a] = 1
  a = v[2*i + 1]
  if a in odd:
    odd[a] += 1
  else:
    odd[a] = 1

even_sorted = sorted(even.items(), key=lambda x:x[1], reverse=True)
odd_sorted = sorted(odd.items(), key=lambda x:x[1], reverse=True)

if even_sorted[0][0] == odd_sorted[0][0]:
  print(n - max(even_sorted[0][1]+odd_sorted[1][1], even_sorted[1][1]+odd_sorted[0][1]))
else:
  print(n - even_sorted[0][1] - odd_sorted[0][1])

Submission status: Accepted

Solution code:
import collections

n = int(input())
v = list(map(int, input().split()))
even = []
odd = []
for i in range(0, n, 2):
    odd.append(v[i])
for i in range(1, n, 2):
    even.append(v[i])
odd_counter = collections.Counter(odd)
even_counter = collections.Counter(even)

odd_counter_sorted = sorted(odd_counter.items(),
                            key=lambda x: x[1],
                            reverse=True)
even_counter_sorted = sorted(even_counter.items(),
                             key=lambda x: x[1],
                             reverse=True)
if odd_counter_sorted[0][0] != even_counter_sorted[0][0]:
    print(
        len(odd) - odd_counter_sorted[0][1] + len(even) -
        even_counter_sorted[0][1])
else:
    ans = float("inf")
    if len(odd_counter_sorted) > 1:
        a = len(odd) - odd_counter_sorted[1][1] + len(
            even) - even_counter_sorted[0][1]
    else:
        a = len(odd) + len(even) - even_counter_sorted[0][1]

    if len(even_counter_sorted) > 1:
        b = len(odd) - odd_counter_sorted[0][1] + len(
            even) - even_counter_sorted[1][1]
    else:
        b = len(odd) - odd_counter_sorted[0][1] + len(even)
    print(min(a, b))

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
e=Counter(v[0::2]).most_common(2)
o=Counter(v[1::2]).most_common(2)
if len(set(v))==1:
    print(n//2)
else:
    if e[0][0]==o[0][0]:
        print(n-max(e[1][1],o[1][1])-e[0][1])
    else:
        print(n-e[0][1]-o[0][1])

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
v = list(map(int, input().split()))
v1 = Counter(v[::2]).most_common(2)
v2 = Counter(v[1::2]).most_common(2)
v1.append((0, 0))
v2.append((0, 0))
if v1[0][0] != v2[0][0]:
    print(n - v1[0][1] - v2[0][1])
else:
    print(min(n - v1[0][1] - v2[1][1], n - v1[1][1] - v2[0][1]))

Submission status: Accepted

Solution code:
import collections
import sys

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

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

c_even = collections.Counter(even).most_common()
c_odd = collections.Counter(odd).most_common()

if c_odd[0][0] == c_even[0][0]:
  if len(c_even) == 1 and len(c_odd) == 1:
    print(int(n/2))
    sys.exit()
  elif len(c_even) == 1:
    print(int(n/2) - c_odd[1][1])
    sys.exit()

  elif len(c_odd) == 1:
    print(int(n/2) - c_even[1][1])
    sys.exit()
  
  if c_even[1][1] > c_odd[1][1]:
     answer = (len(even)-c_even[1][1])+(len(odd)-c_odd[0][1])
  else:
     answer = (len(even)-c_even[0][1])+(len(odd)-c_odd[1][1])
else:
  answer = (len(even)-c_even[0][1])+(len(odd)-c_odd[0][1])
print(answer)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
o=[]
e=[]
for i in range(n):
  if i%2==0:
    o.append(a[i])
  else:
    e.append(a[i])
from collections import Counter as co
o=co(o)
e=co(e)
o['x']=0
e['x']=0
ov=o.values()
ev=e.values()
mo=max(ov)
me=max(ev)
if n==2:
  if a[0]==a[1]:
    print(1)
  else:
    print(0)
else:
  if max(o,key=o.get)==max(e,key=e.get):
    if mo>me:
      me=sorted(ev)[-2]
    elif mo<me:
      mo=sorted(ov)[-2]
    else:
      mo=max(sorted(ev)[-2],sorted(ov)[-2])
  print(n-mo-me)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
def doit(vs):
    ret = {}
    for v in vs:
        ret[v] = ret.get(v, 0) + 1
    return sorted((-ret[v], v) for v in ret) + [(0, None)]
odd = doit(v[0::2])
even = doit(v[1::2])
if odd[0][1] != even[0][1]:
    print(n + odd[0][0] + even[0][0])
else:
    print(n + min(odd[1][0] + even[0][0], odd[0][0] + even[1][0]))

Submission status: Accepted

Solution code:
#111c
import collections
import copy

n = int(input()) #2≤n≤10**5 偶数
v_list = [int(e) for e in input().split()]

#数列 a1,a2,...,anが以下の条件を満たすとき、 /\/\/\/ と呼ぶことにします。

#・各 i=1,2,...,n−2について、ai=ai+2 
#・数列に現れる数はちょうど 2種類

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

#一番多く出てくる文字を求め、書き換えない数字を決める
v_list_odd = [v_list[i] for i in range(n) if i%2==1]
v_list_even = [v_list[i] for i in range(n) if i%2==0]

#print(v_list_odd)
#print(v_list_even)

c = collections.Counter(v_list_odd)
v_list_odd_counter = copy.copy(c)
if len(c)>1:
    fix_picklist_for_v_list_odd = [((c.most_common(2))[0])[0],((c.most_common(2))[0])[1],((c.most_common(2))[1])[0],((c.most_common(2))[1])[1]]
else:
    fix_char_for_v_list_odd = fix_picklist_for_v_list_odd = [((c.most_common(2))[0])[0],((c.most_common(2))[0])[1]]
    
c = collections.Counter(v_list_even)
v_list_even_counter = copy.copy(c)
if len(c)>1:
    fix_picklist_for_v_list_even = [((c.most_common(2))[0])[0],((c.most_common(2))[0])[1],((c.most_common(2))[1])[0],((c.most_common(2))[1])[1]]
else:
    fix_picklist_for_v_list_even = [((c.most_common(2))[0])[0],((c.most_common(2))[0])[1]]
    
#固定文字の決定
#1文字目が重複していなければ、両方とも出現回数最多を固定する
if fix_picklist_for_v_list_even[0] != fix_picklist_for_v_list_odd[0]:
    fix_char_for_v_list_even = fix_picklist_for_v_list_even[0]
    fix_char_for_v_list_odd = fix_picklist_for_v_list_odd[0]

#1文字目が重複している場合
#両方1文字しかなければ片方のみ
elif len(fix_picklist_for_v_list_even) == 2 and len(fix_picklist_for_v_list_odd) == 2:
    fix_char_for_v_list_even = fix_picklist_for_v_list_even[0]
    fix_char_for_v_list_odd = "!"
#片方が1文字ならば、そうでない方は2文字目を使う。
elif len(fix_picklist_for_v_list_even) == 2 and len(fix_picklist_for_v_list_odd) != 2:
    fix_char_for_v_list_even = fix_picklist_for_v_list_even[0]
    fix_char_for_v_list_odd = fix_picklist_for_v_list_odd[2]
elif len(fix_picklist_for_v_list_even) != 2 and len(fix_picklist_for_v_list_odd) == 2:
    fix_char_for_v_list_even = fix_picklist_for_v_list_even[2]
    fix_char_for_v_list_odd = fix_picklist_for_v_list_odd[0]
#両方2文字あるなら、固定パターンの合計が多い方を採用
elif fix_picklist_for_v_list_even[1] + fix_picklist_for_v_list_odd[3] > fix_picklist_for_v_list_even[3]+fix_picklist_for_v_list_odd[1]:
    fix_char_for_v_list_even = fix_picklist_for_v_list_even[0]
    fix_char_for_v_list_odd = fix_picklist_for_v_list_odd[2]
elif fix_picklist_for_v_list_even[1] + fix_picklist_for_v_list_odd[3] <= fix_picklist_for_v_list_even[3] + fix_picklist_for_v_list_odd[1]:
    fix_char_for_v_list_even = fix_picklist_for_v_list_even[2]
    fix_char_for_v_list_odd = fix_picklist_for_v_list_odd[0]

#print(fix_char_for_v_list_odd)
#print(fix_char_for_v_list_even)
        
#書き換え要素数をカウント
ans_odd = n/2 - v_list_odd_counter[fix_char_for_v_list_odd]
ans_even = n/2 - v_list_even_counter[fix_char_for_v_list_even]
print(int(ans_odd + ans_even))

Submission status: Accepted

Solution code:
from collections import Counter

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

a = sorted(Counter(v[ ::2]).items(), key=lambda x: -x[1])
b = sorted(Counter(v[1::2]).items(), key=lambda x: -x[1])
def w(arr, index):
  arr = arr[index:] + arr[0:index]
  return sum(c for _, c in arr[1:])

if a[0][0] == b[0][0]:
  if len(a) == len(b) == 1:
    print(n // 2)
  else:
    print(min(w(a, 0) + w(b, 1), w(a, 1) + w(b, 0)))
else:
  print(w(a, 0) + w(b, 0))

Submission status: Accepted

Solution code:
n = int(input()) // 2
ns = list(map(int, input().split()))

a = [ns[i] for i in range(len(ns)) if i % 2 == 0]
b = [ns[i] for i in range(len(ns)) if i % 2 == 1]
#ca = sorted([(a.count(s), s) for s in sa])[::-1]
#cb = sorted([(b.count(s), s) for s in sb])[::-1]  # [(出た回数, num)]
# countが遅かったので
da = {}
db = {}
for s in a:
    da[s] = da.get(s, 0) + 1
for s in b:
    db[s] = db.get(s, 0) + 1
ca = sorted([(v, k) for k, v in da.items()])[::-1]
cb = sorted([(v, k) for k, v in db.items()])[::-1]

if ca[0][1] != cb[0][1]:
    print(n-ca[0][0] + n-cb[0][0])
else:
    if len(ca) == 1 and len(cb) == 1:
        print(n)
    elif len(ca) == 1 and len(cb) > 1:
        print(n-ca[0][0] + n-cb[1][0])
    elif len(ca) > 1 and len(cb) == 1:
        print(n-ca[1][0] + n-cb[0][0])
    else:
        print(min(n-ca[1][0] + n-cb[0][0], n-ca[0][0] + n-cb[1][0]))  # ?

Submission status: Accepted

Solution code:
import itertools

n = int(input())
vs = [int(s) for s in input().split(' ')]
vs_odd = vs[0::2]
vs_odd.sort()
vs_even = vs[1::2]
vs_even.sort()
odd_group = {0: 0}
even_group = {0: 0}

for k, g in itertools.groupby(vs_odd):
    odd_group[k] = len(list(g))
for k, g in itertools.groupby(vs_even):
    even_group[k] = len(list(g))

ret = len(vs)

odd_max = max(odd_group, key=odd_group.get)
odd_max_v = odd_group[odd_max]
even_max = max(even_group, key=even_group.get)
even_max_v = even_group[even_max]
if odd_max == even_max:
    del odd_group[even_max]
    del even_group[even_max]
    odd_max2 = max(odd_group, key=odd_group.get)
    odd_max2_v = odd_group[odd_max2]
    even_max2 = max(even_group, key=even_group.get)
    even_max2_v = even_group[even_max2]
    ret -= max(odd_max_v + even_max2_v, odd_max2_v + even_max_v)
else:
    ret -= (odd_max_v + even_max_v )
print(ret)

Submission status: Accepted

Solution code:
from collections import Counter
def c_nb(i):
  b = Counter(v[i::2]).most_common()+[(0,0)]
  return b
def c_rpl(a,x,y):
  r = a-o[x][1]-e[y][1]
  return r
n = int(input())
v = list(map(int,input().split()))
o = c_nb(0)
e = c_nb(1)
if o[0][0]!=e[0][0]:
  ans = c_rpl(n,0,0)
else:
  ans = min(c_rpl(n,0,1),c_rpl(n,1,0))
print(ans)

Submission status: Accepted

Solution code:
import collections

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

# 全要素が同じとき
if len(set(v)) == 1:
    print(len(v)//2)
    exit()

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

c_even = collections.Counter(v_even)
n_v_even_majority = c_even.most_common()[0][1]
c_odd = collections.Counter(v_odd)
n_v_odd_majority = c_odd.most_common()[0][1]

# 最頻要素が同じとき
if c_even.most_common()[0][0] == c_odd.most_common()[0][0]:
    if len(set(v_even)) == 1 & len(set(v_odd)) == 1:
        print(0)
    elif len(set(v_even)) > 1:
        tmp = n - c_even.most_common()[1][1] - n_v_odd_majority
        if len(set(v_odd)) > 1:
            tmp = min(tmp, n - n_v_even_majority - c_odd.most_common()[1][1])
        print(tmp)
else:
    print(n - n_v_even_majority - n_v_odd_majority)

Submission status: Accepted

Solution code:
def count_rewrite(v):
    ct = {}
    for x in v:
        if x in ct:
            ct[x] += 1
        else:
            ct[x] = 1
    total = 0
    for x in ct:
        total += ct[x]
    best = 0
    for x in ct:
        if ct[x] > best:
            best = ct[x]
            maxv = x
    del ct[maxv]
    second = 0
    for x in ct:
        if ct[x] > second:
            second = ct[x]
    return maxv, total - best, total - second


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

r1 = count_rewrite(v[::2])
r2 = count_rewrite(v[1::2])
if r1[0] != r2[0]:
    print(r1[1] + r2[1])
else:
    if r1[2] >= r2[2]:
        print(r1[1] + r2[2])
    else:
        print(r1[2] + r2[1])

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
v_1 = v[::2]
v_2 = v[1::2]
v_1_d = {}
v_2_d = {}
for i in v_1:
  if i in v_1_d:
    v_1_d[i] += 1
  else:
    v_1_d[i] = 1
    
for i in v_2:
  if i in v_2_d:
    v_2_d[i] += 1
  else:
    v_2_d[i] = 1
    
v_1_d = sorted(v_1_d.items(),key = lambda x: -x[1])
v_2_d = sorted(v_2_d.items(),key = lambda x: -x[1])


for i in v_2_d:
  if i[0] != v_1_d[0][0]:
    max_v_2 = v_1_d[0][1]+i[1]
    break
else:
  max_v_2 = v_1_d[0][1]
    
for i in v_1_d:
  if i[0] != v_2_d[0][0]:
    max_v_1 = v_2_d[0][1]+i[1]
    break
else:
  max_v_1 = v_2_d[0][1]
print(n-max(max_v_1,max_v_2))