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:
#!/usr/bin/env python3
#ABC111 C

from operator import itemgetter
n = int(input())
v = list(map(int,input().split()))

s = [v[i] for i in range(n) if i % 2 == 0]
t = [v[i] for i in range(n) if i % 2 == 1]

ss = list(set(s))
tt = list(set(t))
ls = len(ss)
lt = len(tt)
sc = [[ss[i],s.count(ss[i])] for i in range(len(ss))]
tc = [[tt[i],t.count(tt[i])] for i in range(len(tt))]
sc.sort(key = itemgetter(1))
tc.sort(key = itemgetter(1))
if sc[-1][0] == tc[-1][0]:
    if ls == 1 and lt == 1:
        print(n - sc[-1][1])
    elif ls == 1 and lt != 1:
        print(n - sc[-1][1] - tc[-2][1])
    elif ls != 1 and lt == 1:
        print(n - sc[-2][1] - tc[-1][1])
    else:
        ma = max(sc[-2][1],tc[-2][1])
        print(n - sc[-1][1] - ma)
else:
    print(n - sc[-1][1] - tc[-1][1])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
#ABC111 C

from operator import itemgetter
n = int(input())
v = list(map(int,input().split()))

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

ss = list(set(s))
tt = list(set(t))

sc = []
tc = []
for i in range(len(ss)):
    sc.append([ss[i],s.count(ss[i])])
for i in range(len(tt)):
    tc.append([tt[i],t.count(tt[i])])
sc.sort(key = itemgetter(1))
tc.sort(key = itemgetter(1))
if sc[-1][0] == tc[-1][0]:
    if len(sc) == 1 and len(tc) == 1:
        print(n - sc[-1][1])
    elif len(sc) == 1 and len(tc) != 1:
        print(n - sc[-1][1] - tc[-2][1])
    elif len(sc) != 1 and len(tc) == 1:
        print(n - sc[-2][1] - tc[-1][1])
    else:
        ma = max(sc[-2][1],tc[-2][1])
        print(n - sc[-1][1] - ma)
else:
    print(n - sc[-1][1] - tc[-1][1])

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
b = list(map(int, input().split()))
c = []
for i in range(a):
    d = False
    for j in range(len(c)):
        if c[j][0] == b[i]:
            c[j][1] += 1
            d = True
            break
    if not d:
        c.append([b[i], 1])
c = sorted(c, key=lambda x: x[0], reverse=True)
if len(c) <= 1:
    print(a // 2)
else:
    e = [c[0][0], c[1][0]] + b
    f = [c[1][0], c[0][0]] + b
    ec = 0
    fc = 0
    for i in range(2, len(e)):
        if e[i] != e[i-2]:
            e[i] = e[i-2]
            ec += 1
        if f[i] != f[i-2]:
            f[i] = f[i-2]
            fc += 1
    print(min(ec, fc))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
v = list(map(int,input().split(" ")))
#print(v)


cnt = 0

#print(len(set(v)))
start = 0

while True:
    #print(v)
    
    if len(set(v)) == 1:
        cnt = n//2
        break
    
    flag = True#条件check用
    
    #print(v,start,n-2)
    for i in range(start,n-2):
        
        if v[i] != v[i+2]:
            v[i] = v[i+2]
            cnt += 1
            flag = False
            start = i+1
            
            break
    if flag and len(set(v)) == 2:
        break
    
    #cnt += 1
        
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
v = list(map(int,input().split(" ")))
#print(v)


cnt = 0

#print(len(set(v)))
start = 0

while True:
    #print(v)
    
    if len(set(v)) == 1:
        cnt = n//2
        break
    
    flag = True#条件check用
    
    #print(v,start,n-2)
    for i in range(start,n-2):
        
        if v[i] != v[i+2]:
            v[i] = v[i+2]
            cnt += 1
            flag = False
            start = i
            
            break
    if flag and len(set(v)) == 2:
        break
    
    #cnt += 1
        
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# Your code here!
n = int(input())
v = list(map(int,input().split(" ")))
#print(v)


cnt = 0

#print(len(set(v)))

while True:
    #print(v)
    
    if len(set(v)) == 1:
        cnt = n//2 if n%2 == 0 else (n-1)//2
        break
    
    for i in range(n-2):
        if v[i] != v[i+2]:
            v[i+2] = v[i]
            cnt += 1
            break
    
    
    
    #条件check
    if len(set(v)) == 2:
        flag = True
        for i in range(n-2):
            if v[i] != v[i+2]:
                flag = False
                break
            
        if flag:
            break
    
    cnt += 1
        
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
v1 = []
v2 = []
maxv1 = 0
secondv1 = 0
maxv2 = 0
secondv2 = 0
a = 0
b = 0

for i in range(int(len(v)/2)):
    v1.append(v[2*i-1])
    v2.append(v[2*i])

v1_ = set(v1)
v2_ = set(v2)
v1_ = list(v1_)
v2_ = list(v2_)

for i in range(len(v1_)):
    if v1.count(v1_[i]) < maxv1:
        secondv1 = max(v1.count(v1_[i]),secondv1)
    else:
        secondv1 = maxv1
        maxv1 = v1.count(v1_[i]) 
        a = i
for i in range(len(v2_)):
    if v2.count(v2_[i]) < maxv2:
        secondv2 = max(v2.count(v2_[i]),secondv2)
    else:
        secondv2 = maxv2
        maxv2 = v2.count(v2_[i])
        b = i 
 
if v2_[b]==v1_[a]:
    print(min(len(v)-maxv1-secondv2, len(v)-maxv2-secondv1))
else:
    print(len(v)-maxv1-maxv2)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=list(map(int,input().split()))
v_odd=[]
v_even=[]
odd=[[0,0]]
even=[[0,0]]

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

for i in range(int(n/2)):
    odd.append([v_odd[i],v_odd.count(v_odd[i])])
    even.append([v_even[i],v_even.count(v_even[i])])

odd.sort(key=lambda x:(x[1],x[0]),reverse=True)
even.sort(key=lambda x:(x[1],x[0]),reverse=True)

if odd[0][0]!=even[0][0]:
    ans=n-odd[0][1]-even[0][1]
else:
    ans=min(n-odd[0][1]-even[even[0][1]][1],n-odd[odd[0][1]][1]-even[0][1])
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int,input().split()))
b = []
c = []

for i, vv in enumerate(v):
    if i%2 == 0:
        b.append(vv)
    else:
        c.append(vv)

b_set = []
c_set = []

for bb in set(b):
    b_set.append((b.count(bb), bb))
for cc in set(c):
    c_set.append((c.count(cc), cc))

b_set.sort(reverse=True)
b_set.append((0,0))
c_set.sort(reverse=True)
c_set.append((0,0))

if b_set[0][1] != c_set[0][1]:
    ans = n - b_set[0][0] - c_set[0][0]
else:
    try1 = n - b_set[1][0] - c_set[0][0]
    try2 = n - b_set[0][0] - c_set[1][0]
    ans = min(try1,try2)

print(ans)

Submission status: Time Limit Exceeded

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

import sys

input = sys.stdin.readline

def main():
    N = int(input())
    S = list(map(int, input().split()))
    odd = {}
    even = {}
    for i in range(N):
        if i % 2 == 0:
            if S[i] not in even:
                even[S[i]] = 1
            else:
                even[S[i]] += 1
        else:
            if S[i] not in odd:
                odd[S[i]] = 1
            else:
                odd[S[i]] += 1

    n = 1000000000
    even_max1 = 0
    even_max2 = 0
    odd_max1 = 0
    odd_max2 = 0
    for ek, ev in even.items():
        if ev > even_max1:
            even_max1 = ev
        elif ev > even_max2:
            even_max2 = ev

    for ok, ov in odd.items():
        if ov > odd_max1:
            odd_max1 = ov
        elif ov > odd_max2:
            odd_max2 = ov

    for ek, ev in even.items():
        if ev < even_max2:
            continue
        for ok, ov in odd.items():
            if ov < odd_max2:
                continue
            if ek == ok:
                n = min(n, N - max(even[ek], odd[ok]))
            else:
                n = min(n, N - even[ek] - odd[ok])
    print(n)

main()

Submission status: Time Limit Exceeded

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

import sys

input = sys.stdin.readline

def main():
    N = int(input())
    S = list(map(int, input().split()))
    odd = {}
    even = {}
    for i in range(N):
        if i % 2 == 0:
            if S[i] not in even:
                even[S[i]] = 1
            else:
                even[S[i]] += 1
        else:
            if S[i] not in odd:
                odd[S[i]] = 1
            else:
                odd[S[i]] += 1

    n = 1000000000
    for ek, ev in even.items():
        for ok, ov in odd.items():
            if ek == ok:
                n = min(n, N - max(even[ek], odd[ok]))
            else:
                n = min(n, N - even[ek] - odd[ok])
    print(n)

main()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

if len(set_v) == 1:
    print(n//2)
    sys.exit()

pattern_v = []
pattern = []

for i in set_v:
    for j in set_v:
        if i != j:
            pattern_v.append([i,j])

for s, e in pattern_v:
    tmp = []
    for k in range(n):
        if k % 2 == 0:
            tmp.append(s)
        else:
            tmp.append(e)
    pattern.append(tmp)

ans = []

for i in range(len(pattern)):
    tmp = 0
    for j in range(n):
        if pattern[i][j] != v[j]:
            tmp += 1
    ans.append(tmp)

print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
###template###
import sys
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())
###template###

N = int(input())
V = list(mi())

oddl = []
evenl = []
cnt = 0
for v in V:
  if cnt % 2 == 0: oddl.append(v)
  else: evenl.append(v)
  cnt += 1

#oddl, evenlに含まれる数の二次元リストを作り、数が多い順にソートする
ocontents = []
econtents = []
oddsetl = list(set(oddl))
evensetl = list(set(evenl))
for o in oddsetl:
  ocontents.append((o, oddl.count(o)))
for e in evensetl:
  econtents.append((e, evenl.count(e)))
ocontents = sorted(ocontents, key=lambda x:-x[1])
econtents = sorted(econtents, key=lambda x:-x[1])

Nd2 = round(N/2) #それぞれの個数
if ocontents[0][0] != econtents[0][0]:
  print(Nd2-ocontents[0][1] + Nd2-econtents[0][1])
elif ocontents[0][1] >= econtents[0][1]:
  if len(econtents)==1: print(Nd2-ocontents[0][1] + Nd2)
  else: print(Nd2-ocontents[0][1] + Nd2-econtents[1][1])
else:
  if len(ocontents)==1: print(Nd2 + Nd2-econtents[0][1])
  else: print(Nd2-ocontents[1][1] + Nd2-econtents[0][1])

Submission status: Time Limit Exceeded

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

odd = [vec[i] for i in range(0, n, 2)]
odd = [(n, odd.count(n)) for n in set(odd)]
odd.sort(key=lambda tpl: tpl[1], reverse=True)

even = [vec[i] for i in range(1, n, 2)]
even = [(n, even.count(n)) for n in set(even)]
even.sort(key=lambda tpl: tpl[1], reverse=True)

if odd[0][0] == even[0][0]:
    if odd[0][1] > even[0][1]:
        print(
            sum([cnt for _, cnt in odd[1:]]) + even[0][1] +
            sum([cnt for _, cnt in even[2:]]))
    else:
        print(
            sum([cnt for _, cnt in even[1:]]) + odd[0][1] +
            sum([cnt for _, cnt in odd[2:]]))
else:
    print(sum([cnt for _, cnt in odd[1:]]) + sum([cnt for _, cnt in even[1:]]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
V = [int(v) for v in input().split()]
how_many_replace = 0
mx_cnt = 0
for i in range(0, N, 2):
  cnt = V[0:N:2].count(V[i])
  if cnt > mx_cnt:
    mx_cnt = cnt
    mx_cnt_nm_V_135 = V[i]
how_many_replace += (len(V)/2-mx_cnt)

mx_cnt = 0
for i in range(1, N, 2):
  cnt = V[1:N:2].count(V[i])
  if cnt > mx_cnt:
    if V[i] != mx_cnt_nm_V_135:
      mx_cnt = cnt
how_many_replace += (len(V)/2-mx_cnt)

print(int(how_many_replace))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
V = [int(v) for v in input().split()]
V_135 = []
V_246 = []
for n in range(N):
  if n % 2 == 0:
    V_135.append(V[n])
  else:
    V_246.append(V[n])

how_many_replace = 0
mx_cnt = 0
for i in range(len(V_135)):
  cnt = V_135.count(V_135[i])
  if cnt > mx_cnt:
    mx_cnt = cnt
    mx_cnt_nm_V_135 = V_135[i]
how_many_replace += (len(V_135)-mx_cnt)

mx_cnt = 0
for i in range(len(V_246)):
  cnt = V_246.count(V_246[i])
  if cnt > mx_cnt:
    if V_246[i] != mx_cnt_nm_V_135:
      mx_cnt = cnt
how_many_replace += (len(V_246)-mx_cnt)

print(how_many_replace)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
V = [int(x) for x in stdin.readline().rstrip().split()]
V_odd = [V[2 * k] for k in range(int(n / 2))]
V_even = [V[2 * k + 1] for k in range(int(n / 2))]
V_odd_major = [0]
V_even_major = [0]
X = 0
Y = 0
for l in range(len(V_odd)):
    V_odd_sub = [x for x in V_odd if x == V_odd[l]]
    V_even_sub = [x for x in V_even if x == V_even[l]]
    if len(V_odd_sub) > V_odd_major[len(V_odd_major) - 1]:
        V_odd_major.append(len(V_odd_sub))
        X = V_odd[l]
    if len(V_even_sub) > V_even_major[len(V_even_major) - 1]:
        V_even_major.append(len(V_even_sub))
        Y = V_even[l]
V_odd_major.sort(reverse=True)
V_even_major.sort(reverse=True)
if X != Y:
    print(n - V_odd_major[0] - V_even_major[0])
else:
    print(n - (max(V_odd_major[1] + V_even_major[0], V_odd_major[0] + V_even_major[1])))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = [int(_) for _ in input().split()]
x = v[::2]
y = v[1::2]
if len(set(x)) == 1 and len(set(y)) == 1:
    if x[0] == y[0]:
        print(len(y))
    else:
        print(0)
elif len(set(x)) != 0 and len(set(y)) == 1:
    u = sorted([[x[i],x.count(x[i])] for i in range(len(x))],key=lambda t:-t[1])
    max1 = u[0][1]
    moji1 = u[0][0]
    max2 = 0
    moji2 = 0
    for i in range(len(x)):
        if u[i][0] != moji1:
            moji2 = u[i][0]
            max2 = u[i][1]
            break
    if moji1 != y[0]:
        print(len(x) - max1)
    else:
        print(len(x) - max2)
elif len(set(y)) != 0 and len(set(x)) == 1:
    v = sorted([[y[i],y.count(y[i])] for i in range(len(y))],key=lambda t:-t[1])
    max3 = v[0][1]
    moji3 = v[0][0]
    max4 = 0
    moji4 = 0
    for i in range(len(y)):
        if v[i][0] != moji3:
            moji4 = v[i][0]
            max4 = v[i][1]
            break
    if moji3 != y[0]:
        print(len(x) - max3)
    else:
        print(len(x) - max4)
else:
    u = sorted([[x[i],x.count(x[i])] for i in range(len(x))],key=lambda t:-t[1])
    max1 = u[0][1]
    moji1 = u[0][0]
    max2 = 0
    moji2 = 0
    for i in range(len(x)):
        if u[i][0] != moji1:
            moji2 = u[i][0]
            max2 = u[i][1]
            break
    v = sorted([[y[i],y.count(y[i])] for i in range(len(y))],key=lambda t:-t[1])
    max3 = v[0][1]
    moji3 = v[0][0]
    max4 = 0
    moji4 = 0
    for i in range(len(y)):
        if v[i][0] != moji3:
            moji4 = v[i][0]
            max4 = v[i][1]
            break
    if moji1 != moji3:
        print(n - max1 - max3)
    else:
        print(min(n-max1-max4,n-max2-max3))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

v = list(map(int,input().split()))
v_odd = []
v_eve = []
v_odd_set = []
v_eve_set = []
v_odd_count = []
v_eve_count = []
v_o_max = 0
v_e_max = 0

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

v_odd_set = list(set(v_odd))
v_eve_set = list(set(v_eve))

for c in v_odd_set:
    a = v_odd.count(c)
    v_odd_count.append(a)
    if a == max(v_odd_count):
        v_o_max = c
    else:
        continue

for c in v_eve_set:
    a = v_eve.count(c)
    v_eve_count.append(a)
    if a == max(v_eve_count):
        v_e_max = c
    else:
        continue

v_odd_count.sort()
v_eve_count.sort()

if len(v_odd_count) != 1 or len(v_eve_count) != 1:
    if v_o_max == v_e_max:
        if v_odd_count[-1] >= v_eve_count[-2]:
            print(N - v_eve_count[-1] - v_eve_count[-2])
        else:
            print(N - v_odd_count[-1] - v_eve_count[-2])
    else:
        print(N - v_eve_count[-1] - v_odd_count[-1])
else:
    if v_o_max == v_e_max:
        print(int(N/2))
    else:
        print(0)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
count_dict = {i:{} for i in range(2)}
#print(count_dict[0])
for i in range(2):
  for j in A[i::2]:
    if j not in count_dict[i]:
      count_dict[i][j] = 1
    else:
      count_dict[i][j] += 1

candidates = [sorted(count_dict[i].items(), key=lambda kv: kv[1]) for i in range(2)]

candidates = [i[1]+j[1] for i in candidates[0] for j in candidates[1] if i[0] != j[0]]
no_move = max(candidates) if candidates != [] else len(A) // 2
print(N-no_move)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
count_dict = {i:{} for i in range(2)}
#print(count_dict[0])
for i in range(2):
  for j in A[i::2]:
    if j not in count_dict[i]:
      count_dict[i][j] = 1
    else:
      count_dict[i][j] += 1

candidates = []
for i in range(2):
  candidates.append(sorted(count_dict[i].items(), key=lambda kv: kv[1]))

candidates = [i[1]+j[1] for i in candidates[0] for j in candidates[1] if i[0] != j[0]]
no_move = max(candidates) if candidates != [] else len(A) // 2
print(N-no_move)

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
v = list(map(int,input().split()))
num1 = []
num2 = []
num2_sam = []
counts =[0,0]

for i in range(len(v)):
    if i % 2 == 0:
        num1.append(v[i])
    else:
        num2.append(v[i])
        num2_sam.append(v[i])

n1 = collections.Counter(num1).most_common()[0][0]
for i in range(num2_sam.count(n1)):
    num2_sam.remove(n1)

if len(num2_sam) == 0:
    n2 = len(num1)
else:
    n2 = collections.Counter(num2_sam).most_common()[0][0]

for i in range(len(num2)):
    if n1 != num1[i]:
        counts[0] += 1

    if n2 != num2[i]:
        counts[1] += 1

if (n1 == n2) and sum(num2) == sum(num1):
    print(len(num1))
else:
    print(sum(counts))

Submission status: Time Limit Exceeded

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

vs1 = [vs[i] for i in range(n) if i % 2 == 0]
vs2 = [vs[i] for i in range(n) if i % 2 != 0]

vsc1 = []
for v in set(vs1):
    vsc1.append((v, vs1.count(v)))

vsc2 = []
for v in set(vs2):
    vsc2.append((v, vs2.count(v)))

vsc1 = sorted(vsc1, key=lambda x: x[1], reverse=True)
vsc2 = sorted(vsc2, key=lambda x: x[1], reverse=True)
vsc1.append((-1, 0))
vsc2.append((-1, 0))

if vsc1[0][0] != vsc2[0][0]:
    print(n - (vsc1[0][1] + vsc2[0][1]))
else:
    print(n - max(vsc1[0][1] + vsc2[1][1], vsc1[1][1] + vsc2[0][1]))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

lst = list(map(int, input().split()))
lst1 = lst[::2]
lst2 = lst[1::2]

#print (lst, lst1, lst2)

a1 = 0
a2 = 0
b1 = 0
b2 = 0
for i in range(N//2):
    a = lst1.count(lst1[i])
    b = lst2.count(lst2[i])
    if a > a1:
        c = lst1[i]
        a1, a2 = a, a1
    elif a > a2 and lst1[i] != c:
        a2 = a
    if b > b1:
        d = lst2[i]
        b1, b2 = b, b1
    elif b > b2 and lst2[i] != d:
        b2 = b

#print(a1, a2, b1, b2)
if c == d:
    ans = max(a1+b2, a2+b1)
else:
    ans =a1 + b1

print (N - ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
V=list(map(int,input().split()))
o={0:0}
e={0:0}
n=1
for i,v in enumerate(V):
    if n:
        if v in o.keys():
            o[v]+=1
        else:
            o[v]=1
    else:
        if v in e.keys():
            e[v]+=1
        else:
            e[v]=1
    n=1-n
O=sorted(o.items(),key=lambda x:-x[1])
E=sorted(e.items(),key=lambda x:-x[1])
if O[0][0]==E[0][0]:
    m=max(O[0][1]+E[1][1],E[0][1]+O[1][1])
else:
    m=O[0][1]+E[0][1]
print(N-m)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=input().split()
o=[("0",0)]
e=[("0",0)]
for i in range(n//2):
    I=2*i
    O=len(o)
    E=len(e)
    for j in range(O):
        if v[I]==o[j][0]:
            o[j]=(o[j][0],o[j][1]+1)
            break
        elif j==O-1:
            o.append((v[I],1))
    for k in range(E):
        if v[I+1]==e[k][0]:
            e[k]=(e[k][0],e[k][1]+1)
            break
        elif k==E-1:
            e.append((v[I+1],1))
o.sort(key=lambda x:-x[1])
e.sort(key=lambda x:-x[1])
if o[0][0]==e[0][0]:
    if o[0][1]<e[0][1]:
        m=e[0][1]+o[1][1]
    else:
        m=o[0][1]+e[1][1]
else:
    m=o[0][1]+e[0][1]
print(n-m)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=input().split()
o=[("0",0)]
e=[("0",0)]
for i in range(n//2):
    I=2*i
    O=len(o)
    E=len(e)
    for j in range(O):
        if v[I]==o[j][0]:
            o[j]=(o[j][0],o[j][1]+1)
            break
        elif j==O-1:
            o.append((v[I],1))
    for k in range(E):
        if v[I+1]==e[k][0]:
            e[k]=(e[k][0],e[k][1]+1)
            break
        elif k==E-1:
            e.append((v[I+1],1))
o.sort(key=lambda x:x[1])
e.sort(key=lambda x:x[1])
if o[-1][0]==e[-1][0]:
    if o[-1][1]<e[-1][1]:
        m=e[-1][1]+o[-2][1]
    else:
        m=o[-1][1]+e[-2][1]
else:
    m=o[-1][1]+e[-1][1]
print(n-m)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
v=input().split()
o=[("0",0)]
e=[("0",0)]
for i in range(n//2):
    for j in range(len(o)):
        if v[2*i]==o[j][0]:
            o[j]=(o[j][0],o[j][1]+1)
            break
        elif j==len(o)-1:
            o.append((v[2*i],1))
    for k in range(len(e)):
        if v[2*i+1]==e[k][0]:
            e[k]=(e[k][0],e[k][1]+1)
            break
        elif k==len(e)-1:
            e.append((v[2*i+1],1))
o.sort(key=lambda x:x[1])
e.sort(key=lambda x:x[1])
if o[-1][0]==e[-1][0]:
    if o[-1][1]<e[-1][1]:
        m=e[-1][1]+o[-2][1]
    else:
        m=o[-1][1]+e[-2][1]
else:
    m=o[-1][1]+e[-1][1]
print(n-m)

Submission status: Time Limit Exceeded

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

w = v[0::2]
z = v[1::2]
 
ww = set(w)
zz = set(z)

x = [w.count(i) for i in ww]
y = [z.count(i) for i in zz]

a = len(x)
b = len(y)
c = sum(x)-max(x)
d = sum(y)-max(y)

if a == 1 and b == 1 and w != z:
  print(0)
elif a == 1 and b == 1 and w == z:
  print(n//2)
else:
  print(c+d)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
v = [int(x) for x in stdin.readline().rstrip().split()]
w = v[0::2]
z = v[1::2]
 
ww = set(w)
zz = set(z)

x = [w.count(i) for i in ww]
y = [z.count(i) for i in zz]
 
if len(x) == 1 and len(y) == 1 and w != z:
  print(0)
elif len(x) == 1 and len(y) == 1 and w == z:
  print(n//2)
else:
  print(sum(x)-max(x)+sum(y)-max(y))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int,input().split()))
w = v[0::2]
z = v[1::2]
 
ww = set(w)
zz = set(z)
 
x = list(map(w.count,ww))
y = list(map(z.count,zz))
 
if len(x) == 1 and len(y) == 1 and w != z:
  print(0)
elif len(x) == 1 and len(y) == 1 and w == z:
  print(n//2)
else:
  print(sum(x)-max(x)+sum(y)-max(y))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int,input().split()))
w = v[0::2]
z = v[1::2]

x = list(map(w.count,set(w)))
y = list(map(z.count,set(z)))

if len(x) == 1 and len(y) == 1:
  if w != z:
    print(0)
    exit()
  else:
    print(n//2)
    exit()

print(sum(x)-max(x)+sum(y)-max(y))

Submission status: Time Limit Exceeded

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

l = []
cnt = [0] * N

for ind, i in enumerate(v):
	if not i in l:
		l.append(i)
	cnt[ind] = l.index(i)

ret = 0
for i, c in enumerate(cnt):
	if i % 2 == 0:
		if c != 0:
			ret += 1
	else:
		if c != 1:
			ret += 1

print(ret)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
from math import *
from itertools import *
from collections import *
from functools import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(n: int, v: "List[int]"):
    e = [(len(v[0::2]) - vv, k, 0) for k, vv in Counter(v[0::2]).items()]
    o = [(len(v[1::2]) - vv, k, 1) for k, vv in Counter(v[1::2]).items()]

    res = [(len(v[0::2]),''), (len(v[1::2]),'')]
    for n, k, t in sorted(e + o):
        if res[t][1] != '':
            continue

        if res[1 - t][1] == k:
            continue

        res[t] = (n, k)
        if res[0][1] != '' and res[1][1] != '':
            break
    return res[0][0] + res[1][0]


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    v = [ int(next(tokens)) for _ in range(n) ]  # type: "List[int]"
    result = solve(n, v)
    print(result)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

even = [x for i, x in enumerate(v) if (i+1)%2 == 0]
odd = [x for i, x in enumerate(v) if (i+1)%2 == 1]

even_map = {}
for x in even:
    if not x in even_map:
        even_map[x] = even.count(x)

odd_map = {}
for x in odd:
    if not x in odd_map:
        odd_map[x] = odd.count(x)

sorted_even = sorted(even_map.items(), key=lambda x:x[1], reverse=True)
sorted_odd = sorted(odd_map.items(), key=lambda x:x[1], reverse=True)

e = 0
o = 0

if sorted_even[0][1] > sorted_odd[0][1]:
    e = sorted_even[0][0]
    if sorted_odd[0][0] != sorted_even[0][0]:
        o = sorted_odd[0][0]
    else:
        if len(sorted_odd) >= 2:
            o = sorted_odd[1][0]
        else:
            o = -1
else:
    o = sorted_odd[0][0]
    if sorted_odd[0][0] != sorted_even[0][0]:
        e = sorted_even[0][0]
    else:
        if len(sorted_even) >= 2:
            e = sorted_even[1][0]
        else:
            e = -1

ans = 0
for x in even:
    if x != e:
        ans = ans + 1

for x in odd:
    if x != o:
        ans = ans + 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq

n = int(input())
v = list(map(int,input().split()))
a_odd = []
a_even = []
check_a_odd = []
check_a_even = []
count_a_odd = []
count_a_even = []

for i in range(len(v)):
    if i%2 == 0:
        a_odd.append(v[i])
    else:
        a_even.append(v[i])

#print(a_odd)
#print(a_even)

for j in range(len(a_odd)):
    if a_odd[j] not in check_a_odd:
        heapq.heappush(count_a_odd,[-1*a_odd.count(a_odd[j]),a_odd[j]])
        check_a_odd.append(a_odd[j])
    if a_even[j] not in check_a_even:
        heapq.heappush(count_a_even,[-1*a_even.count(a_even[j]),a_even[j]])
        check_a_even.append(a_even[j])

count_a_odd.append([0,0])
count_a_even.append([0,0])

#print(count_a_odd)
#print(count_a_even)

if int(count_a_odd[0][1]) != int(count_a_even[0][1]):
    print(n + int(count_a_odd[0][0]) + int(count_a_even[0][0]))
else:
    ans1 = n + int(count_a_odd[0][0]) + int(count_a_even[1][0])
    ans2 = n + int(count_a_odd[1][0]) + int(count_a_even[0][0])
    print(min(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = list(map(int, input().split()))
vo = v[0 : n - 1 : 2]
ve = v[1 : n : 2]

fon = vo[0]
foc = vo.count(v[0])
son = 0
soc = 0
for i in range(n//2):
  if vo[i] != fon and vo[i] != son:
    x = vo.count(vo[i])
    if x > foc:
      son = fon
      soc = foc
      fon = vo[i]
      foc = x
    elif x > soc:
      son = vo[i]
      soc = x
fen = vo[0]
fec = vo.count(vo[0])
sen = 0
sec = 0
for i in range(n//2):
  if ve[i] != fen and ve[i] != sen:
    x = ve.count(ve[i])
    if x > fec:
      sen = fen
      sec = fec
      fen = ve[i]
      fec = x
    elif x > sec:
      sen = ve[i]
      sec = x
if fon != fen:
  print(n - foc - fec)
else:
  p = min(foc - soc, fec - sec)
  print(n - foc - fec + p)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
V = list(map(int,input().split()))
a = [0]*10000000
b = [0]*10000000
v1=[]
v2=[]

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

else:
    for i in range(n):
        if i%2 == 0:
            a[V[i]-1] += 1
            v1.append(V[i])
        else:
            b[V[i]-1] += 1
            v2.append(V[i])

    max1 = 0
    max2 = 0

    for j in range(len(a)):
        if max1 < a[j]:
            max1 = a[j]
            c1 = j
    for k in range(len(b)):
        if max2 < b[k]:
            max2=b[k]
            c2=k

    if c1 != c2:
        print(n-max1-max2)
    else:
        a.sort()
        b.sort()
        print(min(n-max1-b[-2],n-max2-a[-2]))

Submission status: Time Limit Exceeded

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

vv = list(set(v))

count = []
for i in vv:
    count.append((v.count(i), i))

count.sort(key=lambda x: x[0], reverse=True)
vv = [i[1] for i in count]

if len(vv) >= 2:
    vvv = vv.copy()

    min = 10**6
    for i in vv:
        vvv.remove(i)
        for j in vvv:
            if 10-(v.count(i)+v.count(j)) > min:
                pass
            else:
                ct = 0
                a = [i if k % 2 == 0 else j for k in range(n)]
                for k in range(n):
                    if a[k] != v[k]:
                        ct += 1
                if ct < min:
                    min = ct
        vvv.append(i)
    print(min)
else:
    print(int(n/2))

Submission status: Time Limit Exceeded

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

vv = list(set(v))

if len(vv) >= 2:
    vvv = vv.copy()

    min = 10**6
    for i in vv:
        vvv.remove(i)
        for j in vvv:
            ct = 0
            a = [i if k % 2 == 0 else j for k in range(n)]
            for k in range(n):
                if a[k] != v[k]:
                    ct += 1
            if ct < min:
                min = ct
        vvv.append(i)
    print(min)
else:
    print(int(n/2))

Submission status: Time Limit Exceeded

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

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

ia = 0
ib = 0

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

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