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: Time Limit Exceeded

Solution code:
n = int(input())
v = [int(m) for m in input().split()]
a = v[::2]
b = v[1::2]
a = sorted(a)
b = sorted(b)
atop = a[0]
btop = b[0]

countera1 = a.count(a[0])
countera2 = 0
counterb1 = b.count(b[0])
counterb2 = 0

ia = 0
ib = 0

while len(a) > 0:
	i = 0
	while a[0] == a[i]:
		i += 1
		if i >= len(a):
			break
	acount = i
	if acount > countera1:
		countera1, countera2 = acount, countera1
		atop = a[0]
	elif acount > countera2 and ia != 0:
		countera2 = acount
	ia += 1
	a = a[acount:]

while len(b) > 0:
	j = 0
	while b[0] == b[j]:
		j += 1
		if j >= len(b):
			break
	bcount = j
	if bcount > counterb1:
		counterb1, counterb2 = bcount, counterb1
		btop = b[0]
	elif bcount > counterb2 and ib != 0:
		counterb2 = bcount
	b = b[bcount:]
	ib += 1
if atop == btop:
	if countera1 + counterb2 > counterb1 + countera2:
		print(n - (countera1 + counterb2))
	else:
		print(n - (countera2 + counterb1))
else:
	print(n - countera1 - counterb1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = [int(m) for m in input().split()]
a = []
b = []
for i in range(n):
	if i % 2 == 0:
		a.append(v[i])
	else:
		b.append(v[i])

a = sorted(a)
b = sorted(b)

atop = a[0]
btop = b[0]
atop2 = 0
btop2 = 0
acurrent = a[0]
bcurrent = b[0]
countera1 = a.count(a[0])
countera2 = 0
counterb1 = b.count(b[0])
counterb2 = 0

if a.count(a[0]) < a.count(a[1]):
	atop, atop2 = atop2, atop
	countera1, countera2 = countera2, countera1
if b.count(b[0]) < b.count(b[1]):
	btop, btop2 = btop2, btop
	counterb1, counterb2 = counterb2, counterb1
for i in range(n // 2):
	if a[i] != acurrent:
		acount = a.count(a[i])
		acurrent = a[i]
		if acount > countera1:
			countera1, countera2 = acount, countera1
			atop, atop2 = a[i], atop
		elif acount > countera2:
			countera2 = acount
			atop2 = a[i]
	if bcurrent != b[i]:
		bcurrent = b[i]
		bcount = b.count(b[i])
		if bcount > counterb1:
			counterb1, counterb2 = bcount, counterb1
			btop, btop2 = b[i], btop
		elif bcount > counterb2:
			counterb2 = bcount
			btop2 = b[i]
if atop == btop:
	if countera1 + counterb2 > counterb1 + countera2:
		print(n - (countera1 + counterb2))
	else:
		print(n - (countera2 + counterb1))
else:
	print(n - countera1 - counterb1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = [int(m) for m in input().split()]
a = []
b = []
for i in range(n):
	if i % 2 == 0:
		a.append(v[i])
	else:
		b.append(v[i])

a = sorted(a)
b = sorted(b)

atop = a[0]
btop = b[0]
atop2 = 0
btop2 = 0
acurrent = a[0]
bcurrent = b[0]
flaga = 0
flagb = 0
countera1 = a.count(a[0])
countera2 = 0
counterb1 = b.count(b[0])
counterb2 = 0

if a.count(a[0]) < a.count(a[1]):
	atop, atop2 = atop2, atop
	countera1, countera2 = countera2, countera1
if b.count(b[0]) < b.count(b[1]):
	btop, btop2 = btop2, btop
	counterb1, counterb2 = counterb2, counterb1
for i in range(n // 2):
	if a[i] != acurrent:
		acount = a.count(a[i])
		if acount > countera1:
			countera1, countera2 = acount, countera1
			atop, atop2 = a[i], atop
		elif acount > countera2:
			countera2 = acount
			atop2 = a[i]
	if bcurrent != b[i]:
		bcount = b.count(b[i])
		if bcount > counterb1:
			counterb1, counterb2 = bcount, counterb1
			btop, btop2 = b[i], btop
		elif bcount > counterb2:
			counterb2 = bcount
			btop2 = b[i]
if atop == btop:
	if countera1 + counterb2 > counterb1 + countera2:
		print(n - (countera1 + counterb2))
	else:
		print(n - (countera2 + counterb1))
else:
	print(n - countera1 - counterb1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
i_num = 0
two_0 = []
two_1 = []
t1 = 10 ** 5
ans = 0
for i in v:
    if i_num % 2 == 0:
        two_0.append(i)
    else:
        two_1.append(i)
    i_num += 1
c = 0
for i in two_0:
    if c < two_0.count(i):
        c = two_0.count(i)
        t0 = i
for i in two_0:
    if i != t0:
        ans += 1
c = 0
for i in two_1:
    if c < two_1.count(i) and i != t0:
        c = two_1.count(i)
        t1 = i
for i in two_1:
    if i != t1:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = [int(m) for m in input().split()]
a = []
b = []
for i in range(n):
	if i % 2 == 0:
		a.append(v[i])
	else:
		b.append(v[i])
countera1 = 0
countera2 = 0
counterb1 = 0
counterb2 = 0
atop = a[0]
btop = b[0]
atop2 = a[1]
btop2 = b[1]
for i in range(100000):
	acount = a.count(i)
	bcount = b.count(i)
	if acount > countera1:
		countera1, countera2 = acount, countera1
		atop, atop2 = i, atop
	elif acount > countera2:
		countera2 = acount
		atop2 = i
	if bcount > counterb1:
		counterb1, counterb2 = bcount, counterb1
		btop, btop2 = i, btop
	elif bcount > counterb2:
		counterb2 = bcount
		btop2 = i
if atop == btop:
	if countera1 + counterb2 > counterb1 + countera2:
		print(n - (countera1 + counterb2))
	else:
		print(n - (countera2 + counterb1))
else:
	print(n - countera1 - counterb1)

Submission status: Time Limit Exceeded

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

n = len(v)

ev = v[::2]
ov = v[1::2]

es = set(ev)
os = set(ov)

el = sorted([[s,ev.count(s)] for s in es],reverse=True,key = lambda x:x[1])
ol = sorted([[s,ov.count(s)] for s in os],reverse=True,key = lambda x:x[1])

el.append([0,0])
ol.append([0,0])
emax = el[0][1]
omax = ol[0][1]

dif = 0
if el[0][0] == ol[0][0]:
	edif = el[0][1] - el[1][1]
	odif = ol[0][1] - ol[1][1]
	dif = min(edif,odif)

print(n-emax-omax+dif)

Submission status: Time Limit Exceeded

Solution code:
from itertools import *
from collections import Counter

def f(v,m,s):
    cnt = 0
    for j in range(N):
        if j % 2 != 0:
            if v[j] != m:
                cnt += 1
        else:
            if v[j] != s:
                cnt += 1
    return cnt

N = int(input())
v = list(map(int,input().split()))
d = Counter(v).most_common()
#print(d)
try:
    second_common = d[1][0]
except:
    print(N//2)
    exit()
J = [i[0] for i in d]
p = list(product(J,J))
p = [i for i in p if i[0] != i[1]]
#print(p)
ans = float('inf')
for i in p:
    tmp = f(v,i[0],i[1])
    ans = min(ans,tmp)

print(ans)

Submission status: Time Limit Exceeded

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

a=v[::2]
b=v[1::2]
c={i for i in a}
d={i for i in b}

e=0
for i in c:
    ec=a.count(i)
    if e<ec:
        e=ec
        es=i
f=0
for i in d:
    fc=b.count(i)
    if f<fc and i!=es:
        f=fc
        fs=i

print(len(a)-e + len(b)-f)

Submission status: Time Limit Exceeded

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

a=v[::2]
b=v[1::2]
c={i for i in a}
d={i for i in b}

e=0
for i in c:
    if e<a.count(i):
        e=a.count(i)
        es=i
f=0
for i in d:
    if f<b.count(i) and i!=es:
        f=b.count(i)
        fs=i

print(len(a)-e + len(b)-f)

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
n = int(input())
vls = [int(x) for x in input().split()]
countls =[]
for i in range(100000):
    for j in range(100000):
        als = []
        for k in range(n):
            if n % 2 ==0:
                als.append(i)
            else:
                als.append(j)
        count = 0
        for l in range(n):
            if als[l] != vls[l]:
                count += 1
        countls.append(count)
        count = 0

print(min(countls))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
odd = [0 for _ in range(n // 2)]
odd_num = [0 for _ in range(n // 2)]
even = [0 for _ in range(n // 2)]
even_num = [0 for _ in range(n // 2)]
for i in range(n):
    if i % 2 == 0:
        if v[i] in odd:
            odd_num[odd.index(v[i])] += 1
        else:
            odd_num[i // 2] = 1 
        odd[i // 2] = v[i]
    else:
        if v[i] in even:
            even_num[even.index(v[i])] += 1
        else:
            even_num[i // 2] = 1 
        even[i // 2] = v[i]

odd_sort = sorted(odd_num, reverse = True)
o = odd[odd_num.index(odd_sort[0])]
even_sort = sorted(even_num, reverse = True)
e = even[even_num.index(even_sort[0])]
if o != e:
    ans = n - odd_sort[0] - even_sort[0]
else:
    if odd_sort[1] < even_sort[1]:
        ans = n - odd_sort[0] - even_sort[1]
    else:
        ans = n - odd_sort[1] - even_sort[0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[a[2*i] for i in range(int(n/2))]
c=[a[2*i-1] for i in range(1,int(n/2)+1)]
d=[b.count(i) for i in list(set(b))]
e=[c.count(i) for i in list(set(c))]
if list(set(b))[d.index(max(d))] != list(set(c))[e.index(max(e))]:
    print(n-max(d)-max(e))
else:
    f=max(d)
    g=max(e)
    del list(set(b))[d.index(max(d))]
    del list(set(c))[e.index(max(e))]
    d.remove(f)
    e.remove(g)
    if len(d)==0:
        h=0
    else:
        h=max(d)
    if len(e)==0:
        i=0
    else:
        i=max(e)
    print(n-max(f+i,g+h))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
def ans(l):
    max = 0
    b = set(l)
    if len(b)==1:
        return 0
    else:
        for i in b:
            s = l.count(i)
            if s>max:
                max = s
        return n/2 - max
if len(set(a)) == 1:
    p = int(n/2)
else:
    p = int(ans(a[::2]) + ans(a[1::2]))
print(p)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
def ans(l):
    max = 0
    b = set(l)
    for i in b:
        s = l.count(i)
        if s>max:
            max = s
    return n/2 - max
if len(set(a)) == 1:
    p = int(n/2)
else:
    p = int(ans(a[::2]) + ans(a[1::2]))
print(p)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
def ans(l):
    max = 0
    b = set(l)
    for i in b:
        s = l.count(i)
        if s>max:
            max = s
    return n/2 - max
if a.count(a[0])==n:
    p = int(n/2)
else:
    p = int(ans(a[::2]) + ans(a[1::2]))
print(p)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
def ans(l):
    max = 0
    for i in l:
        s = l.count(i)
        if s>max:
            max = s
    return n/2 - max
if a.count(a[0])==n:
    p = int(n/2)
else:
    p = int(ans(a[::2]) + ans(a[1::2]))
print(p)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=[int(i) for i in input().split()]
v_even=[vi for vi in v[::2]]
v_odd=[vi for vi in v[1::2]]
def same(v):
    count=[]
    v_set=set(v)
    for vi in v_set:
        count.append([vi, v.count(vi)])
    count=sorted(count, key = lambda k:k[1])
    return count
e1=len(v_even)-same(v_even)[-1][1]
o1=len(v_odd)-same(v_odd)[-1][1]
e2=len(v_even)-same(v_even)[-2][1] if len(set(v_even))>1 else len(v_even)
o2=len(v_odd)-same(v_odd)[-2][1] if len(set(v_odd))>1 else len(v_odd)
if same(v_even)[-1][0]==same(v_odd)[-1][0]:
    print(min(e1+o2, e2+o1))
else:
    print(e1+o1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
V = list(map(int,input().split()))
if len(set(V))==1:
  print(int(n/2))
else:
  even = V[0::2]
  odd = V[1::2]
  even_count = []
  for char in set(even):
    even_count.append(even.count(char))
  odd_count = []
  for char in set(odd):
    odd_count.append(odd.count(char))
  ans = sum(even_count)-max(even_count)+sum(odd_count)-max(odd_count)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
V = list(map(int,input().split()))
if len(set(V))==1:
  print(int(n/2))
else:
  even = [V[2*i] for i in range(int(n/2))]
  odd = [V[2*i+1] for i in range(int(n/2))]
  even_count = []
  for char in set(even):
    even_count.append(even.count(char))
  odd_count = []
  for char in set(odd):
    odd_count.append(odd.count(char))
  ans = sum(even_count)-max(even_count)+sum(odd_count)-max(odd_count)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
arr=input().split(" ")
arr_s=set(arr)
#print(arr)
a=0
most_e=0
most_o=0
arr_e=[0 for i in range(int(n/2))]
arr_o=[0 for i in range(int(n/2))]
for i in range(int(n/2)):
    arr_e[i]=arr[a]
    arr_o[i]=arr[a+1]
    a+=2
#print(arr_e,arr_o)
arr_es=set(arr_e)
arr_os=set(arr_o)
#print(arr_es,arr_os)

for i in arr_es:
    cnt_e=arr_e.count(i)
    if cnt_e>most_e:
        most_e=cnt_e
        if most_e>=(n/2-most_e)/2:
            break
for i in arr_os:    
    cnt_o=arr_o.count(i)
    if cnt_o>most_o:
        most_o=cnt_o
        if most_o>=(n/2-most_o)/2:
            break
#print(most_e,most_o)
if len(arr_s)==1:
    print(int(n/2))
else:
    print(n-most_e-most_o)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
arr=input().split(" ")
arr_s=set(arr)
#print(arr)
a=0
most_e=0
most_o=0
arr_e=[0 for i in range(int(n/2))]
arr_o=[0 for i in range(int(n/2))]
for i in range(int(n/2)):
    arr_e[i]=arr[a]
    arr_o[i]=arr[a+1]
    a+=2
#print(arr_e,arr_o)
arr_es=set(arr_e)
arr_os=set(arr_o)
#print(arr_es,arr_os)

for i in arr_es:
    cnt_e=arr_e.count(i)
    if cnt_e>most_e:
        most_e=cnt_e
        if cnt_e>=(n/2-cnt_e)/2:
            break
        
for i in arr_os:
    cnt_o=arr_o.count(i)
    if cnt_o>most_o:
        most_o=cnt_o
        if cnt_o>=(n/2-cnt_o)/2:
            break

#print(most_e,most_o)
if len(arr_s)==1:
    print(int(n/2))
else:
    print(n-most_e-most_o)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
arr=input().split(" ")
arr_s=set(arr)
#print(arr)
a=0
most_e=0
most_o=0
arr_e=[0 for i in range(int(n/2))]
arr_o=[0 for i in range(int(n/2))]
for i in range(int(n/2)):
    arr_e[i]=arr[a]
    arr_o[i]=arr[a+1]
    a+=2
#print(arr_e,arr_o)
arr_es=set(arr_e)
arr_os=set(arr_o)
#print(arr_es,arr_os)

for i in arr_es:
    cnt_e=arr_e.count(i)
    if cnt_e>most_e:
        most_e=cnt_e
        if cnt_e>=n/4:
            break
        
for i in arr_os:
    cnt_o=arr_o.count(i)
    if cnt_o>most_o:
        most_o=cnt_o
        if cnt_o>=n/4:
            break

#print(most_e,most_o)
if len(arr_s)==1:
    print(int(n/2))
else:
    print(n-most_e-most_o)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
arr=input().split(" ")
arr_s=set(arr)

#print(arr)
a=0
most_e=0
most_o=0
arr_e=[0 for i in range(int(n/2))]
arr_o=[0 for i in range(int(n/2))]
for i in range(int(n/2)):
    arr_e[i]=arr[a]
    arr_o[i]=arr[a+1]
    a+=2
#print(arr_e,arr_o)
arr_es=set(arr_e)
arr_os=set(arr_o)
#print(arr_es,arr_os)

for i in arr_es:
    cnt_e=arr_e.count(i)
    if cnt_e>most_e:
        most_e=cnt_e
        
for i in arr_os:
    cnt_o=arr_o.count(i)
    if cnt_o>most_o:
        most_o=cnt_o

#print(most_e,most_o)
if len(arr_s)==1:
    print(int(n/2))
else:
    print(n-most_e-most_o)

Submission status: Time Limit Exceeded

Solution code:
# /user/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import sys

n = int(sys.stdin.readline().rstrip())
v_s = list(map(int, sys.stdin.readline().rstrip().split()))

eval_type = {}
odd_type = {}

# v を eo_type(dict) に追加する
def add_v(eo_type, v):
  if v not in eo_type:
    eo_type[v] = 1
  else:
    eo_type[v] += 1

# eo_type(dict) の max value 2つの index を返す
def get_max2(eo_type):
  mx1, mx2 = [0] * 2
  for i in eo_type:
    if (mx1 not in eo_type) or (eo_type[mx1] < eo_type[i]):
      mx2 = mx1
      mx1 = i
    elif (mx2 not in eo_type) or (eo_type[mx2] < eo_type[i]):
      mx2 = i
  return([mx1, mx2])


def solve(idx_e, idx_o):
  if idx_e == 0 or idx_o == 0:
    return(sys.maxsize)
  elif idx_e == idx_o:
    return(min(n-eval_type[idx_e], n-odd_type[idx_o]))
  else:
    return(n - eval_type[idx_e] - odd_type[idx_o])


if __name__ == "__main__":
  for i in range(len(v_s)):
    if i % 2 == 0:
      add_v(eval_type, v_s[i])
    else:
      add_v(odd_type, v_s[i])
  
    ve1,ve2 = get_max2(eval_type)
    vo1,vo2 = get_max2(odd_type)
  
  print(min(solve(ve1,vo1), solve(ve1,vo2), solve(ve2,vo1)))

Submission status: Time Limit Exceeded

Solution code:
#cording: utf-8

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

count = 0

if len(list(set( v ))) == 1:
    count = int(N/2)
    print(count)

else:
    for i in range(N-2):
        if v[i] != v[i+2]:
            #v[i]とv[i+2]以降の要素v[j]のうち，多く含まれる方の出ないものを多く含まれるものにする
            List = []
            count_vi = 0
            count_vi2 = 0
            for j in range(i, N-2, 2):
                List.append(v[j])
            count_vi = List.count(v[i])
            count_vi2 = List.count(v[i+2])
            #ここまで
            if count_vi <= count_vi2:
                v[i] = v[i+2]
            if count_vi > count_vi2:
                v[i+2] = v[i]
            count += 1
    print(count)

Submission status: Time Limit Exceeded

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

va, ve = v[0::2], v[1::2]
add, even = va[0], ve[0]
for a in set(va):
  add = a if va.count(add) < va.count(a) else add
for e in set(ve):
  even = e if ve.count(even) < ve.count(e) else even

print(len(v)-va.count(add)-ve.count(even) if add!=even else len(v)-va.count(add))

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**7) 
 
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())
 
# h,w = li()
# ss = [ns() for _ in range(h)]

n = int(input())

vs = input().split(" ")
v_odd = []
v_even = []

i=0
for v in vs:
    if i % 2 == 0:
        v_even.append(v)
    else:
        v_odd.append(v)
    i = i + 1

max_count_odd = 0

m_odd = 0
m_even = 0
for ss in set(v_odd):
    count = v_odd.count(ss)
    if count > max_count_odd:
        max_count_odd = count
        m_odd = ss
# import pdb;pdb.set_trace() 
max_count_even = 0
for ss in set(v_even):
    count = v_even.count(ss)
    if count > max_count_even:
        max_count_even = count
        m_even = ss
# import pdb;pdb.set_trace() 
ans = len(v_even)-max_count_even  +  len(v_odd)-max_count_odd

m2_oddmax = 0
m2_evenmax = 0
import copy
# import pdb;pdb.set_trace()
if(m_even==m_odd):
    temp = set(v_odd)
    temp.remove(m_odd)
    m2_odd = 0
    # import pdb;pdb.set_trace()
    for ss in temp:
        count = v_odd.count(ss)
        if count > m2_oddmax:
            m2_oddmax = count
            m2_odd = ss
    temp = set(v_even)
    temp.remove(m_even)
    m2_even = 0
    for ss in temp:
        count = v_even.count(ss)
        if count > m2_evenmax:
            m2_evenmax = count
            m2_even = ss
    if m2_evenmax > m2_oddmax:
        # import pdb;pdb.set_trace()
        ans = len(v_even)-m2_evenmax + len(v_odd)-max_count_odd
    elif m2_evenmax <= m2_oddmax:
        # import pdb;pdb.set_trace()
        ans = len(v_even)-max_count_even + len(v_odd)-m2_oddmax
    if m2_even == 0 and m2_odd == 0:
        ans = int(n/2)
    # import pdb;pdb.set_trace()


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**7) 
 
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())
 
# h,w = li()
# ss = [ns() for _ in range(h)]

n = int(input())

vs = li()
v_odd = []
v_even = []

i=0
for v in vs:
    if i % 2 == 0:
        v_even.append(v)
    else:
        v_odd.append(v)
    i = i + 1

max_count_odd = 0

m_odd = 0
m_even = 0
for ss in set(v_odd):
    count = v_odd.count(ss)
    if count > max_count_odd:
        max_count_odd = count
        m_odd = ss
# import pdb;pdb.set_trace() 
max_count_even = 0
for ss in set(v_even):
    count = v_even.count(ss)
    if count > max_count_even:
        max_count_even = count
        m_even = ss
# import pdb;pdb.set_trace() 
ans = len(v_even)-max_count_even  +  len(v_odd)-max_count_odd

m2_oddmax = 0
m2_evenmax = 0
import copy
# import pdb;pdb.set_trace()
if(m_even==m_odd):
    temp = set(v_odd)
    temp.remove(m_odd)
    m2_odd = 0
    # import pdb;pdb.set_trace()
    for ss in temp:
        count = v_odd.count(ss)
        if count > m2_oddmax:
            m2_oddmax = count
            m2_odd = ss
    temp = set(v_even)
    temp.remove(m_even)
    m2_even = 0
    for ss in temp:
        count = v_even.count(ss)
        if count > m2_evenmax:
            m2_evenmax = count
            m2_even = ss
    if m2_evenmax > m2_oddmax:
        # import pdb;pdb.set_trace()
        ans = len(v_even)-m2_evenmax + len(v_odd)-max_count_odd
    elif m2_evenmax <= m2_oddmax:
        # import pdb;pdb.set_trace()
        ans = len(v_even)-max_count_even + len(v_odd)-m2_oddmax
    if m2_even == 0 and m2_odd == 0:
        ans = int(n/2)
    # import pdb;pdb.set_trace()


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**7) 
 
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())
 
# h,w = li()
# ss = [ns() for _ in range(h)]

n = int(input())

vs = li()
v_odd = []
v_even = []

i=0
for v in vs:
    if i % 2 == 0:
        v_even.append(v)
    else:
        v_odd.append(v)
    i = i + 1

max_count_odd = 0

m_odd = 0
m_even = 0
for ss in set(v_odd):
    count = v_odd.count(ss)
    if count > max_count_odd:
        max_count_odd = count
        m_odd = ss
# import pdb;pdb.set_trace() 
max_count_even = 0
for ss in set(v_even):
    count = v_even.count(ss)
    if count > max_count_even:
        max_count_even = count
        m_even = ss
# import pdb;pdb.set_trace() 
ans = len(v_even)-max_count_even  +  len(v_odd)-max_count_odd

m2_oddmax = 0
m2_evenmax = 0
import copy
# import pdb;pdb.set_trace()
if(m_even==m_odd):
    temp = list(filter((m_odd).__ne__, v_odd))
    m2_odd = 0
    for ss in set(temp):
        count = temp.count(ss)
        if count > m2_oddmax:
            m2_oddmax = count
            m2_odd = ss
    temp = list(filter((m_even).__ne__, v_even))
    m2_even = 0
    for ss in set(temp):
        count = temp.count(ss)
        if count > m2_evenmax:
            m2_evenmax = count
            m2_even = ss
    if m2_evenmax > m2_oddmax:
        # import pdb;pdb.set_trace()
        ans = len(v_even)-m2_evenmax + len(v_odd)-max_count_odd
    elif m2_evenmax <= m2_oddmax:
        # import pdb;pdb.set_trace()
        ans = len(v_even)-max_count_even + len(v_odd)-m2_oddmax
    if m2_even == 0 and m2_odd == 0:
        ans = int(n/2)
    # import pdb;pdb.set_trace()


print(ans)

Submission status: Time Limit Exceeded

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

even = []
odd = []

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

def count(list):
    max = 0
    for i in set(list):
        if list.count(i) >= max:
           max = list.count(i)
    return max

if len(set(v)) != 1:
    print(len(v) - count(even) - count(odd))
else:
    print(len(v)//2)

Submission status: Time Limit Exceeded

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

def my_sort(d):
    if len(d)==1:
        return list(d.keys())[0], '0', list(d.values())[0], 0
    k_0, k_1, v_0, v_1 = '0', '0', 0, 0
    for k,v in d.items():
        if v_0 < v:
            v_1 = v_0
            k_1 = k_0
            v_0 = v
            k_0 = k
        elif v_1 < v:
            v_1 = v
            k_1 = k
    return k_0, k_1, v_0, v_1


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

    v = V[2*i+1]
    if v in d1.keys():
        d1[v] += 1
    else:
        d1[v] = 1

k0_0, k0_1, v0_0, v0_1 = my_sort(d0)
k1_0, k1_1, v1_0, v1_1 = my_sort(d1)


if k0_0 != k1_0:
    a0 = sum(d0.values()) - v0_0
    a1 = sum(d1.values()) - v1_0
    print(a0 + a1)
else:
    if v0_0 <= v1_0:
        a0 = sum(d0.values()) - v0_1
        a1 = sum(d1.values()) - v1_0
    else:
        a0 = sum(d0.values()) - v0_0
        a1 = sum(d1.values()) - v1_1
    print(a0 + a1)

Submission status: Time Limit Exceeded

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

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

    v = V[2*i+1]
    if v in d1.keys():
        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:
    a0 = sum(d0.values()) - v0_0
    a1 = sum(d1.values()) - v1_0
    print(a0 + a1)
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_0:
        a0 = sum(d0.values()) - v0_1
        a1 = sum(d1.values()) - v1_0
    else:
        a0 = sum(d0.values()) - v0_0
        a1 = sum(d1.values()) - v1_1
    print(a0 + a1)

Submission status: Time Limit Exceeded

Solution code:
import copy
N = int(input())
v = list(map(int,input().split()))
values = list(set(v))
d = {}
for i in range(len(values)):
    number  = v.count(values[i])
    d[values[i]] = number
d  = sorted(d.items(), key=lambda x: x[1],reverse=True)

most_common = d[0][0]
#v_ = [i for i in v if i != most_common]
try:
    second_common = d[1][0]
except:
    second_common = most_common + 1
#print(v)
vv = copy.deepcopy(v)
cnt = 0
for j in range(N):
    if j % 2 != 0:
        if v[j] != most_common:
            #v[j] = most_common
            cnt += 1
    else:
        if v[j] != second_common:
            #v[j] = second_common
            cnt += 1
ccnt = 0
for j in range(N):
    if j % 2 != 0:
        if vv[j] != second_common:
            #vv[j] = second_common
            ccnt += 1
    else:
        if vv[j] != most_common:
            #vv[j] = most_common
            ccnt += 1
#print(v,cnt)
#print(vv,ccnt)
print(min(cnt,ccnt))

Submission status: Time Limit Exceeded

Solution code:
from statistics import mode
import copy
N = int(input())
v = list(map(int,input().split()))
values = list(set(v))
d = {}
for i in range(len(values)):
    number  = v.count(values[i])
    d[values[i]] = number
d  = sorted(d.items(), key=lambda x: x[1],reverse=True)

most_common = d[0][0]
#v_ = [i for i in v if i != most_common]
try:
    second_common = d[1][0]
except:
    second_common = most_common + 1
#print(v)
vv = copy.deepcopy(v)
cnt = 0
for j in range(N):
    if j % 2 != 0:
        if v[j] != most_common:
            v[j] = most_common
            cnt += 1
    else:
        if v[j] != second_common:
            v[j] = second_common
            cnt += 1
ccnt = 0
for j in range(N):
    if j % 2 != 0:
        if vv[j] != second_common:
            vv[j] = second_common
            ccnt += 1
    else:
        if vv[j] != most_common:
            vv[j] = most_common
            ccnt += 1
#print(v,cnt)
#print(vv,ccnt)
print(min(cnt,ccnt))

Submission status: Time Limit Exceeded

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

#print(n,v)
v0=[v[i] for i in range(n) if i%2==0]
v1=[v[i] for i in range(n) if i%2==1]
#print(v0,v1)

setv0=set(v0)
setv1=set(v1)

maxsetv0=0
for i in setv0:
  tmp=v0.count(i)
  if maxsetv0 < tmp:
    maxsetv0=tmp
    
maxsetv1=0    
for i in setv1:
  tmp=v1.count(i)
  if maxsetv1 < tmp:
    maxsetv1=tmp
#print(maxsetv1)

if len(set(v))==1:
  print(n//2)
else:
  print(len(v0)-maxsetv0+len(v1)-maxsetv1)

Submission status: Time Limit Exceeded

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

#print(n,v)
v0=[v[i] for i in range(n) if i%2==0]
v1=[v[i] for i in range(n) if i%2==1]
#print(v0,v1)

setv0=set(v0)
setv1=set(v1)

maxsetv0=0
for i in setv0:
  tmp=v0.count(i)
  if maxsetv0 < tmp:
    maxsetv0=tmp
    
maxsetv1=0    
for i in setv1:
  tmp=v1.count(i)
  if maxsetv1 < tmp:
    maxsetv1=tmp
#print(maxsetv1)

if len(set(v))==1:
  print(n//2)
else:
  print(len(v0)-maxsetv0+len(v1)-maxsetv1)

Submission status: Time Limit Exceeded

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

gu = []
ki = []
for i in range(n):
    if i%2:
        ki.append(li[i])
    else:
        gu.append(li[i])

ki_max = []
ki_s = list(set(ki))
for i in ki_s:
    ki_max.append(ki.count(i))

gu_max = []
gu_s = list(set(gu))
for i in gu_s:
    gu_max.append(gu.count(i))


if ki_s[ki_max.index(max(ki_max))] != gu_s[gu_max.index(max(gu_max))]:
    ans = len(ki)-max(ki_max) + len(gu)-max(gu_max)
else:
    ki = max(ki_max)
    gu = max(gu_max)

    ki_max.remove(ki)
    gu_max.remove(gu)

    if len(ki_max) == 0:
        ans = n-gu
    elif len(gu_max) == 0:
        ans = n-ki
    else:
        if max(ki_max) > max(gu_max):
            ans = n - max(ki_max) - gu
        else:
            ans = n - ki - max(gu_max)



print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
V=[int(i) for i in input().split()]

def separate(a):
    b,c=[],[]
    for i in range(len(a)):
        if i%2==1:
            b.append(a[i])
        else:
            c.append(a[i])
    return [b,c]

def appearGetter(a):
    b = list(set(a))
    c=[]
    for i in b:
        c.append([i,a.count(i)])
    return c
    
sep = separate(V)
oddApp=appearGetter(sep[0])
evenApp=appearGetter(sep[1])

oddApp.sort(key=lambda x:x[1])
evenApp.sort(key=lambda x:x[1])

ans=0
if oddApp[-1][0]!=evenApp[-1][0]:
    ans = len(V)-oddApp[-1][1]-evenApp[-1][1]
else:
    if len(oddApp)==1:
        oddApp.append([0,0])
    if len(evenApp)==1:
        evenApp.append([0,0])
    ans1 = len(V)-oddApp[-1][1]-evenApp[-2][1]
    ans2 = len(V)-oddApp[-2][1]-evenApp[-1][1]
    ans = min(ans1,ans2)

print(ans)