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 = list(map(int,input().split()))
dict = {}

odd = list()
even = list()

od = list()
ed = list()

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

odd.sort()
even.sort()

o = 0
ob = 0
e = 0
eb = 0

onum = int()
onumb = int()
enum = int()
enumb = int()

for i in od:
    if odd.count(i) > o:
        ob = o
        o = odd.count(i)
        onumb = onum
        onum = i

for i in ed:
    if ed.count(i) > e:
        eb = e
        e = even.count(i)
        enumb = enum
        enum = i
else:
    #print(odd)
    #print(o)
    if onum == enum:
        if ob + e > o + eb:
            print(n-(ob+e))
        else:
            print(n-(o+eb))
        exit()
    if o == n/2 and e == n/2:
        print(0)

    else:
        print(n-(o+e))

Submission status: Time Limit Exceeded

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

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

v_odd_count=[]
v_even_count=[]
v_odd_num=[]
v_even_num=[]
for i in range(int(n/2)):
	v_odd_count.append(v_odd.count(v_odd[0]))
	v_odd_num.append(v_odd[0])
	v_odd=[i for i in v_odd if not i == v_odd[0]]
	if not v_odd: break
for i in range(int(n/2)):
	v_even_count.append(v_even.count(v_even[0]))
	v_even_num.append(v_even[0])
	v_even=[i for i in v_even if not i == v_even[0]]
	if not v_even: break

v_odd_max_count = max(v_odd_count)
v_even_max_count = max(v_even_count)

if v_odd_num[v_odd_count.index(v_odd_max_count)]!=v_even_num[v_even_count.index(v_even_max_count)]:
	print(n-v_odd_max_count-v_even_max_count)
else:
	del v_odd_num[v_odd_count.index(v_odd_max_count)]
	del v_odd_count[v_odd_count.index(v_odd_max_count)]
	del v_even_num[v_even_count.index(v_even_max_count)]
	del v_even_count[v_even_count.index(v_even_max_count)]
	if v_odd_count: v_odd_sec_count=max(v_odd_count)
	else: v_odd_sec_count=0
	if v_even_count: v_even_sec_count=max(v_even_count)
	else: v_even_sec_count=0
	if v_odd_max_count+v_even_sec_count>v_odd_sec_count+v_even_max_count: print(n-v_odd_max_count-v_even_sec_count)
	else: print(n-v_odd_sec_count-v_even_max_count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
numberL = list(map(int, input().split()))
 
kisuuSyuL=[]
kisuuKosuL=[]
kisuuKosuL2=[]
gusuSyuL=[]
gusuKosuL=[]
gusuKosuL2=[]
 
index=0
while index<n:
    if index==0:
        gusuSyuL.append(numberL[index])
        gusuKosuL.append(1)
        gusuKosuL2.append(1)
    elif index==1:
        kisuuSyuL.append(numberL[index])
        kisuuKosuL.append(1)
        kisuuKosuL2.append(1)
    else:
        if index%2==0:
            if numberL[index] in gusuSyuL:
                tarI = gusuSyuL.index(numberL[index])
                gusuKosuL[tarI]+=1
                gusuKosuL2[tarI]+=1
            else:
                gusuSyuL.append(numberL[index])
                gusuKosuL.append(1)
                gusuKosuL2.append(1)
        else:
            if numberL[index] in kisuuSyuL:
                tarI = kisuuSyuL.index(numberL[index])
                kisuuKosuL[tarI]+=1
                kisuuKosuL2[tarI]+=1
            else:
                kisuuSyuL.append(numberL[index])
                kisuuKosuL.append(1)
                kisuuKosuL2.append(1)
    index+=1

 
if len(kisuuSyuL)==1 and len(gusuSyuL)==1:
    if kisuuSyuL[0]==gusuSyuL[0]:
        print(int(n/2))
    else:
        print(0)
else:
    kiIndex = kisuuKosuL.index(max(kisuuKosuL))
    guIndex = gusuKosuL.index(max(gusuKosuL))
    if kisuuSyuL[kiIndex]!=gusuSyuL[guIndex]:
        noC=max(kisuuKosuL)+max(gusuKosuL)
        print(n-noC)
    else:
        kisuuKosuL2.sort()
        gusuKosuL2.sort()
        if max(kisuuKosuL)-kisuuKosuL2[len(kisuuKosuL2)-2] < max(gusuKosuL)-gusuKosuL2[len(gusuKosuL2)-2]:
            noC=kisuuKosuL2[len(kisuuKosuL2)-2]+max(gusuKosuL)
            print(n-noC)
        else:
            noC=gusuKosuL2[len(gusuKosuL2)-2]+max(kisuuKosuL)
            print(n-noC)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=list(map(int, input().split()))
v_even=v[::2]
v_odd=v[1::2]

import collections
odd=collections.Counter(v_odd).most_common()
even=collections.Counter(v_even).most_common()

odd_values, odd_counts = zip(*odd)
even_values, even_counts = zip(*even)
odd_v=list(odd_values)#値
odd_c=list(odd_counts)#出現回数
even_v=list(even_values)
even_c=list(even_counts)
odd_mode_list=odd_v[:len([i for i in odd_c if i ==max(odd_c)])]
even_mode_list=even_v[:len([i for i in even_c if i ==max(even_c)])]
if len(odd_mode_list)==1 and len(odd_mode_list)==len(even_mode_list) and odd_v[0]==even_v[0]:
    if len(odd_c)==1 and len(even_c)==1:
        print(n//2)
    else:
        print(n-max(odd_c)-max(max([i for i in odd_c if i  < max(odd_c)]),max([i for i in even_c if i  < max(even_c)])))
else:
    print(n-odd_c[0]-even_c[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int,input().split()))
o = v[0::2]
e = v[1::2]
o_s = list(set(o))
e_s = list(set(e))
o_c = [o.count(o_s[i]) for i in range(len(o_s))]
e_c = [e.count(e_s[i]) for i in range(len(e_s))]
if o == e:
  print(int(n/2))
else:
  print(n-max(o_c)-max(e_c))

Submission status: Time Limit Exceeded

Solution code:
n = input()
v = []
a = []
b = []
count = 0
v = map(int, raw_input().split())
for i in range(0, n, 2):
	a.append(v[i])
	b.append(v[i+1])
if(max(set(a), key=a.count) != max(set(b), key=b.count)):
	for i in range(0, n/2):
		if(a[i]!=max(set(a), key=a.count)):
			count += 1
		if(b[i] != max(set(b), key=b.count)):
			count += 1
else:
	count = n/2
	for i in range(0, n/2):
		if(a[i]!=max(set(a), key=a.count)):
			count -= 1
		if(b[i] != max(set(b), key=b.count)):
			count -= 1
print count

Submission status: Time Limit Exceeded

Solution code:
import random
question="C"
input=raw_input
if question=="A":
	t=input()
	t=t.replace("9","n")
	t=t.replace("1","o")
	t=t.replace("n","1")
	t=t.replace("o","9")
	print(t)
if question=="B":
	s=int(input())
	t=[111,222,333,444,555,666,777,888,999]
	t=filter(lambda x:x>=s,t)[0]
	print(t)
if question=="C":
	N=int(input())
	a=map(int,input().split())
	#a=[random.choice([1,2]) for i in xrange(N)]
	A=Amoto=a[0:][::2]
	B=Bmoto=a[1:][::2]
	def f(x):
		a=[(i, x.count(i)) for i in set(x)]
		a=sorted(a,key=lambda x:-x[1])
		return a
	A=f(A)
	B=f(B)
	if A[0][1]<B[0][1]:
		A,B=B,A
	A=A[0][0]
	B.append((0,0))
	B=filter(lambda x:(x[0]!=A),B)[0][0]
	print N-Amoto.count(A)-Bmoto.count(B)
if question=="D":
	pass

Submission status: Time Limit Exceeded

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

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

v_odd_count=[]
v_even_count=[]
v_odd_num=[]
v_even_num=[]
for i in range(int(n/2)):
	v_odd_count.append(v_odd.count(v_odd[0]))
	v_odd_num.append(v_odd[0])
	v_odd=[i for i in v_odd if not i == v_odd[0]]
	if not v_odd: break
for i in range(int(n/2)):
	v_even_count.append(v_even.count(v_even[0]))
	v_even_num.append(v_even[0])
	v_even=[i for i in v_even if not i == v_even[0]]
	if not v_even: break



if v_odd_num[v_odd_count.index(max(v_odd_count))]!=v_even_num[v_even_count.index(max(v_even_count))]:
	print(n-max(v_odd_count)-max(v_even_count))
else:
	v_odd_max_count = max(v_odd_count)
	v_even_max_count = max(v_even_count)
	del v_odd_num[v_odd_count.index(max(v_odd_count))]
	del v_odd_count[v_odd_count.index(max(v_odd_count))]
	del v_even_num[v_even_count.index(max(v_even_count))]
	del v_even_count[v_even_count.index(max(v_even_count))]
	if v_odd_count: v_odd_sec_count=max(v_odd_count)
	else: v_odd_sec_count=0
	if v_even_count: v_even_sec_count=max(v_even_count)
	else: v_even_sec_count=0
	if v_odd_max_count+v_even_sec_count>v_odd_sec_count+v_even_max_count: print(n-v_odd_max_count-v_even_sec_count)
	else: print(n-v_odd_sec_count-v_even_max_count)

Submission status: Time Limit Exceeded

Solution code:
n = input()
v = []
a = []
b = []
count = 0
v = map(int, raw_input().split())
for i in range(0, n, 2):
	a.append(v[i])
for i in range(1, n, 2):
	b.append(v[i])
if(max(set(a), key=a.count) != max(set(b), key=b.count)):
	for x in a:
		if(x!=max(set(a), key=a.count)):
			count += 1
	for x in b:
		if(x != max(set(b), key=b.count)):
			count += 1
else:
	count = n/2
	for x in a:
		if(x!=max(set(a), key=a.count)):
			count -= 1
	for x in b:
		if(x != max(set(b), key=b.count)):
			count -= 1
print count

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=list(map(int,input().split()))
w=['']*int(n/2)
x=['']*int(n/2)
wcntr=0
xcntr=0
wcnt=n+1
xcnt=n+1
for i in range(int(n/2)):
	w[i]=v[i*2]
	x[i]=v[i*2+1]
for k in range(int(n/2)):
	wcntr=max(wcntr,w.count(w[k]))
	if wcntr==w.count(w[k]):
		wcnt=w[k]
for l in range(int(n/2)):
	if x[l]!=wcnt:
		xcntr=max(xcntr,x.count(x[l]))
ans=n-(wcntr+xcntr)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
nums = list(map(int, input().split()))
array_0 = []
array_1 = []
for i in range(0, len(nums)):
    if i % 2 == 0:
        array_0.append(nums[i])
    else:
        array_1.append(nums[i])
c0 = collections.Counter(array_0)
c1 = collections.Counter(array_1)
count_0 = 0
count_1 = 0

if c0.most_common()[0][1] >= c1.most_common()[0][1]:
    if (len(c0) != 1):
        for i in range(1, len(c0)):
            # print(c0.most_common()[i][1])
            count_0 += c0.most_common()[i][1]
    if c0.most_common()[0][0] != c1.most_common()[0][0]:
        if (len(c1) != 1):
            for i in range(1, len(c1)):
                # print(c1.most_common()[i][1])
                count_1 += c1.most_common()[i][1]
    else:
        count_1 = c1.most_common()[0][1]
        for i in range(2, len(c1)):
            # print(c1.most_common()[i][1])
            count_1 += c1.most_common()[i][1]
else:
    if (len(c1) != 1):
        for i in range(1, len(c1)):
            # print(c0.most_common()[i][1])
            count_1 += c1.most_common()[i][1]
    if c0.most_common()[0][0] != c1.most_common()[0][0]:
        if (len(c0) != 1):
            for i in range(1, len(c0)):
                # print(c1.most_common()[i][1])
                count_0 += c0.most_common()[i][1]
    else:
        count_0 = c0.most_common()[0][1]
        for i in range(2, len(c0)):
            # print(c1.most_common()[i][1])
            count_0 += c0.most_common()[i][1]            
print(count_0 + count_1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(x) for x in input().split()]
A1 = [A[2*i] for i in range(int(n/2))]
A2 = [A[2*i+1] for i in range(int(n/2))]
A1_count = [A1.count(A1[i]) for i in range(len(A1))]
A2_count = [A2.count(A2[i]) for i in range(len(A2))]
x= len(A1)+len(A2) - max(A1_count) -max(A2_count)
if (x==0) and (max(A1) == max(A2)):
  print(len(A1))
else:
  print(x)

Submission status: Time Limit Exceeded

Solution code:
# AtCoder Beginner Contest 109 20180908
# Question:c
 
def main(nums, N):
	
	if nums.count(nums[0]) == N: 
		return int(N/2)
	else:
		
		odds = nums[0::2]
		max_odd1 = nums[0]
		max_odd2 = nums[0]
		max_odd1_count = 0
		max_odd2_count = 0
		
		evens = nums[1::2]
		max_even1 = nums[1]
		max_even2 = nums[1]
		max_even1_count = 0
		max_even2_count = 0
		
		for odd in range(len(odds)):
			od = odds.count(odds[odd]) 
			if od >= max_odd1_count:
				
				max_odd2_count = max_odd1_count
				max_odd2 = max_odd1
				
				max_odd1_count = od
				max_odd1 = odds[odd]
				
			ev = evens.count(evens[odd])
			
			if  ev >= max_even1_count:
				max_even2_count = max_even1_count
				max_even2 = max_even1
				
				max_even1_count = ev
				max_even1 = evens[odd]
				
		
		if max_odd1 != max_even1:
			return N - max_even1_count - max_odd1_count 
			
		elif max_odd2 != max_even1 :
			answer = N - max_even1_count - max_odd2_count
			if max_odd1 != max_even2:
				answer = min(answer, N - max_even2_count - max_odd1_count)
			return answer
			
		elif max_odd1 != max_even2:
			answer = N - max_even2_count - max_odd1_count
			if max_odd2 != max_even1:
				answer = min(answer, N - max_even1_count - max_odd2_count)
			return answer
			
			
		
N = int(input())
nums = input().split(' ')
print(main(nums, N))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(x) for x in input().split()]
A1 = [A[2*i] for i in range(int(n/2))]
A2 = [A[2*i+1] for i in range(int(n/2))]
A1_count = [A1.count(A1[i]) for i in range(len(A1))]
A2_count = [A2.count(A2[i]) for i in range(len(A2))]
x= len(A1)+len(A2) - max(A1_count) -max(A2_count)
if (len(A1)==max(A1_count)) and (len(A2) == max(A2_count)) and (A1[0] == A2[0]):
  print(len(A1))
else:
  print(x)

Submission status: Time Limit Exceeded

Solution code:
question="C"
input=raw_input
if question=="A":
	t=input()
	t=t.replace("9","n")
	t=t.replace("1","o")
	t=t.replace("n","1")
	t=t.replace("o","9")
	print(t)
if question=="B":
	s=int(input())
	t=[111,222,333,444,555,666,777,888,999]
	t=filter(lambda x:x>=s,t)[0]
	print(t)
if question=="C":
	N=int(input())
	a=map(int,input().split())
	A=Amoto=a[0:][::2]
	B=Bmoto=a[1:][::2]
	def f(x):
		a=[(i, x.count(i)) for i in set(x)]
		a=sorted(a,key=lambda x:-x[1])
		return a
	A=f(A)
	B=f(B)
	if A[0][1]<B[0][1]:
		A,B=B,A
	A=A[0][0]
	B.append((0,0))
	B=filter(lambda x:(x[0]!=A),B)[0][0]
	print N-Amoto.count(A)-Bmoto.count(B)
if question=="D":
	pass
if question=="E":
	pass

Submission status: Time Limit Exceeded

Solution code:
import collections

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

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

v_l_odd = []
v_l_even = []

for i in range(n):
    if i % 2 == 0:
        v_l_odd.append(v_l[i])
    else:
        v_l_even.append(v_l[i])
times = 1
while True:
    count_odd = collections.Counter(v_l_odd)
    mon = count_odd.most_common(times)[0][0]
    count_even = collections.Counter(v_l_even)
    men = count_even.most_common(times)[0][0]

    v_l_odd_diff = []
    v_l_even_diff = []

    for i in range(n):
        if i % 2 == 0:
            if v_l[i] != mon:
                v_l_odd_diff.append(v_l[i])
                v_l[i] = mon
        else:
            if v_l[i] != men:
                v_l_even_diff.append(v_l[i])
                v_l[i] = men

    if len(set(v_l)) != 1:
        print(len(v_l_odd_diff) + len(v_l_even_diff))
        exit()
    else:
        times = times + 1

Submission status: Time Limit Exceeded

Solution code:
def cal_uniq_num(input_list):
    unq_list=set(input_list)
    duplicate=[]
    for x in unq_list:
        duplicate.append(input_list.count(x))
    duplicate.sort()
    return sum(duplicate[:-1])

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

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

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

    ans = cal_uniq_num(odd)+cal_uniq_num(even)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
question="C"
input=raw_input
if question=="A":
	t=input()
	t=t.replace("9","n")
	t=t.replace("1","o")
	t=t.replace("n","1")
	t=t.replace("o","9")
	print(t)
if question=="B":
	s=int(input())
	t=[111,222,333,444,555,666,777,888,999]
	t=filter(lambda x:x>=s,t)[0]
	print(t)
if question=="C":
	input()
	a=map(int,input().split())
	A=Amoto=a[0:][::2]
	B=Bmoto=a[1:][::2]
	def f(x):
		a=[(i, x.count(i)) for i in set(x)]
		a=sorted(a,key=lambda x:-x[1])
		return a
	A=f(A)
	B=f(B)
	if A[0][1]<B[0][1]:
		A,B=B,A
	A=A[0][0]
	B.append((0,0))
	B=filter(lambda x:(x[0]!=A),B)[0][0]
	print sum(i!=A for i in Amoto) + sum(i!=B for i in Bmoto)
if question=="D":
	pass
if question=="E":
	pass

Submission status: Time Limit Exceeded

Solution code:
def cal_uniq_num(input_list):
    unq_list=set(input_list)
    duplicate=[]
    for x in unq_list:
        duplicate.append(input_list.count(x))
    duplicate.sort()
    return sum(duplicate[:-1])

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

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

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

    odd_unq = set(odd)
    odd_even = set(even)
    ans = cal_uniq_num(odd)+cal_uniq_num(even)
    print(ans)

Submission status: Time Limit Exceeded

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

Vdic = [{}, {}]
# 0: even 1: odd
for i in range(len(V_list)):
    v = V_list[i]

    tmp = (i + 1) % 2
    Vdic[tmp][v] = Vdic[tmp].get(v, 0) + 1

even = sorted(Vdic[0].values())
odd = sorted(Vdic[1].values())

ans_even = [(-1, 0)]
ans_odd = [(-2, 0)]
if(len(even) == 1):
    t = max(Vdic[0], key=Vdic[0].get)
    ans_even.append((t, even[0]))
else:
    n1 = even[-1]
    n2 = even[-2]

    ans_even = [(k, v) for k, v in Vdic[0].items() if v == n1 or v == n2]

if(len(odd) == 1):
    t = max(Vdic[1], key=Vdic[1].get)
    ans_odd.append((t, odd[0]))
else:
    n1 = odd[-1]
    n2 = odd[-2]

    ans_odd = [(k, v) for k, v in Vdic[1].items() if v == n1 or v == n2]

# print(ans_even, ans_odd)

ans = []
for ke, ve in ans_even:
    for ko, vo in ans_odd:
        if(ke != ko):
            ans.append(n - ve - vo)

print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
question="C"
input=raw_input
if question=="A":
	t=input()
	t=t.replace("9","n")
	t=t.replace("1","o")
	t=t.replace("n","1")
	t=t.replace("o","9")
	print(t)
if question=="B":
	s=int(input())
	t=[111,222,333,444,555,666,777,888,999]
	t=filter(lambda x:x>=s,t)[0]
	print(t)
if question=="C":
	input()
	a=input().split()
	A=Amoto=a[0:][::2]
	B=Bmoto=a[1:][::2]
	def f(x):
		a=[(i, x.count(i)) for i in set(x)]
		a=sorted(a,key=lambda x:-x[1])
		return a
	A=f(A)
	B=f(B)
	if A[0][1]<B[0][1]:
		A,B=B,A
	A=A[0][0]
	B.append(("X",0))
	B=filter(lambda x:(x[0]!=A),B)[0][0]
	print sum(i!=A for i in Amoto) + sum(i!=B for i in Bmoto)
if question=="D":
	pass
if question=="E":
	pass

Submission status: Time Limit Exceeded

Solution code:
# AtCoder Beginner Contest 109 20180908
# Question:c
 
def main(nums, N):
	
	if nums.count(nums[0]) == N: 
		return int(N/2)
	else:
		
		odds = nums[0::2]
		max_odd1 = nums[0]
		max_odd2 = nums[0]
		max_odd1_count = 0
		max_odd2_count = 0
		
		evens = nums[1::2]
		max_even1 = nums[1]
		max_even2 = nums[1]
		max_even1_count = 0
		max_even2_count = 0
		
		for odd in odds:
			if odd.count(odd) > max_odd1_count:
				
				max_odd2_count = max_odd1_count
				max_odd2 = max_odd1
				
				max_odd1_count = odds.count(odd)
				max_odd1 = odd
				
		for even in evens:
			if evens.count(even) > max_even1_count:
				
				max_even2_count = max_even1_count
				max_even2 = max_even1
				
				max_even1_count = evens.count(even)
				max_even1 = even
		
		if max_odd1 != max_even1:
			return N - max_even1_count - max_odd1_count 
			
		elif max_odd2 != max_even1 :
			answer = N - max_even1_count - max_odd2_count
			if max_odd1 != max_even2:
				answer = min(answer, N - max_even2_count - max_odd1_count)
			return answer
			
		elif max_odd1 != max_even2:
			answer = N - max_even2_count - max_odd1_count
			if max_odd2 != max_even1:
				answer = min(answer, N - max_even1_count - max_odd2_count)
			return answer
			
			
		
N = int(input())
nums = input().split(' ')
print(main(nums, N))

Submission status: Time Limit Exceeded

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

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

v_odd_count=[]
v_even_count=[]
v_odd_max_count=0
v_even_max_count=0
v_odd_max_num=0
v_even_max_num=0
for i in range(int(n/2)):
	if v_odd.count(v_odd[i])>v_odd_max_count:
		v_odd_max_count=v_odd.count(v_odd[i])
		v_odd_max_num=v_odd[i]
		if v_odd_max_count>n/2: break
for i in range(int(n/2)):
	if v_even.count(v_even[i])>v_even_max_count:
		v_even_max_count=v_even.count(v_even[i])
		v_even_max_num=v_even[i]
		if v_even_max_count>n/2: break

if v_odd_max_num!=v_even_max_num:
	print(n-v_odd_max_count-v_even_max_count)
else:
	v_odd_sec_count=0
	v_even_sec_count=0
	v_odd_sec_num=0
	v_even_sec_num=0
	v_odd=[i for i in v_odd if not i == v_odd_max_num]
	v_even=[i for i in v_even if not i == v_even_max_num]
	for i in range(len(v_odd)):
		if v_odd.count(v_odd[i])>v_odd_sec_count:
			v_odd_sec_count=v_odd.count(v_odd[i])
			v_odd_sec_num=v_odd[i]
			if v_odd_sec_count>n/2: break
	for i in range(len(v_even)):
		if v_even.count(v_even[i])>v_even_sec_count:
			v_even_sec_count=v_even.count(v_even[i])
			v_even_sec_num=v_even[i]
			if v_even_sec_count>n/2: break
	if v_odd_max_count+v_even_sec_count>v_odd_sec_count+v_even_max_count: print(n-v_odd_max_count-v_even_sec_count)
	else: print(n-v_odd_sec_count-v_even_max_count)

Submission status: Time Limit Exceeded

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

if log.count(log[0]) == len(log):
    print(n // 2)
    exit()

def replacement(f,s):
    cnt = 0
    rep = f
    for i in log:
        if i != rep:
           cnt += 1
        
        if rep == f:
            rep = s
        else:
            rep = f
    return cnt
        
        
result = []

for i,val in enumerate(log):
    if i+1 == len(log):
        continue
    
    f = val
    s = log[i+1]
    
    if f != s:
        result.append(replacement(f,s))

print(min(result))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
v_tmp = input().split()
v = [int(_) for _ in v_tmp]
v_count = Counter(v_tmp)
# print(v_count)
if len(v_count) == 1:
    print(n//2)
else:
    # print("hoge")
    v_keys = list(v_count.keys())
    tmps = []
    # print(v_keys)
    for _ in v_keys:
        for _2 in v_keys:
            tmp = []
            # print(_, _2)
            if _ == _2:
                continue
            else:
                for i in range(n):
                    if i % 2 == 0:
                        tmp.append(_)
                    else:
                        tmp.append(_2)
            # print("tmp", tmp)
            if tmp == []:
                continue
            else:
                tmps.append(tmp)
    sols = []
    for _ in tmps:
        sol_tmp = 0
        for i in range(n):
            if _[i] != v_tmp[i]:
                sol_tmp += 1
        sols.append(sol_tmp)
    print(min(sols))

Submission status: Time Limit Exceeded

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

v1 = []
v2 = []
count_v1 = [0]
count_v2 = [0]
for i in range(int(n / 2)):
    v1.append(v[2 * i])
    v2.append(v[2 * i + 1])

for i in range(int(n / 2)):
    count_v1.append(v1.count(v1[i]))
    count_v2.append(v2.count(v2[i]))

max_v1 = max(count_v1)
max_v2 = max(count_v2)

if v1[count_v1.index(max_v1)] == v2[count_v2.index(max_v2)]:
    for j1 in range(max_v1):
        count_v1.remove(max_v1)
    max2_v1 = max(count_v1)

    for j2 in range(max_v2):
        count_v2.remove(max_v2)
    max2_v2 = max(count_v2)

    if max2_v1 > max2_v2:
        max_v1 = max2_v1
    else:
        max_v2 = max2_v2

print(n - max_v1 - max_v2)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
vo = [v[i] for i in range(n) if i%2 == 0]
ve = [v[i] for i in range(n) if i%2 == 1]
vo_uni = list(set(vo))
ve_uni = list(set(ve))
vo_cnt = [vo.count(x) for x in vo_uni]
ve_cnt = [ve.count(x) for x in ve_uni]
if vo_uni == ve_uni and len(vo_uni) == 1:
    ans = n//2
else:
    ans = n - max(vo_cnt) - max(ve_cnt)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=list(map(int,input().split()))
m=int(n/2)
w=['']*m
x=['']*m
wcntr=0
xcntr=0
wcnt=n+1
xcnt=n+1
for i in range(m):
	w[i]=v[i*2]
	x[i]=v[i*2+1]
for k in range(m):
	wcntr=max(wcntr,w.count(w[k]))
	if wcntr==w.count(w[k]):
		wcnt=w[k]
for l in range(m):
	if x[l]!=wcnt:
		xcntr=max(xcntr,x.count(x[l]))
ans=n-(wcntr+xcntr)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(b) for b in input().split()]
k1 = a[::2]
k2 = a[1::2]

items1 = [b for b in set(k1)]
counts1 = [k1.count(b) for b in items1]
items2 = [b for b in set(k2)]
counts2 = [k2.count(b) for b in items2]
mcount1 = max(counts1)
cc1 = counts1.copy()
cc2 = counts2.copy()
cc1.remove(mcount1)
try:
    mcb = max(cc1)
except:
    mcb = 0

maxind1 = counts1.index(mcount1)
max1 = items1[maxind1]

mcount2 = max(counts2) 
cc2.remove(mcount2)
try:
    mc2b = max(cc2)
except:
    mc2b = 0

maxind2 = counts2.index(mcount2)
max2 = items2[maxind2]
#print(mcount1, max1)
#print(mcount2, max2)
if max1 == max2:
    print(n - mcount1 - max(mcb,mc2b))
else:
    print(n - mcount1 - mcount2)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import itertools


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

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

max_hit = 0
a_nums = [v[i] for i in range(0, len(v), 2)]
a_count = Counter(a_nums)
b_nums = [v[i] for i in range(1, len(v), 2)]
b_count = Counter(b_nums)
vals = [None, None]

for i, j in itertools.product(set(a_nums), set(b_nums)):
    if i == j:
        continue

    if a_count[i] + b_count[j] > max_hit:
        max_hit = a_count[i] + b_count[j]
        vals[0] = i
        vals[1] = j

fix = 0
for i, x in enumerate(v):
    if x != vals[i % 2]:
        fix += 1
print(fix)

Submission status: Time Limit Exceeded

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


def replacement(f,s):
    cnt = 0
    rep = f
    for i in log:
        if i != rep:
           cnt += 1
        
        if rep == f:
            rep = s
        else:
            rep = f
    return cnt
        
        
result = []

for i,val in enumerate(log):
    if i+1 == len(log):
        continue
    f = val
    s = log[i+1]
    result.append(replacement(f,s))


if sum(result) == 0:
    print(n //2)
else:
    print(min(result))

Submission status: Time Limit Exceeded

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

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

former = []
latter = []
counter = 0
for node in v:
    if(counter % 2 == 0):
        former.append(node)
    else:
        latter.append(node)
    counter += 1

forlen = len(former)
latlen = len(latter)

forfreq = [0] * forlen
latfreq = [0] * latlen

for i in range(forlen):
    for j in range(forlen):
        if(former[i] == former[j]):
            forfreq[i] += 1

for i in range(latlen):
    for j in range(latlen):
        if(latter[i] == latter[j]):
            latfreq[i] += 1

forfreqidx = 0
latfreqidx = 0

for i in range(forlen):
    if(forfreq[i] > forfreq[forfreqidx]):
        forfreqidx = i

for i in range(latlen):
    if(latfreq[i] > latfreq[latfreqidx]):
        latfreqidx = i

if(former[forfreqidx] == latter[latfreqidx]):
    for1st = forfreq[forfreqidx]
    forfreq[forfreqidx] = 0

    forfreqidx2 = 0
    for i in range(forlen):
        if (forfreq[i] > forfreq[forfreqidx2]):
            forfreqidx2 = i

    for2nd = forfreq[forfreqidx2]

    lat1st = latfreq[latfreqidx]
    latfreq[latfreqidx] = 0

    latfreqidx2 = 0
    for i in range(latlen):
        if (latfreq[i] > latfreq[latfreqidx2]):
            latfreqidx2 = i

    lat2nd = latfreq[latfreqidx2]

    if(for1st - for2nd >= lat1st - lat2nd):
        latfreqidx = latfreqidx2
    else:
        forfreqidx = forfreqidx2


# 最頻値の出現回数
forval = forfreq[forfreqidx]
latval = latfreq[latfreqidx]

print((forlen - forval) + (latlen - latval))

Submission status: Time Limit Exceeded

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])
    elif i%2 == 1:
        v2.append(v[i])
d1 = {}
for i in range(len(v1)):
    d1.update({v1[i]:v1.count(v1[i])})
d2 = {}
for i in range(len(v2)):
    d2.update({v2[i]:v2.count(v2[i])})
v11 = []
v22 = []
for i,j in d1.items():
    v11.append(j)
for i,j in d2.items():
    v22.append(j)
if v.count(v[0]) == n:
    print(int(n/2))
else:    
    print(n-max(v11)-max(v22))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left as bl
from bisect import bisect_right as br
n = int(input())
v = list(map(int, input().split()))
ev = []
od = []
for i in range(n):
    if i % 2 == 0:
        ev.append(v[i])
    else:
        od.append(v[i])
ev.sort()
od.sort()
m = 0
oo = [[0, 0]]
ii = [[0, 0]]
while True:
    oo.append([ev[m], br(ev, ev[m], 0, n//2)-bl(ev, ev[m], 0, n//2)])
    m += ev.count(ev[m])
    if m >= n//2:
        break
m = 0
while True:
    ii.append([od[m], br(od, od[m], 0, n//2)-bl(od, od[m], 0, n//2)])
    m += od.count(od[m])
    if m >= n//2:
        break
ii.sort(key=lambda x: x[1], reverse=True)
oo.sort(key=lambda x: x[1], reverse=True)
if ii[0][0] != oo[0][0]:
    print(n - oo[0][1] - ii[0][1])
else:
    print(min(n - oo[1][1] - ii[0][1], n - oo[0][1] - ii[1][1]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = list(map(int,input().split()))
A = []
B = []
Q = [0,0]
R = [0,0]
S = [0,0]
T = [0,0]
J = 0
K = 0
for i in range(n):
  if (i%2 == 0):
    A.append(L[i])
  else:
    B.append(L[i])
A.sort()
B.sort()
for j in range(len(A)):
  J = A.count(A[j])
  if (J >= Q[0]):
    del Q[0]
    Q.insert(0,J)
    J = 0
    del R[0]
    R.insert(0,A[j])
    j += J
  elif (Q[0] >= J >= Q[1]):
    del Q[1]
    Q.append(J)
    del R[1]
    R.append(A[j])
    j += J
  else:
    j += J
for k in range(len(B)):
  K = B.count(B[k])
  if (K >= S[0]):
    del S[0]
    S.insert(0,K)
    K = 0
    del T[0]
    T.insert(0,B[j])
    k += K
  elif (S[0] >= J >= S[1]):
    del S[1]
    S.append(K)
    del T[1]
    T.append(B[k])
    k += K
  else:
    k += K
if (R[0] != T[0]):
  print(n-Q[0]-S[0])
else:
  if (Q[0] >= S[0]):
    print(n-Q[0]-S[1])
  else:
    print(n-S[0]-Q[1])

Submission status: Time Limit Exceeded

Solution code:
question="C"
input=raw_input
if question=="A":
	t=input()
	t=t.replace("9","n")
	t=t.replace("1","o")
	t=t.replace("n","1")
	t=t.replace("o","9")
	print(t)
if question=="B":
	s=int(input())
	t=[111,222,333,444,555,666,777,888,999]
	t=filter(lambda x:x>=s,t)[0]
	print(t)
if question=="C":
	input()
	a=input().split()
	A=a[0:][::2]
	B=a[1:][::2]
	f=lambda x:sorted([[i,x.count(i)] for i in list(set(x))],key=lambda x:-x[1])
	A=f(A)
	B=f(B)
	if A[0][1]<B[0][1]:
		A,B=B,A
	t=filter(lambda x:(x[0]!=A[0][0]),B)
	if t:B.remove(t[0])
	del A[0]
	a=sum(i[1] for i in A)
	b=sum(i[1] for i in B)
	print(a+b)
if question=="D":
	pass
if question=="E":
	pass