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:
_ = input()
seq = list(map(int,input().split()))
 
seq1 = seq[::2]
seq2 = seq[1::2]
 
if len(set(seq)) == 1:
    print(min(len(seq1),len(seq2)))
    exit()
    
def count_dictonary(seq):
    ret_dict={}
    for item in seq:
        if item not in ret_dict:
            ret_dict[item] = 1
        else:
            ret_dict[item]+=1
    return ret_dict
 
seq_dict1 = sorted(count_dictonary(seq1).items(),key=lambda x:x[1],reverse=True)
seq_dict2 = sorted(count_dictonary(seq2).items(),key=lambda x:x[1],reverse=True)
 
min_change=len(seq)
for i1, count1 in seq_dict1:
    ch1 = len(seq1) - count1
    if ch1 > min_change:
        continue
    for i2, count2 in seq_dict2:
      ch2 = len(seq2) - count2
      #print(i1,i2,ch1,ch2,ch1+ch2)
      change_ = ch1+ch2
      if(min_change > ch1+ch2):
        min_change = ch1+ch2
            
print(min_change)

Submission status: Time Limit Exceeded

Solution code:
_ = input()
seq = list(map(int,input().split()))

seq1 = seq[::2]
seq2 = seq[1::2]

if len(set(seq)) == 1:
    print(min(len(seq1),len(seq2)))
    exit()
    
def count_dictonary(seq):
    ret_dict={}
    for item in seq:
        if item not in ret_dict:
            ret_dict[item] = 1
        else:
            ret_dict[item]+=1
    return ret_dict

seq_dict1 = count_dictonary(seq1)
seq_dict2 = count_dictonary(seq2)

min_change=len(seq)
for i1 in seq_dict1:
    for i2 in seq_dict2:
        ch1 = len(seq1) - seq_dict1[i1]
        ch2 = len(seq2) - seq_dict2[i2]
        #print(i1,i2,ch1,ch2,ch1+ch2)
        change_ = ch1+ch2
        if(min_change > ch1+ch2):
            min_change = ch1+ch2
            
print(min_change)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python

import numpy as np

n = int(input())

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

v1 = v[0::2]
v2 = v[1::2]

v1_n = np.unique(v1)
v2_n = np.unique(v2)
v1_cnt = np.array([len(v1[v1==n]) for n in v1_n])
v2_cnt = np.array([len(v2[v2==n]) for n in v2_n])

v1_n = v1_n[np.argsort(v1_cnt)[::-1]]
v2_n = v2_n[np.argsort(v2_cnt)[::-1]]
v1_cnt.sort()
v2_cnt.sort()
v1_cnt = np.append(v1_cnt[::-1], 0)
v2_cnt = np.append(v2_cnt[::-1], 0)

if v1_n[0] != v2_n[0]:
    print(n - v1_cnt[0] - v2_cnt[0])
else:
    n1 = n - v1_cnt[0] - v2_cnt[1]
    n2 = n - v1_cnt[1] - v2_cnt[0]
    print(min(n1, n2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = [int(i) for i in input().split()]
if len(set(v)) == 1:
    print(int(n / 2))
else:
    v1 = v[0::2]
    v2 = v[1::2]
    v1b = set(v1)
    stop1 = len(v1b)
    A1 = []
    for i in v1b:
        num = v1.count(i)
        A1.append(num)
    A1.sort()
    ans1 = sum(A1[:stop1-1])
    v2b = set(v2)
    stop2 = len(v2b)
    A2 = []
    for t in v2b:
        num = v2.count(t)
        A2.append(num)
    A2.sort()
    ans2 = sum(A2[:stop2-1])
    print(ans1 + ans2)

Submission status: Time Limit Exceeded

Solution code:
def make_unique(list_):
	set_ = set()
	return [x for x in list_ if x not in set_ and not set_.add(x)]

n = int(input())
v = [int(c) for c in input().split()]
vo = v[::2]
ve = v[1::2]
# set(vo) is heavy
counts_o = [(elm, vo.count(elm)) for elm in make_unique(vo)]
counts_e = [(elm, ve.count(elm)) for elm in make_unique(ve)]
counts_o.append((-1, 0))
counts_e.append((-2, 0))
counts_o.sort(key=lambda x:x[1])
counts_e.sort(key=lambda x:x[1])
if counts_o[-1][0] != counts_e[-1][0]:
	print(n - counts_o[-1][1] - counts_e[-1][1])
else:
	res1 = n - counts_o[-1][1] - counts_e[-2][1]
	res2 = n - counts_o[-2][1] - counts_e[-1][1]
	print(min(res1, res2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = [int(c) for c in input().split()]
vo = v[::2]
ve = v[1::2]
counts_o = [(elm, vo.count(elm)) for elm in set(vo)]
counts_e = [(elm, ve.count(elm)) for elm in set(ve)]
counts_o.append((-1, 0))
counts_e.append((-2, 0))
counts_o.sort(key=lambda x:x[1])
counts_e.sort(key=lambda x:x[1])
if counts_o[-1][0] != counts_e[-1][0]:
	print(n - counts_o[-1][1] - counts_e[-1][1])
else:
	res1 = n - counts_o[-1][1] - counts_e[-2][1]
	res2 = n - counts_o[-2][1] - counts_e[-1][1]
	print(min(res1, res2))

Submission status: Time Limit Exceeded

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

li2 = li[::2]
li3 = li[1::2]
max2,max22,max3,max33 = [0,0,0,0]

for i in li2:
    a = li2.count(i)
    if a > max2:
        max22 = max2
        max2 = a
        b = i
    elif a > max22 and i != b:
        max22 = a

for i in li3:
    a = li3.count(i)
    if a > max3:
        max33 = max3
        max3 = a
        c = i
    elif a > max33 and i != c:
        max33 = a

if b == c:
    print(max(n-max22-max3,n-max2-max33))
else:
    print(n-max2-max3)

Submission status: Time Limit Exceeded

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

li2 = li[::2]
li3 = li[1::2]
max2 = 0
max22 = 0
max3 = 0
max33 = 0

for i in range(int(n/2)):
    a = li2[i]
    count = 0
    for j in range(int(n/2)):
        if li2[j] == a:
            count += 1
    if count > max2:
        max22 = max2
        max2 = count
        b = a
    elif count > max22 and a != b:
        max22 = count


for i in range(int(n/2)):
    a = li3[i]
    count = 0
    for j in range(int(n/2)):
        if li3[j] == a:
            count += 1
    if count > max3:
        max33 = max3
        max3 = count
        c = a
    elif count > max33 and a != c:
        max33 = count

if b == c:
    print(max(n-max22-max3,n-max2-max33))
else:
    print(n-max2-max3)

Submission status: Time Limit Exceeded

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

kisuSL=[]
kisuKL=[]
kisuKL2=[]

gusuSL=[]
gusuKL=[]
gusuKL2=[]

index =0
while index < len(listV):
    if index%2 ==0:
        if listV[index] in gusuSL:
            index2 = gusuSL.index(listV[index])
            gusuKL[index2]+=1
            gusuKL2[index2] += 1
        else:
            gusuSL.append(listV[index])
            gusuKL.append(1)
            gusuKL2.append(1)
    else:
        if listV[index] in kisuSL:
            index2 = kisuSL.index(listV[index])
            kisuKL[index2]+=1
            kisuKL2[index2] += 1
        else:
            kisuSL.append(listV[index])
            kisuKL.append(1)
            kisuKL2.append(1)
    index+=1

kisuMax = max(kisuKL)
gusuMax = max(gusuKL)
kMaxI = kisuKL.index(kisuMax)
gMaxI = gusuKL.index(gusuMax)

if kisuSL[kMaxI]!=gusuSL[gMaxI]:
    print(len(listV)-kisuMax-gusuMax)
else:
    if len(kisuSL)==1 and len(gusuSL)==1:
        print(len(listV)//2)
    else:
        kisuKL2.sort()
        gusuKL2.sort()
        kisuMax2 = kisuKL2[len(kisuKL2)-2]
        gusuMax2 = gusuKL2[len(gusuKL2)-2]
        if kisuMax + gusuMax2 > gusuMax + kisuMax2:
            print(len(listV)-kisuMax-gusuMax2)
        else:
            print(len(listV)-gusuMax-kisuMax2)

Submission status: Time Limit Exceeded

Solution code:
"""
C - /\/\/\/
"""
import collections
import numpy as np

n = int(input())

value = []
for part in input().split():
    value.append(int(part))

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

#print(odd)
#print(even)

odd_set = collections.Counter(odd)
even_set = collections.Counter(even)

odd_cnt = np.array([[i, odd_set[i]] for i in odd_set])
even_cnt = np.array([[i, even_set[i]] for i in even_set])

#print(even_cnt)

# 数字
#print(even_cnt[:, 0])
# 個数
#print(even_cnt[:, 1])

# 個数優先でソート
odd_sort_index = np.argsort(odd_cnt[:, 1])
even_sort_index = np.argsort(even_cnt[:, 1])

#print(even_sort_index)

odd_max = 0
even_max = 0
# 数字が一緒でなく、個数が最大を求める

for odd_i in odd_sort_index:
    for even_i in even_sort_index:
        
        odd_n = odd_cnt[odd_i, 0]
        even_n = even_cnt[even_i, 0]

        if odd_n != even_n:
            odd_max = max(odd_max, odd_cnt[odd_i, 1])
            even_max = max(even_max, even_cnt[even_i, 1])

if odd_max == 0 and even_max == 0:
    print(n // 2)
else:
    #print(odd_max)
    #print(even_max)
    print(n - odd_max - even_max)

Submission status: Time Limit Exceeded

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

l1=[]
l2=[]

for i in range(n):
    if i%2==1:
        l1.append(l[i])
    else:
        l2.append(l[i])

ans=len(l1)+len(l2)

l1cnt=0
l1cnt2=0
l1ind=0


for i in set(l1):
    cnt = l1.count(i)
    if cnt>l1cnt:
        l1ind=i
        l1cnt=cnt
    elif cnt>l1cnt2:
        l1cnt2=cnt

l2cnt=0
l2cnt2=0
l2ind=0

for i in set(l2):
    cnt = l2.count(i)
    if cnt>l2cnt:
        l2ind=i
        l2cnt=cnt
    elif cnt>l2cnt2:
        l2cnt2=cnt

if l1ind!=l2ind:
    ans -= l1cnt + l2cnt
else:
    ans -= max(l1cnt+l2cnt2,l1cnt2+l2cnt)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
Odd = []
OddCount = []
Even = []
EvenCount = []
list = input().split(" ")

for i in range(n):
    if i%2==1:
        x = int(list[i])
        if x in Odd:
            OddCount[Odd.index(x)] += 1
        else:
            OddCount.append(1)
            Odd.append(x)
    else:
        x = int(list[i])
        if x in Even:
            EvenCount[Even.index(x)] += 1
        else:
            EvenCount.append(1)
            Even.append(x)

O1c = max(OddCount)
O1n = Odd[OddCount.index(O1c)]
Odd[OddCount.index(O1c)] = 0
OddCount[OddCount.index(O1c)] = 0

E1c = max(EvenCount)
E1n = Even[EvenCount.index(E1c)]
Even[EvenCount.index(E1c)] = 0
EvenCount[EvenCount.index(E1c)] = 0

if O1c > E1c:
    if O1n != E1n:
        print(n-O1c-E1c)
    else:
        print(n-O1c-max(EvenCount))
elif O1c < E1c:
    if O1n != E1n:
        print(n-O1c-E1c)
    else:
        print(n-max(OddCount)-E1c)
else:
    if O1n == E1n:
        if max(EvenCount) > max(OddCount):
            print(n-O1c-max(EvenCount))
        else:
            print(n-max(OddCount)-E1c)
    else:
        print(n-O1c-E1c)

Submission status: Time Limit Exceeded

Solution code:
from numpy import argsort
def count_change(vs):
  v_list, count_list = [],[]
  for v in list(set(vs)):
    count = len([1 for vi in vs if vi !=v])
    v_list.append(v)
    count_list.append(count)
  return v_list, count_list

n = int(input())
vs = list(map(int, input().split()))
v_odd = vs[0::2]
v_even = vs[1::2]
odd_list, odd_count = count_change(v_odd)
even_list, even_count = count_change(v_even)

o_order = argsort(odd_count)[::-1]
e_order = argsort(even_count)[::-1]
o, e = 0, 0
if odd_list[o_order[0]] == even_list[e_order[0]]:
  if len(odd_list) == 1 and len(even_list)  == 1:
    print(len(v_even))
  elif odd_count[1] + even_count[0] > odd_count[0] + even_count[1]:
    e = 1
    print(odd_count[o] + even_count[e])
  else:
    o = 1
    print(odd_count[o] + even_count[e])
else:
  print(odd_count[o] + even_count[e])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
Odd = []
OddCount = []
Even = []
EvenCount = []
list = input()

for i in range(n):
    if i%2==1:
        x = int(list.split(" ")[i])
        if x in Odd:
            OddCount[Odd.index(x)] += 1
        else:
            OddCount.append(1)
            Odd.append(x)
    else:
        x = int(list.split(" ")[i])
        if x in Even:
            EvenCount[Even.index(x)] += 1
        else:
            EvenCount.append(1)
            Even.append(x)

O1c = max(OddCount)
O1n = Odd[OddCount.index(O1c)]
Odd[OddCount.index(O1c)] = 0
OddCount[OddCount.index(O1c)] = 0

E1c = max(EvenCount)
E1n = Even[EvenCount.index(E1c)]
Even[EvenCount.index(E1c)] = 0
EvenCount[EvenCount.index(E1c)] = 0

if O1c > E1c:
    if O1n != E1n:
        print(n-O1c-E1c)
    else:
        print(n-O1c-max(EvenCount))
elif O1c < E1c:
    if O1n != E1n:
        print(n-O1c-E1c)
    else:
        print(n-max(OddCount)-E1c)
else:
    if O1n == E1n:
        if max(EvenCount) > max(OddCount):
            print(n-O1c-max(EvenCount))
        else:
            print(n-max(OddCount)-E1c)
    else:
        print(n-O1c-E1c)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
OddList = []
EvenList = []
list = input()

for i in range(n):
    if i%2==1:
        OddList.append(int(list.split(" ")[i]))
    else:
        EvenList.append(int(list.split(" ")[i]))

Odd = collections.Counter(OddList)
Even = collections.Counter(EvenList)

if (Odd.most_common(1)[0][0] == Even.most_common(1)[0][0]):
    if (len(Odd) == 1)and(len(Even) == 1):
        print(len(OddList))
    elif (len(Odd) == 1)and(len(Even) > 1):
        print(len(EvenList) - Even.most_common(2)[1][0])
    elif (len(Odd) > 1)and(len(Even) == 1):
        print(len(OddList) - Odd.most_common(2)[1][0])
    else:
        if Odd.most_common(2)[1][1] >= Even.most_common(2)[1][1]:
            print(len(OddList)*2 - Odd.most_common(2)[1][1] - Even.most_common(2)[0][1])
        else:
            print(len(OddList)*2 - Odd.most_common(2)[0][1] - Even.most_common(2)[1][1])
else:
    print(len(OddList)*2-Odd.most_common(1)[0][1]-Even.most_common(1)[0][1])

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
OddList = []
EvenList = []
list = input()

for i in range(n):
    if i%2==1:
        OddList.append(int(list.split(" ")[i]))
    else:
        EvenList.append(int(list.split(" ")[i]))

Odd = collections.Counter(OddList)
Even = collections.Counter(EvenList)

if (Odd.most_common()[0][0] == Even.most_common()[0][0]):
    if (len(Odd) == 1)and(len(Even) == 1):
        print(len(OddList))
    elif (len(Odd) == 1)and(len(Even) > 1):
        print(len(EvenList) - Even.most_common()[1][0])
    elif (len(Odd) > 1)and(len(Even) == 1):
        print(len(OddList) - Odd.most_common()[1][0])
    else:
        if Odd.most_common()[1][1] >= Even.most_common()[1][1]:
            print(len(OddList) - Odd.most_common()[1][1] + len(EvenList) - Even.most_common()[0][1])
        else:
            print(len(OddList) - Odd.most_common()[0][1] + len(EvenList) - Even.most_common()[1][1])
else:
    print(len(OddList)+len(EvenList)-Odd.most_common()[0][1]-Even.most_common()[0][1])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict


def solve():
    n = int(input())
    v = [int(i) for i in input().split()]
    d1 = defaultdict(int)
    d2 = defaultdict(int)
    for i in range(n):
        if i % 2 == 0:
            d1[v[i]] += 1
        else:
            d2[v[i]] += 1
    rd1 = defaultdict(list)
    rd2 = defaultdict(list)
    for k, v in d1.items():
        rd1[v].append(k)
    for k, v in d2.items():
        rd2[v].append(k)
    a1 = [i for i in sorted(rd1.keys(), reverse=True)]
    a2 = [i for i in sorted(rd2.keys(), reverse=True)]
    ans = n
    la1 = len(a1) - 1
    la2 = len(a2) - 1
    for h in range(3):
        for i in range(3):
            aa1 = rd1[a1[min(h, la1)]]
            aa2 = rd2[a2[min(i, la2)]]
            for j in aa1:
                for k in aa2:
                    if j != k:
                        ans = min(
                            ans, (n // 2) - a1[min(h, la1)] + (n // 2) - a2[min(i, la2)])
                    else:
                        ans = min(ans, (n // 2) - a1[min(h, la1)] + (n // 2) - a2[min(i, la2)]
                                  + min(a1[min(h, la1)], a2[min(i, la2)]))
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter

n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]

a_even = a[::2]
even = len(a_even)
a_even_count = [[0] * 2 for j in range(even)]
a_e_c_unique = []

a_odd = a[1::2]
odd = len(a_odd)
a_odd_count = [[0] * 2 for j in range(odd)]
a_o_c_unique = []

ans = 0

for i in range(even):
    x = a_even[i]
    y = a_even.count(a_even[i])
    a_even_count[i][0] = x
    a_even_count[i][1] = y
    
for line in a_even_count:
    if line not in a_e_c_unique:
        a_e_c_unique.append(line)
a_e_c_unique.sort(key = itemgetter(1))

for i in range(odd):
    x = a_odd[i]
    y = a_odd.count(a_odd[i])
    a_odd_count[i][0] = x
    a_odd_count[i][1] = y
    

for line in a_odd_count:
    if line not in a_o_c_unique:
        a_o_c_unique.append(line)
a_o_c_unique.sort(key = itemgetter(1))

if a_e_c_unique[-1][0] == a_o_c_unique[-1][0]:
    if len(a_e_c_unique) == 1 and len(a_o_c_unique) == 1:
        ans = a_e_c_unique[-1][1]
        print(ans)
        exit()
    elif len(a_e_c_unique) == 1:
        a_e_c_unique.pop(-1)
        a_o_c_unique.pop(-2)
    elif len(a_o_c_unique) == 1:
        a_e_c_unique.pop(-2)
        a_o_c_unique.pop(-1)
    elif a_e_c_unique[-2][1] >= a_o_c_unique[-2][1]:
        a_e_c_unique.pop(-1)
        a_o_c_unique.pop(-2)
    else:
        a_e_c_unique.pop(-2)
        a_o_c_unique.pop(-1)
else:
    a_e_c_unique.pop(-1)
    a_o_c_unique.pop(-1)
    
ans_e = []
for i in range(len(a_e_c_unique)):
    ans_e.append(a_e_c_unique[i][1])
    ans += sum(ans_e)
    
ans_o = []
for i in range(len(a_o_c_unique)):
    ans_o.append(a_o_c_unique[i][1])
    ans += sum(ans_o)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter

n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]

a_even = a[::2]  # 偶数のリストに分割
even = len(a_even)
a_even_count = [[0] * 2 for j in range(even)]
a_e_c_unique = []

a_odd = a[1::2]
odd = len(a_odd)
a_odd_count = [[0] * 2 for j in range(odd)]
a_o_c_unique = []

ans = 0

for i in range(even):
    x = a_even[i]
    y = a_even.count(a_even[i])
    a_even_count[i][0] = x
    a_even_count[i][1] = y
    
for line in a_even_count:
    if line not in a_e_c_unique:
        a_e_c_unique.append(line)
a_e_c_unique.sort(key = itemgetter(1))

for i in range(odd):
    x = a_odd[i]
    y = a_odd.count(a_odd[i])
    a_odd_count[i][0] = x
    a_odd_count[i][1] = y
    

for line in a_odd_count:
    if line not in a_o_c_unique:
        a_o_c_unique.append(line)
a_o_c_unique.sort(key = itemgetter(1))

if a_e_c_unique[-1][0] == a_o_c_unique[-1][0]:
    if len(a_e_c_unique) == 1 and len(a_o_c_unique) == 1:
        ans = a_e_c_unique[-1][1]
        print(ans)
        exit()
    elif len(a_e_c_unique) == 1:
        a_e_c_unique.pop(-1)
        a_o_c_unique.pop(-2)
    elif len(a_o_c_unique) == 1:
        a_e_c_unique.pop(-2)
        a_o_c_unique.pop(-1)
    elif a_e_c_unique[-2][1] >= a_o_c_unique[-2][1]:
        a_e_c_unique.pop(-1)
        a_o_c_unique.pop(-2)
    else:
        a_e_c_unique.pop(-2)
        a_o_c_unique.pop(-1)
else:
    a_e_c_unique.pop(-1)
    a_o_c_unique.pop(-1)

for i in range(len(a_e_c_unique)):
    ans += a_e_c_unique[i][1]
for i in range(len(a_o_c_unique)):
    ans += a_o_c_unique[i][1]

print(ans)

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

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_copy = v_odd
v_even_copy = v_even

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[0])>v_odd_max_count:
		v_odd_max_count=v_odd.count(v_odd[0])
		v_odd_max_num=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)):
	if v_even.count(v_even[0])>v_even_max_count:
		v_even_max_count=v_even.count(v_even[0])
		v_even_max_num=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_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_copy=[i for i in v_odd_copy if not i == v_odd_max_num]
	v_even_copy=[i for i in v_even_copy if not i == v_even_max_num]
	for i in range(len(v_odd_copy)):
		if v_odd_copy.count(v_odd_copy[0])>v_odd_sec_count:
			v_odd_sec_count=v_odd.count(v_odd_copy[0])
			v_odd_copy=[i for i in v_odd_copy if not i == v_odd_copy[0]]
			if not v_odd_copy: break
	for i in range(len(v_even_copy)):
		if v_even_copy.count(v_even[0])>v_even_sec_count:
			v_even_sec_count=v_even_copy.count(v_even[0])
			v_even_copy=[i for i in v_even_copy if not i == v_even_copy[0]]
			if not v_even_copy: 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())
V = list(map(int,input().split(" ")))
e,o = [],[]

for i,v in enumerate(V):
    if i % 2 == 0:
        e.append(v)
    else:
        o.append(v)

e_ele = list(set(e))
o_ele = list(set(o))
e1,e2,e1c,e2c = 0,0,0,0
o1,o2,o1c,o2c = 0,0,0,0

for val in e_ele:
    valc = e.count(val)
    if valc > e1c:
        e1 = val
        e1c = valc
        continue
    if valc > e2c:
        e2 = val
        e2c = valc

for val in o_ele:
    valc = o.count(val)
    if valc > o1c:
        o1 = val
        o1c = valc
        continue
    if valc > o2c:
        o2 = val
        o2c = valc

ans = 0
elen = len(e)
olen = len(o)
if e1 != o1:
    ans = elen - e1c + olen - o1c
else:
    ans = elen - e1c + olen - o2c
    if ans > olen - o1c + elen - e2c:
        ans = olen - o1c + elen - e2c
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
V = list(map(int,input().split(" ")))
e,o = [],[]

for i,v in enumerate(V):
    if i % 2 == 0:
        e.append(v)
    else:
        o.append(v)

e_ele = list(set(e))
o_ele = list(set(o))
e1,e2 = None,None
o1,o2 = None,None

for val in e_ele:
    if e.count(val) > e.count(e1):
        e1 = val
        continue
    if e.count(val) > e.count(e2):
        e2 = val

for val in o_ele:
    if o.count(val) > o.count(o1):
        o1 = val
        continue
    if o.count(val) > o.count(o2):
        o2 = val

ans = 0
if e1 != o1:
    ans = len(e) - e.count(e1) + len(o) - o.count(o1)
else:
    ans = len(e) - e.count(e1) + len(o) - o.count(o2)
    if ans > len(o) - o.count(o1) + len(e) - e.count(e2):
        ans = len(o) - o.count(o1) + len(e) - e.count(e2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
MAX = 100001
li = input().split(" ")
el = []
ol = []
for i in range(n):
    if i % 2 == 0:
        el.append(int(li[i]))
    else:
        ol.append(int(li[i]))
n_el = len(list(set(el)))
n_ol = len(list(set(ol)))
if n_el == 1 and n_ol == 1 and el[0] != ol[0]:
    print(0)
elif n_el == 1 and n_ol == 1 and el[0] == ol[0]:
    print(len(ol))
else:
    kinds = sorted(list(set(li)))
    nums = [[0,0] for i in range(MAX)]
    for e in el:
        nums[e][0] += 1
    for o in ol:
        nums[o][1] += 1
    costs = [[0,0] for i in range(MAX)]
    for i in range(MAX):
        costs[i] = [n//2 - nums[i][0], n//2 - nums[i][1]]
    index_e = []
    index_o = []
    min_e = 1000000
    min_o = 1000000
    for i in range(MAX):
        if min_e > costs[i][0]:
            min_e = costs[i][0]
            index_e = [i]
        elif min_e == costs[i][0]:
            index_e.append(i)
        if min_o > costs[i][1]:
            min_o = costs[i][1]
            index_o = [i]
        elif min_o == costs[i][1]:
            index_o.append(i)
    ans_temp = 1000000
    
    # e が最小だったら
    temp_cost_e = []
    for j in range(MAX):
        temp_cost_e.append(costs[j][1])
    for i in index_e:
        temp_cost = copy.deepcopy(temp_cost_e)
        temp_cost[i] = 999999
        tete = min_e + min(temp_cost)
        if tete < ans_temp:
            ans_temp = tete
    temp_cost_o = []
    for j in range(MAX):
        temp_cost_o.append(costs[j][0])
    for i in index_o:
        temp_cost = copy.deepcopy(temp_cost_o)
        temp_cost[i] = 999999
        tete = min_o + min(temp_cost)
        if tete < ans_temp:
            ans_temp = tete
    print(ans_temp)

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:
    kisuuKosuL2.sort()
    gusuKosuL2.sort()
    kiIndex = kisuuKosuL.index(kisuuKosuL2[len(kisuuKosuL2)-1])
    guIndex = gusuKosuL.index(gusuKosuL2[len(gusuKosuL2)-1])
    if kisuuSyuL[kiIndex]!=gusuSyuL[guIndex]:
        noC=kisuuKosuL2[len(kisuuKosuL2)-1]+gusuKosuL2[len(gusuKosuL2)-1]
        print(n-noC)
    else:
        if kisuuKosuL2[len(kisuuKosuL2)-1]-kisuuKosuL2[len(kisuuKosuL2)-2] < gusuKosuL2[len(gusuKosuL2)-1]-gusuKosuL2[len(gusuKosuL2)-2]:
            noC=kisuuKosuL2[len(kisuuKosuL2)-2]+gusuKosuL2[len(gusuKosuL2)-1]
            print(n-noC)
        else:
            noC=gusuKosuL2[len(gusuKosuL2)-2]+kisuuKosuL2[len(kisuuKosuL2)-1]
            print(n-noC)

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
MAX = 100001
li = input().split(" ")
el = []
ol = []
for i in range(n):
    if i % 2 == 0:
        el.append(int(li[i]))
    else:
        ol.append(int(li[i]))
n_el = len(list(set(el)))
n_ol = len(list(set(ol)))
if n_el == 1 and n_ol == 1 and el[0] != ol[0]:
    print(0)
elif n_el == 1 and n_ol == 1 and el[0] == ol[0]:
    print(len(ol))
else:
    kinds = sorted(list(set(li)))
    nums = [[0,0] for i in range(MAX)]
    for e in el:
        nums[e][0] += 1
    for o in ol:
        nums[o][1] += 1
    costs = [[0,0] for i in range(MAX)]
    for i in range(MAX):
        costs[i] = [n//2 - nums[i][0], n//2 - nums[i][1]]
    index_e = []
    index_o = []
    min_e = 1000000
    min_o = 1000000
    for i in range(MAX):
        if min_e > costs[i][0]:
            min_e = costs[i][0]
            index_e = [i]
        elif min_e == costs[i][0]:
            index_e.append(i)
        if min_o > costs[i][1]:
            min_o = costs[i][1]
            index_o = [i]
        elif min_o == costs[i][1]:
            index_o.append(i)
    ans_temp = 1000000
    # e が最小だったら
    for i in index_e:
        temp_cost = []
        for j in range(MAX):
            temp_cost.append(costs[j][1])
        temp_cost[i] = 999999
        tete = min_e + min(temp_cost)
        if tete < ans_temp:
            ans_temp = tete
    for i in index_o:
        temp_cost = []
        for j in range(MAX):
            temp_cost.append(costs[j][0])
        temp_cost[i] = 999999
        tete = min_o + min(temp_cost)
        if tete < ans_temp:
            ans_temp = tete
    print(ans_temp)

Submission status: Time Limit Exceeded

Solution code:
input=raw_input
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)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
V = list(map(int,input().split()))
L1 = []
L2 = []
for i in range(n) :
    if i % 2 == 0 :
        L1.append(V[i])
    else :
        L2.append(V[i])
T1 = list(set(L1))
T2 = list(set(L2))
X1 = []
X2 = []
for j in range(len(T1)) :
    X1.append(L1.count(T1[j]))
for k in range(len(T2)) :
    X2.append(L2.count(T2[k]))

p = len(T1)
q = len(T2)
if p == 1 and q == 1 :
    if L1[0] != L2[0] :
        print(0)
    else :
        print(len(L2))
elif p != 1 and q == 1 :
    print(sum(X1)-max(X1))
elif p == 1 and q != 1 :
    print(sum(X2)-max(X2))
else :
    print(sum(X1)-max(X1)+sum(X2)-max(X2))

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_count[v_odd_count.index(v_odd_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)