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:
import collections
import itertools
n = int(input())
v = list(map(int,input().split()))
#n = len(v)
c = collections.Counter(v).most_common()
if len(c) == 1:
    print(n//2)
    exit()
values, counts = zip(*c)
k = sorted(list(set(counts)),reverse=True)[:2]
pairs = []
for vcnt in c:
    val = vcnt[0]
    cnt = vcnt[1]
    #print(val,cnt)
    if cnt in k:
        pairs.append(val)
#print(pairs)
P = list(itertools.combinations(pairs,2))
#print(P)
ans = float('inf')
for pair in P:
    #print(pair)
    mc = pair[0]
    sc = pair[1]
    tmp = 0
    for i in range(n):
        if i % 2 == 0 and v[i] != mc:
            tmp += 1
        elif i % 2 == 1 and v[i] != sc:
            tmp += 1
    ttmp = 0
    for i in range(n):
        if i % 2 == 0 and v[i] != sc:
            ttmp += 1
        elif i % 2 == 1 and v[i] != mc:
            ttmp += 1

    ans = min(ans,min(tmp,ttmp))
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int,input().split()))
vo = v[::2]
ve = v[1::2]
vo_uni = set(vo)
vo_cnt_max = 0
vo_cnt_sec = 0
vo_cnt_max_dup = False
vo_max = vo[0]
ve_uni = set(ve)
ve_cnt_max = 0
ve_cnt_sec = 0
ve_cnt_max_dup = False
ve_max = ve[0]

for c in list(vo_uni):
    if vo.count(c) == vo_cnt_max:
        vo_cnt_sec = vo_cnt_max
        vo_cnt_max_dup = True
    elif vo.count(c) > vo_cnt_max:
        vo_cnt_sec = vo_cnt_max
        vo_cnt_max = vo.count(c)
        vo_max = c
        vo_cnt_max_dup = False
        
for c in list(ve_uni):
    if ve.count(c) == ve_cnt_max:
        ve_cnt_sec = ve_cnt_max
        ve_cnt_max_dup = True
    elif ve.count(c) > ve_cnt_max:
        ve_cnt_sec = ve_cnt_max
        ve_cnt_max = ve.count(c)
        ve_max = c
        ve_cnt_max_dup = False

ans = n -vo_cnt_max - ve_cnt_max
if not(vo_cnt_max_dup + ve_cnt_max_dup) and (vo_max == ve_max):
    ans = min((n-vo_cnt_max-ve_cnt_sec),(n-ve_cnt_max-vo_cnt_sec))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int,input().split()))
v_odd = []
v_even = []
for i in range(n//2):
    v_odd.append(v[2*i])
    v_even.append(v[2*i+1])
p1 = 0
p2 = 0
q1 = 0
q2 = 0
o_max = -1
e_max = -1
for i in range(n//2):
    a = v_odd[i]
    b = v_even[i]
    x = v_odd.count(a)
    y = v_even.count(b)
    if p1 <= x:
        if o_max != a:
            o_max = a
            p2 = p1
            p1 = x
    else:
        if x > p2:
            p2 = x
    if q1 <= y:
        if e_max != b:
            e_max = b
            q2 = q1
            q1 = y
    else:
        if y > q2:
            q2 = y
s = p1 + q1
if o_max == e_max:
    s = max(p1+q2,q1+p2)
ans = n-s
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int,input().split()))
v_odd = []
v_even = []
for i in range(n//2):
    v_odd.append(v[2*i])
    v_even.append(v[2*i+1])
p1 = 0
p2 = 0
q1 = 0
q2 = 0
o_max = -1
e_max = -1
for i in range(n//2):
    a = v_odd[i]
    b = v_even[i]
    x = v_odd.count(a)
    y = v_even.count(b)
    if p1 <= x:
        if o_max != a:
            o_max = a
            p2 = p1
            p1 = x
    else:
        if x > p2:
            p2 = x
    if q1 <= y:
        if e_max != b:
            e_max = b
            q2 = q1
            q1 = y
    else:
        if y > q2:
            q2 = y
s = p1 + q1
if p1 == q1:
    if o_max == e_max:
        s = max(p2,q2) + p1
elif p1 > q2:
    if o_max == e_max:
        s = p1 + q2
else: 
    if o_max == e_max:
        s = p2 + q1
ans = n - s
print(ans)

Submission status: Time Limit Exceeded

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

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

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

for k,v in one_count.items():
    one_count[k] = one_len-v
one_count[10**6]=one_len

two_len=len(two)
two_count={}

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

for k,v in two_count.items():
    two_count[k] = two_len-v
two_count[10**6]=two_len


mins = 1000000

for one_key, one_value in one_count.items():
    for two_key, two_value in two_count.items():
        if one_key != two_key:
            if (one_value + two_value) < mins:
                mins = one_value + two_value
print(mins)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    v = list(map(int,input().split()))
    o=[]
    e=[]
    checked=[]
    for i in v:
        if not i in checked:
            checked.append(i)
            c0=c1=0
            for j in range(n//2):
                if v[2*j]==i:
                    c0+=1
                if v[2*j+1]==i:
                    c1+=1
            if n%2==1:
                if v[n-1]==i:
                    c1+=1
            o.append([c0,i])
            e.append([c1,i])
    o.append([0,10**6])
    e.append([0,10**6])
    o.sort(reverse=True)
    e.sort(reverse=True)
    if o[0][1]!=e[0][1]:
        print(n-o[0][0]-e[0][0])
        return
    else:
        print(min(n-o[1][0]-e[0][0],n-o[0][0]-e[1][0]))
        return
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input()) // 2
ns = list(map(int, input().split()))
a = [ns[i] for i in range(len(ns)) if i % 2 == 0]
b = [ns[i] for i in range(len(ns)) if i % 2 == 1]
sa = set(a)
sb = set(b)
ca = sorted([(a.count(s), s) for s in sa])[::-1]
cb = sorted([(b.count(s), s) for s in sb])[::-1]  # [(出た回数, num)]
if ca[0][1] != cb[0][1]:
    print(n-ca[0][0] + n-cb[0][0])
else:
    if len(ca) == 1 and len(cb) == 1:
        print(n)
    elif len(ca) == 1:
        print(n-ca[0][0] + n-cb[1][0])
    else:
        print(n-ca[1][0] + n-cb[0][0])

Submission status: Time Limit Exceeded

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

cnt = [[0] * 2 for _ in range(4)]
for i in set(v1):
    tmp = v1.count(i)
    if cnt[0][0] <= tmp:
        cnt[0], cnt[1] = [tmp, i], cnt[0]
for i in set(v2):
    tmp = v2.count(i)
    if cnt[2][0] <= tmp:
        cnt[2], cnt[3] = [tmp, i], cnt[2]

if cnt[0][1] != cnt[2][1]:
    print(n - cnt[0][0] - cnt[2][0])
else:
    print(min(n - cnt[0][0] - cnt[3][0], n - cnt[1][0] - cnt[2][0]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
V = list(map(int,input().split()))
V1 = V[::2]
V2 = V[1::2]
V1_s = list(set(V1))
V2_s = list(set(V2))
S1 = len(V1_s)
S2 = len(V2_s)
if S1 == 1 and S2 == 1:
  if V1[0] != V2[0]:
    print(0)
  else:
    print(len(V) // 2)
else:
  tmp = 0
  ans = 0
  for i in range(S1):
    tmp = max(tmp,V1.count(V1_s[i]))
  ans += len(V1) - tmp
  tmp = 0
  for i in range(S2):
    tmp = max(tmp,V2.count(V2_s[i]))
  ans += len(V2) - tmp
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

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

count=0

a=[v[i] for i in range(0,n,2)] 
b=max(a,key=lambda x:a.count(x))
c=[v[i] for i in range(1,n,2)] 
d=max(c,key=lambda x:c.count(x))

if a == c:
    print(min(len(a),len(c)))
    exit()

for i in a:
    if i != b:
        count +=1

for j in c:
    if j != d:
        count +=1
        

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())

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

odds = []
evens = []

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

evens_c = Counter(evens).most_common()
odds_c = Counter(odds).most_common()

ans = n

fl = 0
for i in range(len(evens_c)):
    for j in range(len(odds_c)):
        try:
            if evens_c[i][0] != odds_c[j][0]:
                ans = min(ans, n - evens_c[i][1] - odds_c[j][1])
                fl = 1
        except:
            continue

if fl == 1:
    print(ans)
    exit()
print(n // 2)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = a[0::2]
b1 = sorted(b, key=lambda x: b.count(x))
c = a[1::2]
c1 = sorted(c, key=lambda x: c.count(x))

if b == c:
    print(n//2)
else:
    print(n - b1.count(b1[-1]) - c1.count(c1[-1]))

Submission status: Time Limit Exceeded

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

ans1 =  N//2
ans2 =  N//2
ansv1 = ""

for v in V[::2]:
    counter = 0
    for v_ in V[::2]:
        counter += 1 if v_ != v else 0
    if counter < N//4:
        ans1, ansv1 = [counter, v]
        break
    ans1, ansv1 = [counter, v] if counter < ans1 else [ans1, ansv1]

for v in V[1::2]:
    if v == ansv1:
        continue
    counter = 0
    for v_ in V[1::2]:
        counter += 1 if v_ != v else 0
    if counter < N//4:
        ans2 = counter
        break
    ans2 = min(ans2, counter)

print(ans1 + ans2)

Submission status: Time Limit Exceeded

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

ans1 =  N//2
ans2 =  N//2
ansv1 = ""

for v in V[::2]:
    counter = 0
    for v_ in V[::2]:
        counter += 1 if v_ != v else 0
    ans1, ansv1 = [counter, v] if counter < ans1 else [ans1, ansv1]

for v in V[1::2]:
    if v == ansv1:
        continue
    counter = 0
    for v_ in V[1::2]:
        counter += 1 if v_ != v else 0
    ans2 = min(ans2, counter)

print(ans1 + ans2)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
V = list(map(int, input().split()))
if len(set(V)) == 1:
    print(n//2)
else:
    A = V[::2]
    B = V[1::2]
    max_countA = max([A.count(a) for a in set(A)])
    max_countB = max([B.count(b) for b in set(B)])
    
    print(len(A)-max_countA + len(A)-max_countB)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

vvv_odd = vvv[::2]
vvv_even = vvv[1::2]

odd_vocab = set(vvv_odd)
even_vocab = set(vvv_even)


def calc_rewrite_number(vvv_each, vocab):
    max_count = -1
    for v in vocab:
        max_count = max(max_count, vvv_each.count(v))
    len_vvv_each = len(vvv_each)
    return len_vvv_each - max_count


# Calculation for odd and even elements in vvv
n_rewrite = 0
n_rewrite += calc_rewrite_number(vvv_odd, odd_vocab)
n_rewrite += calc_rewrite_number(vvv_even, even_vocab)
if len(odd_vocab & even_vocab) == 1:
    n_rewrite += len(vvv_odd)

# Output
print(n_rewrite)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = [int(i) for i in input().split()]
v_even = v[::2]
v_odd = v[1::2]
e_max = 0
e_max_ele = 0
for e in set(v_even):
    tmp = v_even.count(e)
    if e_max < tmp:
        e_max = tmp
        e_max_ele = e
ans1 = len(v_even) - e_max

o_max = 0
o_max_ele = 0
for o in set(v_odd):
    tmp = v_odd.count(o)
    if o_max < tmp:
        o_max = tmp
        o_max_ele = o
ans2 = len(v_odd) - o_max

if e_max_ele == o_max_ele:
    if e_max < o_max:
        ban = e_max_ele
        e_max = 0
        e_max_ele = 0
        for e in set(v_even):
            if e == ban:
                continue
            tmp = v_even.count(e)
            if e_max < tmp:
                e_max = tmp
                e_max_ele = e
        ans1 = len(v_even) - e_max
    else:
        ban = o_max_ele
        o_max = 0
        o_max_ele = 0
        for o in set(v_odd):
            if o == ban:
                continue
            tmp = v_odd.count(o)
            if o_max < tmp:
                o_max = tmp
                o_max_ele = o
        ans2 = len(v_odd) - o_max

print(ans1+ans2)

Submission status: Time Limit Exceeded

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

v1 = []
v2 = []
for i in range(0,n,2):
  v1.append(V[i])
for i in range(1,n,2):
  v2.append(V[i])
  
v1s = set(v1)
v2s = set(v2)

v1c = []
v2c = []
v1cc = []
v2cc = []
for i in list(v1s):
  v1c.append([i,v1.count(i)])
  v1cc.append(v1.count(i))
for i in list(v2s):
  v2c.append([i,v2.count(i)])
  v2cc.append(v2.count(i))
  
v1c.sort(key = lambda x:x[1],reverse=True)
v2c.sort(key = lambda x:x[1],reverse=True)
v1cc.sort(reverse=True)
v2cc.sort(reverse=True)

if v1c[0][0] == v2c[0][0]:
  print(min(sum(v1cc[1:])+sum([v2cc[0],sum(v2cc[2:])]),
            sum([v1cc[0],sum(v1cc[2:])])+sum(v2cc[1:])))
else:
  print(sum(v1cc[1:])+sum(v2cc[1:]))

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

n, = map(int, input().split())
data = getInputListInt()

param = []
tmpparam = []
onelist = []
twolist = []

tmp = 0
for i in range(n):
    if tmpparam.count(data[i]) == 0:
        param.append([data[i], 0, 0])
        tmpparam.append(data[i])

if len(param)==1:
    param.append([0, 0, 0])

for i in range(len(param)):
    for j in range(n):
        if j%2==0:
            if data[j]!=param[i][0]:
                param[i][1]+=1
        else:
            if data[j]!=param[i][0]:
                param[i][2]+=1

srtlist = sorted(param, key=lambda x:x[1])
onemin1 = [srtlist[0][0], srtlist[0][1]]
onemin2 = [srtlist[1][0], srtlist[1][1]]

srtlist = sorted(param, key=lambda x:x[2])
twomin1 = [srtlist[0][0], srtlist[0][2]]
twomin2 = [srtlist[1][0], srtlist[1][2]]

if onemin1[0] != twomin1[0]:
    print(onemin1[1]+twomin1[1])
else:
    print(min(onemin1[1]+twomin2[1], onemin2[1]+twomin1[1]))

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

n, = map(int, input().split())
data = getInputListInt()

param = []

tmp = 0
for i in range(n):
    if param.count(data[i])==0:
        param.append(data[i])

if len(param)==1:
    param.append(0)

ans = sys.maxsize

for i in range(len(param)):
    one = param[i]
    for j in range(len(param)):
        if i == j:
            continue

        two = param[j]
        cnt = 0
        for k in range(n):
            if k % 2 == 0:
                if data[k] != one:
                    cnt+=1
            else:
                if data[k] != two:
                    cnt+=1

        ans = min(ans, cnt)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

f = Counter(v[::2])
s = Counter(v[1::2])

ans = []

for i in f.keys():
    for j in s.keys():
        if i == j:
            continue
        else:
            ans.append(f[i] + s[j])

if ans:
    print(n - max(ans))
else:
    print(n // 2)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

f = collections.Counter([v[i] for i in range(n) if i % 2 == 0])
s = collections.Counter([v[i] for i in range(n) if i % 2 == 1])

ans = []

for i in f.keys():
    for j in s.keys():
        if i == j:
            continue
        else:
            ans.append(f[i] + s[j])

if ans:
    print(n - max(ans))
else:
    print(n // 2)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

f = collections.Counter([v[i] for i in range(n) if i % 2 == 0]).most_common()
s = collections.Counter([v[i] for i in range(n) if i % 2 == 1]).most_common()

ans = []

for i in f:
    for j in s:
        if i[0] == j[0]:
            pass
        else:
            ans.append(i[1] + j[1])

if ans:
    print(n - max(ans))
else:
    print(n // 2)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
V = list(map(int, input().split()))
A = V[::2]
B = V[1::2]
set_A = set(A)
set_B = set(B)
max_a = 0
max_b = 0

for a in set_A:
  cnt_a = A.count(a)
  max_a = max(max_a, cnt_a)
for b in set_B:
  cnt_b = B.count(b)
  max_b = max(max_b, cnt_b)
ans = (len(A) - max_a) + (len(B) - max_b)
if ans == 0 and A[0] == B[0]:
  ans = n // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
vs=[int(i) for i in input().split()]
vs1=[v for i,v in enumerate(vs) if i%2==0]
vs2=[v for i,v in enumerate(vs) if i%2==1]

ans=0
if len(list(set(vs)))==1:
  ans+=len(vs2)
if len(list(set(vs1)))!=1:
  tmp=[]
  for uniq in list(set(vs1)):
    tmp.append(n/2-vs1.count(uniq))
  ans+=min(tmp)
if len(list(set(vs2)))!=1:
  tmp=[]
  for uniq in list(set(vs2)):
    tmp.append(n/2-vs2.count(uniq))
  ans+=min(tmp)

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
v1 = []
v2 = []
ans = 0

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

mostCount1 = 0
mostChar1 = 0
dp1 = [None] * 100000

for i in range(n//2):
    count = 0
    if dp1[v1[i]] is not None:
        count = dp1[v1[i]]
    else:
        dp1[v1[i]] = v1.count(v1[i])
        count = dp1[v1[i]]
#    count = v1.count(v1[i])
    if count > mostCount1:
        mostCount1 = count
        mostChar1 = v1[i]
        if mostCount1 > n//2:
            break

mostCount2 = 0
mostChar2 = 0
dp2 = [None] * 100000

for i in range(n//2):
#    count = v2.count(v2[i])
    count = 0
    if dp2[v2[i]] is not None:
        count = dp2[v2[i]]
    else:
        dp2[v2[i]] = v2.count(v2[i])
        count = dp2[v2[i]]

    if count > mostCount2:
        mostCount2 = count
        mostChar2 = v2[i]
        if mostCount2 > n//2:
            break

if mostChar1 == mostChar2:
    ans = n//2
else:
    ans = n - mostCount1 - mostCount2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
v1 = []
v2 = []
ans = 0

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

mostCount1 = 0
mostChar1 = 0
dp1 = [None] * (n//2)

for i in range(n//2):
    count = 0
    if dp1[i] is not None:
        count = dp1[i]
    else:
        dp1[i] = v1.count(v1[i])
        count = dp1[i]
#    count = v1.count(v1[i])
    if count > mostCount1:
        mostCount1 = count
        mostChar1 = v1[i]
        if mostCount1 > n//2:
            break

mostCount2 = 0
mostChar2 = 0
dp2 = [None] * (n//2)

for i in range(n//2):
#    count = v2.count(v2[i])
    count = 0
    if dp2[i] is not None:
        count = dp2[i]
    else:
        dp2[i] = v2.count(v2[i])
        count = dp2[i]

    if count > mostCount2:
        mostCount2 = count
        mostChar2 = v2[i]
        if mostCount2 > n//2:
            break

if mostChar1 == mostChar2:
    ans = n//2
else:
    ans = n - mostCount1 - mostCount2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
v1 = []
v2 = []
ans = 0

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

mostCount1 = 0
mostChar1 = 0

for i in range(n//2):
    count = v1.count(v1[i])
    if count > mostCount1:
        mostCount1 = count
        mostChar1 = v1[i]
        if mostCount1 > n//2:
            break

mostCount2 = 0
mostChar2 = 0

for i in range(n//2):
    count = v2.count(v2[i])
    if count > mostCount2:
        mostCount2 = count
        mostChar2 = v2[i]
        if mostCount2 > n//2:
            break

if mostChar1 == mostChar2:
    ans = n//2
else:
    ans = n - mostCount1 - mostCount2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
v1 = []
v2 = []
ans = 0

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

mostCount1 = 0
mostChar1 = 0

for i in range(n//2):
    count = v1.count(v1[i])
    if count > mostCount1:
        mostCount1 = count
        mostChar1 = v1[i]

mostCount2 = 0
mostChar2 = 0

for i in range(n//2):
    count = v2.count(v2[i])
    if count > mostCount2:
        mostCount2 = count
        mostChar2 = v2[i]

if mostChar1 == mostChar2:
    ans = n//2
else:
    ans = n - mostCount1 - mostCount2

print(ans)

Submission status: Time Limit Exceeded

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

V1 = [V[n] for n in range(0, n, 2)] #v1, v3, v5, ...
V2 = [V[n] for n in range(1, n, 2)] #v2, v4, v6, ...

V1_set = list(set(V1))
V1_cnt = [[0, 0]] #V1に一種類しか数字が存在しなかったときのために
for i in V1_set:
  i_cnt = V1.count(i)
  V1_cnt += [[i, i_cnt]]
V1_cnt.sort(key=lambda x: x[1], reverse=True)
  
V2_set = list(set(V2))
V2_cnt = [[0, 0]] #V2に一種類しか数字が存在しなかったときのために
for i in V2_set:
  i_cnt = V2.count(i)
  V2_cnt += [[i, i_cnt]]
V2_cnt.sort(key=lambda x: x[1], reverse=True)

mxnum_V1_cnt = V1_cnt[0][0] #V1の中で一番出現回数多い数字
mxnum_V2_cnt = V2_cnt[0][0] #V2の中で一番出現回数多い数字
if mxnum_V1_cnt != mxnum_V2_cnt:
  ans = n - (V1_cnt[0][1] + V2_cnt[0][1])
else:
  ans = n - max(V1_cnt[0][1] + V2_cnt[1][1], V1_cnt[1][1] + V2_cnt[0][1])

print(ans)

Submission status: Time Limit Exceeded

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

V1 = [V[n] for n in range(0, n, 2)] #v1, v3, v5, ...
V2 = [V[n] for n in range(1, n, 2)] #v2, v4, v6, ...

V1_set = list(set(V1))
V1_cnt = []
for i in V1_set:
  i_cnt = V1.count(i)
  V1_cnt.append([i, i_cnt])
V1_cnt.append([0, 0]) #V1に一種類しか数字が存在しなかったときのために
V1_cnt.sort(key=lambda x: x[1], reverse=True)
  
V2_set = list(set(V2))
V2_cnt = []
for i in V2_set:
  i_cnt = V2.count(i)
  V2_cnt.append([i, i_cnt])
V2_cnt.append([0, 0]) #V2に一種類しか数字が存在しなかったときのために
V2_cnt.sort(key=lambda x: x[1], reverse=True)

mxnum_V1_cnt = V1_cnt[0][0] #V1の中で一番出現回数多い数字
mxnum_V2_cnt = V2_cnt[0][0] #V2の中で一番出現回数多い数字
if mxnum_V1_cnt != mxnum_V2_cnt:
  ans = n - (V1_cnt[0][1] + V2_cnt[0][1])
else:
  ans = n - max(V1_cnt[0][1] + V2_cnt[1][1], V1_cnt[1][1] + V2_cnt[0][1])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys

from collections import Counter


def cnt_mode(v1, v2, n, mode1, mode2):
    cc1 = 0
    cc2 = 0
    for i in range(int(n/2)):
        if v1[i] == mode1:
            cc1 += 1
        if v2[i] == mode2:
            cc2 += 1
    return cc1+cc2


n = int(input())

v = np.array([])
v1 = np.array([], dtype=np.int64)
v2 = np.array([], dtype=np.int64)

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

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

e = Counter(v1)
o = Counter(v2)

ee = e.most_common(2)
oo = o.most_common(2)

if ee[0][0] != oo[0][0]:
    mode1 = oo[0][1]
    mode2 = ee[0][1]
    print(cnt_mode(v1, v2, n, mode1, mode2))
    sys.exit()

if ee[0][0] == oo[0][0]:
    if len(ee) == 1 and len(oo) == 1:
        mode2 = int(ee[0][0]) + 1
        mode1 = ee[0][0]
        print(cnt_mode(v1, v2, n, mode1, mode2))
        sys.exit()

    if len(ee) == 1:
        mode2 = ee[0][0]
        mode1 = oo[1][0]
        print(cnt_mode(v1, v2, n, mode1, mode2))
        sys.exit()

    if len(oo) == 1:
        mode2 = ee[1][0]
        mode1 = oo[0][0]
        print(cnt_mode(v1, v2, n, mode1, mode2))
        sys.exit()

    if ee[0][1] >= oo[0][1]:
        mode2 = ee[0][0]
        mode1 = oo[1][0]
        print(cnt_mode(v1, v2, n, mode1, mode2))
        sys.exit()
    else:
        mode2 = ee[1][0]
        mode1 = ee[0][0]
        print(cnt_mode(v1, v2, n, mode1, mode2))
        sys.exit()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
e = {}
o = {}
for i in range(0, n, 2):
    if str(v[i]) not in e.keys():
        e[str(v[i])] = 1
    else:
        e[str(v[i])] += 1
    if str(v[i+1]) not in o.keys():
        o[str(v[i+1])] = 1
    else:
        o[str(v[i+1])] += 1
"""
e = sorted(e.items(), key=lambda x:x[1])
o = sorted(o.items(), key=lambda x:x[1])
print(e)
"""

e = sorted(e.items())
o = sorted(o.items())
ek = []
ev = []
ok = []
ov = []
for i in range(len(e)):
    ek.append(e[i][0])
    ev.append(e[i][1])
for i in range(len(o)):
    ok.append(o[i][0])
    ov.append(o[i][1])

if ek[-1] != ok[-1]:
    print(int(n/2-ev[-1] + n/2-ov[-1]))
else:
    if len(ek) != 1 and len(ok) != 1:
        if ek[-1] != ok[-2] or ek[-2] != ok[-1]:
            print(int(min(n/2-ev[-1] + n/2-ov[-2], n/2-ev[-2] + n/2-ov[-1])))
        else:
            print(int(n/2))
    else:
        print(int(n/2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int,input().split()))
even = [[v[i], 0] for i in range(n) if (i+1)%2 == 0]
odd = [[v[i], 0] for i in range(n) if (i+1)%2 == 1]
for i in range(n):
    for j in range(n):
        if (i+1) % 2 == 0 and (j+1) % 2 == 0:
            if even[i//2][0] == even[j//2][0]:
                even[i//2][1] += 1
        elif (i+1) % 2 == 1 and (j+1) % 2 == 1:
            if odd[i//2][0] == odd[j//2][0]:
                odd[i//2][1] += 1
even = sorted(even, key = lambda x: x[1],reverse = True)
odd = sorted(odd, key = lambda x: x[1],reverse = True)
#print(even)
#print(odd)
if even == odd:
    print(n//2)
elif even[0][0] != odd[0][0]:
    print(n - even[0][1] - odd[0][1])
elif even[0][1] >= odd[0][1]:
    for i in range(n//2):
        if odd[i][0] != even[0][0]:
            print(n - even[0][1] - odd[i][1])
            break
else:
    for i in range(n//2):
        if even[i][0] != odd[0][0]:
            print(n - even[i][1] - odd[0][1])
            break

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
v = list(map(int,input().split()))
v1 = v[::2]
v2 = v[1::2]
v1_n = Counter(v1)
v1_N =v1_n.most_common()
v2_n = Counter(v2)
v2_N = v2_n.most_common()
if len(v1_N) == 1 and len(v2_N) == 1 and v1_N[0][0] == v2_N[0][0]:
    print(int(n/2))
elif len(([k[0] for k in v1_n.items() if k[1] == max(v1_n.values())])) == 1 and len(([k[0] for k in v2_n.items() if k[1] == max(v2_n.values())])) == 1 and v1_N[0][0] == v2_N[0][0]:
    print(min(n-v1_N[0][1]-v2_N[1][1],n-v1_N[1][1]-v2_N[0][1]))
else:
    print(n-v1_N[0][1]-v2_N[0][1])

Submission status: Time Limit Exceeded

Solution code:
import collections


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

    v0 = count(v[::2])
    v1 = count(v[1::2])

    if len(v0) == 1 and len(v1) == 1:
        if v0[0][0] == v1[0][0]:
            print(n // 2)
        else:
            print(0)
        return

    l0 = max(2, len(v0))
    l1 = max(2, len(v1))

    INF = 10 ** 9
    score = [[0] * l1 for _ in range(l0)]
    for i in range(l0):
        for j in range(l1):
            score[i][j] = sum_v(v0, i) + sum_v(v1, j)
            if i >= l0 - 1 or j >= l1 - 1 or v0[i][0] == v1[j][0]:
                score[i][j] = INF
    print(min([min(s) for s in score]))


def count(v):
    return collections.Counter(v).most_common()


def sum_v(v, ii):
    return sum([vv[1] for i, vv in enumerate(v) if i != ii])


main()

Submission status: Time Limit Exceeded

Solution code:
import collections


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

    v0 = count(v[::2])
    v1 = count(v[1::2])

    if len(v0) == 1 and len(v1) == 1:
        if v0[0][0] == v1[0][0]:
            print(n // 2)
        else:
            print(0)
        return

    l0 = max(2, len(v0))
    l1 = max(2, len(v1))

    INF = 10 ** 9
    score = [[0] * l1 for _ in range(l0)]
    for i in range(l0):
        for j in range(l1):
            score[i][j] = sum_v(v0, i) + sum_v(v1, j)
            if i >= l0 - 1 or j >= l1 - 1 or v0[i][0] == v1[j][0]:
                score[i][j] = INF
    print(min([min(s) for s in score]))


def count(v):
    return sorted(list(collections.Counter(v).items()), key=lambda x: x[1], reverse=True)


def sum_v(v, ii):
    return sum([vv[1] for i, vv in enumerate(v) if i != ii])


main()

Submission status: Time Limit Exceeded

Solution code:
def sukuna(l):
	s=set(l)
	return len(l)-max([l.count(i) for i in s])
	

def resolve():
	n=int(input())
	v=list(map(int,input().split()))
	if len(set(v))==1:
		print(n//2)
	else:
		vo=v[::2]
		ve=v[1::2]
		vos=set(vo)
		ves=set(ve)
		o=sukuna(vo)
		e=sukuna(ve)
		print(o+e)

resolve()

Submission status: Time Limit Exceeded

Solution code:
n1 = [0] * 100005
n2 = [0] * 100005
v1 = []
v2 = []
n = int(input())
v = list(map(int, input().split()))
for i in range(n):
    if(i % 2 == 0):
        n1[v[i]] += 1
        v1.append(v[i])
    else:
        n2[v[i]] += 1
        v2.append(v[i])
v1s = list(set(v1))
v2s = list(set(v2))
v1s.append(0)
v2s.append(0)
ans = 10000000
for num1 in v1s:
    for num2 in v2s:
        if(num2 == num1):
            continue
        ans = min(ans, n//2-n2[num2] + n//2-n1[num1])
print(ans)