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: Accepted

Solution code:
import collections

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

V_odd = [V[i] for i in range(0, n, 2)]
V_even = [V[i] for i in range(1, n, 2)]

c_odd = collections.Counter(V_odd)
c_even = collections.Counter(V_even)

list_odd = []
list_even = []

for k, v in c_odd.items():
    list_odd.append([v, k])
list_odd = sorted(list_odd, reverse=True)

for k, v in c_even.items():
    list_even.append([v, k])
list_even = sorted(list_even, reverse=True)

if list_odd[0][1] == list_even[0][1]:
    if len(list_odd) == 1:
        print(n - list_even[0][0])
    elif len(list_even) == 1:
        print(n - list_odd[0][0])
    else:
        print(min(n - list_odd[0][0] - list_even[1][0],
                  n - list_odd[1][0] - list_even[0][0]))
else:
    print(n - list_odd[0][0] - list_even[0][0])

Submission status: Accepted

Solution code:
n = int(input())
v = [int(i) for i in input().split()]
odd = [[0,i] for i in range(10**5+1)]
even = [[0,i] for i in range(10**5+1)]
for i in v[0:n-1:2]:
    odd[i][0] += 1
for i in v[1:n:2]:
    even[i][0] += 1
odd.sort(reverse=True)
even.sort(reverse=True)
if odd[0][1] != even[0][1]:
    ans = n//2 - odd[0][0] + n//2 - even[0][0]
else:
    ans=min(n//2-odd[0][0]+n//2-even[1][0],n//2-odd[1][0]+n//2-even[0][0])
print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
list1 = list(map(int,input().split()))
list2n = list1[::2]
list2n_1 = list1[1::2]
c2n = collections.Counter(list2n)
c2n_1 = collections.Counter(list2n_1)
c2n_mc = c2n.most_common(2)
c2n_1_mc = c2n_1.most_common(2)

if c2n_mc[0][0] == c2n_1_mc[0][0]:
    if len(c2n_mc) == 1 and len(c2n_1_mc)==1:
        if c2n_mc[0][0] == c2n_1_mc[0][0]:
            ans = n//2
        else:
            ans = 0
    elif len(c2n_mc)==1 or len(c2n_1_mc)==1:
        if len(c2n_mc)==1:
            ans = n - c2n_mc[0][1] - c2n_1_mc[1][1]
        else:
            ans = n - c2n_1_mc[0][1] - c2n_mc[1][1]
    elif c2n_mc[1][1] <= c2n_1_mc[1][1]:
        ans = n - c2n_mc[0][1] - c2n_1_mc[1][1]
    else:
        ans = n - c2n_1_mc[0][1] - c2n_mc[1][1]
else:
    ans = n - c2n_mc[0][1] - c2n_1_mc[0][1]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline
 
def solve(N,V):
    V_odd = Counter(V[::2]).most_common(2)
    V_even = Counter(V[1::2]).most_common(2)
 
    if V_odd[0][0] == V_even[0][0]:
        if len(V_odd) == 1 or len(V_even) == 1:
            print(n//2)
        else:
            print(N-max(V_odd[0][1]+V_even[1][1], V_odd[1][1]+V_even[0][1]))
    else:
        print(N-V_odd[0][1]-V_even[0][1])
 
if __name__ == "__main__":
    n = int(input())
    v = list(map(int,input().split()))
    solve(n,v)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline

n = int(input())
vn = [int(x) for x in input().strip().split()]

v1 = [v for i, v in enumerate(vn) if i % 2 == 0]
v2 = [v for i, v in enumerate(vn) if i % 2 != 0]
v1cnt = Counter(v1)
v2cnt = Counter(v2)
maxv11 = 0
maxv12 = 0
maxv21 = 0
maxv22 = 0
c_ = 0
for v, c in v1cnt.items():
    if c > c_:
        maxv12 = c_
        c_ = c
        maxv1 = [v]
    elif c == c_:
        maxv1.append(v)
    else:
        maxv12 = max(maxv12, c)

c_ = 0
for v, c in v2cnt.items():
    if c > c_:
        maxv22 = c_
        c_ = c
        maxv2 = [v]
    elif c == c_:
        maxv2.append(v)
    else:
        maxv22 = max(maxv22, c)

if maxv1 != maxv2 or len(maxv1) > 1 or len(maxv2) > 1:
    print(len(v1) - max(v1cnt.values()) + len(v2) - max(v2cnt.values()))
else:
    print(min(len(v1) - max(v1cnt.values()) + len(v2) - maxv22, \
        len(v1) - maxv12 + len(v2) - max(v2cnt.values())))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
w=list(map(int,input().split()))
ans=0
if len(set(w))==1:
    print(len(w)//2)
else:
    a=[]
    a1=[]
    for i in range(n):
        if i%2==0:
            a.append(w[i])
        else:
            a1.append(w[i])
    a.sort()
    a1.sort()
    ans=0
    t=a[0]
    cnt1=[]
    cnt=[]
    b=[]
    b1=[]
    for j in range(len(a)):
        if a[j]==t:
            ans+=1
        else:
            cnt.append([ans,t])
            t=a[j]
            ans=1
        if j==len(a)-1:
            cnt.append([ans,t])
    s=a1[0]
    ans=0
    for j in range(len(a)):
        if a1[j]==s:
            ans+=1
        else:
            cnt1.append([ans,s])
            s=a1[j]
            ans=1
        if j==len(a1)-1:
            cnt1.append([ans,s])
    cnt.sort(reverse=True)
    cnt1.sort(reverse=True)
    """
    print(a)
    print(a1)
    print(cnt)
    print(cnt1)
    """
    if cnt[0][1]!=cnt1[0][1]:
        print(n-cnt[0][0]-cnt1[0][0])
    else:
        if cnt[0][0]>cnt1[0][0]:
            print(n-cnt[0][0]-cnt1[1][0])
        elif cnt[0][0]<cnt1[0][0]:
            print(n-cnt1[0][0]-cnt[1][0])
        else:
            t=0
            s=0
            for i in range(1,min(len(cnt),len(cnt1))):
                if cnt[i][0]!=cnt1[i][0]:
                    if cnt[i][0]==max(cnt[i][0],cnt1[i][0]):
                        t=cnt[i][0]
                        s=cnt1[0][0]
                        break
                if cnt[i][0]!=cnt1[i][0]:
                    if cnt1[i][0]==max(cnt[i][0],cnt1[i][0]):
                        t=cnt1[i][0]
                        s=cnt[0][0]
                        break
                if i==-1+min(len(cnt),len(cnt1)):
                    t=cnt[0][0]
                    s=cnt1[1][0]
            print(n-t-s)

Submission status: Accepted

Solution code:
n = int(input())
u = list(map(int, input().split()))
A = []
B = []
Af = [[i, -1] for i in range(100007)]
Bf = [[i, -1] for i in range(100007)]
for i in range(n):
    if i % 2 == 0:
        A.append(u[i])
        Af[u[i]][1] += 1
    else:
        B.append(u[i])
        Bf[u[i]][1] += 1

Af.sort(key=lambda x: -x[1])
Bf.sort(key=lambda x: -x[1])

Ae = 0
Be = 0
ans = 0
if Af[0][0] == Bf[0][0]:
    Ae = Af[0][0]
    Be = Bf[1][0]
    ans1 = 0
    for i in range(len(A)):
        if Ae != A[i]:
            ans1 += 1
    for i in range(len(B)):
        if Be != B[i]:
            ans1 += 1

    Ae = Af[1][0]
    Be = Bf[0][0]
    ans2 = 0
    for i in range(len(A)):
        if Ae != A[i]:
            ans2 += 1
    for i in range(len(B)):
        if Be != B[i]:
            ans2 += 1
    ans = min(ans1, ans2)
else:
    Ae = Af[0][0]
    Be = Bf[0][0]
    for i in range(len(A)):
        if Ae != A[i]:
            ans += 1
    for i in range(len(B)):
        if Be != B[i]:
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
import collections

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

vs_odd=vs[::2]
c_odd=collections.Counter(vs_odd) 
C1=c_odd.most_common()
C1.append((0,0))
common1=C1[0][1] #最頻値の個数
common1sec=C1[1][1] #第2頻値

vs_even=vs[1::2]
c_even=collections.Counter(vs_even) 
C2=c_even.most_common()
C2.append((0,0))
common2=C2[0][1] #最頻値の個数
common2sec=C2[1][1] #第2頻値

Ans1=len(vs_odd)-common1
Ans2=len(vs_even)-common2
Ans3=len(vs_odd)-common1sec
Ans4=len(vs_even)-common2sec

if c_odd.most_common()[0][0] == c_even.most_common()[0][0]:
    #最頻値が同じ場合
    print(min(Ans1+Ans4, Ans2+Ans3))
else:
    print(Ans1+Ans2)

Submission status: Accepted

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

odev = [{}, {}]

for i in range(N):
  odev[i%2][v[i]] = odev[i%2].get(v[i],0) + 1

for i in range(2):
  odev[i] = sorted(odev[i].items(), key=lambda x: x[1], reverse=True)

sameChr = odev[0][0][0] == odev[1][0][0]
odev = [list(map(lambda x: x[1], odev[0])), list(map(lambda x: x[1], odev[1]))]

if(sameChr):
  if(len(odev[0]) > 1 and len(odev[1]) > 1):
    print(N - max(odev[0][1] + odev[1][0], odev[0][0] + odev[1][1]))
  elif(len(odev[0]) == 1 and len(odev[1]) == 1):
    print(N // 2)
  elif(len(odev[0]) == 1):
    print(N - odev[0][0] + odev[1][1])
  else:
    print(N - odev[0][1] + odev[1][0])
else:
  print(N - odev[0][0] - odev[1][0])

Submission status: Accepted

Solution code:
def list_count(my_list,n,x):
    dic={} #辞書型の宣言
    for i in range(x,n,2):
        if my_list[i] in dic: #もしnumがkeyとして辞書にあれば
            dic[my_list[i]]+=1 #valueに+1
        else: #なければ
            dic[my_list[i]]=1 #dicにnumというkeyを追加し、対応するvalueを1とする
    return dic

n=int(input())
v=list(map(int,input().split()))
dic0=list_count(v,n,0)
dic1=list_count(v,n,1)
a=[[0,0],[0,0]]
b=[[0,0],[0,0]]
for i in dic0.keys():
    if a[0][1]<=dic0[i]:
        a[1][0]=a[0][0]
        a[1][1]=a[0][1]
        a[0][0]=i
        a[0][1]=dic0[i]
    elif a[1][1]<dic0[i]:
        a[1][0]=i
        a[1][1]=dic0[i]
for i in dic1.keys():
    if b[0][1]<=dic1[i]:
        b[1][0]=b[0][0]
        b[1][1]=b[0][1]
        b[0][0]=i
        b[0][1]=dic1[i]
    elif b[1][1]<dic1[i]:
        b[1][0]=i
        b[1][1]=dic1[i]
if a[0][0]==b[0][0]:
    print(n-max(a[0][1]+b[1][1],a[1][1]+b[0][1]))
else:
    print(n-a[0][1]-b[0][1])

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int,input().split()))
if collections.Counter(v).most_common()[0][1] == n:
    print(n//2)
    exit()
c = collections.Counter(v[::2])
d = collections.Counter(v[1::2])
c_ = c.most_common()[0][1]
d_ = d.most_common()[0][1]
e = 0
if c.most_common()[0][0] == d.most_common()[0][0]:
    if len(c.most_common()) == 1:
        d_ = d.most_common()[1][1]
    elif len(d.most_common()) == 1:
        c_ = c.most_common()[1][1]
    else:
        if c.most_common()[1][1] < d.most_common()[1][1]:
            d_ = d.most_common()[1][1]
        else:
            c_ = c.most_common()[1][1]
    
cnt = (n//2) - c_
cnt += (n//2) - d_
print(cnt+e)

Submission status: Accepted

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

from collections import Counter
A = Counter(V[::2])
B = Counter(V[1::2])

ac = sorted(A.items(), key=lambda x: -x[1])
bc = sorted(B.items(), key=lambda x: -x[1])

if len(ac) == 1 and len(bc) == 1:
    if ac[0][0] == bc[0][0]:
        print(h)
    else:
        print(0)
elif len(ac) == 1:
    print(h-bc[0][1])
elif len(bc) == 1:
    print(h-ac[0][1])
elif ac[0][0] == bc[0][0]:
    if ac[0][1]-ac[1][1] == bc[0][1]-bc[1][1]:
        if ac[0][1] >= bc[0][1]:
            print((h-ac[0][1]) + (h-bc[1][1]))
        else:
            print((h-ac[1][1]) + (h-bc[0][1]))
    elif ac[0][1]-ac[1][1] > bc[0][1]-bc[1][1]:
        print((h-ac[0][1]) + (h-bc[1][1]))
    else:
        print((h-ac[1][1]) + (h-bc[0][1]))
else:
    print((h-ac[0][1]) + (h-bc[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter

def main():
	n = int(input())
	w = input().split()
	a = Counter(w[::2]).most_common(2)
	b = Counter(w[1::2]).most_common(2)
	if a[0][0]!=b[0][0]:
		stay = a[0][1] + b[0][1]
	elif a[0][1]==b[0][1]==n // 2:
		stay = n // 2
	else:
		stay = max(a[0][1] + b[1][1], a[1][1] + b[0][1])
	print(n - stay)

if __name__=="__main__":
	main()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

# コーナーケースは先に終わらせる
# 全部同じ数の場合は、N/2が答え
if len(set(V)) == 1:
    print(N//2)
    exit()
# 2つしかない場合は1が答え
if N == 2:
    print(1)
    exit()

# 方針: 偶数番目と奇数番目は分けて考える
# 偶数番目、奇数番目はそれぞれの最頻値に合わせるのが低コスト
# 最頻値が同じ場合は「数列に現れる数はちょうど2種類」の制約を外れるので、2番目に多い数にあわせる
d_even = defaultdict(int)
d_odd = defaultdict(int)

for i in range(N):
    v = V[i]
    if i % 2 == 0:
        d_even[v] += 1
    else:
        d_odd[v] += 1

l_even = sorted(d_even.items(), key=lambda x: -x[1])
l_odd = sorted(d_odd.items(), key=lambda x: -x[1])

if l_even[0][0] != l_odd[0][0]:
    print(N - l_even[0][1] - l_odd[0][1])
else:
    # どちらも試してコストの小さい方を採用する
    ans1 = N - l_even[0][1] - l_odd[1][1]
    ans2 = N - l_even[1][1] - l_odd[0][1]
    print(min(ans1, ans2))

Submission status: Accepted

Solution code:

from collections import defaultdict

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

if N == 2:
    if V[0] == V[1]:
        print(0)
    else:
        print(1)
    exit()

d_even = defaultdict(int)
d_odd = defaultdict(int)

for i in range(N):
    v = V[i]
    if i % 2 == 0:
        d_even[v] += 1
    else:
        d_odd[v] += 1

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


l_even = sorted(d_even.items(), key=lambda x: -x[1])
l_odd = sorted(d_odd.items(), key=lambda x: -x[1])

if l_even[0][0] != l_odd[0][0]:
    print(N - l_even[0][1] - l_odd[0][1])
else:
    print(min(N - l_even[0][1] - l_odd[1][1], N - l_even[1][1] - l_odd[0][1]))

Submission status: Accepted

Solution code:
import sys
import collections
n = int(input())
vl = list(map(int,input().split()))

v1 = collections.Counter(vl[0::2])
v2 = collections.Counter(vl[1::2])


v1s = []
for a,b in v1.items():
    v1s.append((b,a))

v2s = []
for a,b in v2.items():
    v2s.append((b,a))

v1s = sorted(v1s)
v2s = sorted(v2s)

if v1s[-1][1] != v2s[-1][1]:
    print(n - v1s[-1][0] - v2s[-1][0])
else:
    if len(v2s) == 1:
        p2 = 0
    else:
        p2 = v2s[-2][0]
    if len(v1s) == 1:
        p1 = 0
    else:
        p1 = v1s[-2][0]
    ans = min(n - v1s[-1][0] - p2, n - p1 - v2s[-1][0])
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


def solve(n, v):
    origin_odd = [v[i] for i in range(n) if i % 2 == 1]
    origin_even = [v[i] for i in range(n) if i % 2 == 0]

    o = Counter(origin_odd).most_common()
    e = Counter(origin_even).most_common()

    o.sort(key=lambda k: k[1])
    e.sort(key=lambda k: k[1])
    # (a, b)
    o_p = o[-1]
    e_p = e[-1]
    ans = 0
    if len(o) == 1 and len(e) == 1:
        if o_p[0] == e_p[0]:
            ans = n//2
        else:
            ans = 0
    elif len(o) == 1:
        if o_p[0] == e_p[0]:
            e_s = e[-2]
            ans = n - (o_p[1]+e_s[1])
        else:
            ans = n - (o_p[1]+e_p[1])
    elif len(e) == 1:
        if o_p[0] == e_p[0]:
            o_s = o[-2]
            ans = n - (e_p[1]+o_s[1])
        else:
            ans = n - (e_p[1]+o_p[1])
    else:
        o_s = o[-2]
        e_s = e[-2]
        if o_p[0] == e_p[0]:
            o_s = o[-2]
            e_s = e[-2]
            ans = n-(max(o_p[1]+e_s[1], o_s[1]+e_p[1]))
        else:
            ans = n-(o_p[1]+e_p[1])

    return ans


if __name__ == '__main__':
    n = int(input())
    v = list(map(int, input().split()))
    print(solve(n, v))

Submission status: Accepted

Solution code:
from collections import Counter

n=int(input())
V=list(map(int,input().split()))
V_1=[(0,0)]+sorted(Counter([V[i] for i in range(n) if i%2==0]).items(), key=lambda x:x[1])
V_2=[(0,0)]+sorted(Counter([V[j] for j in range(n) if j%2==1]).items(), key=lambda x:x[1])

a,b=V_1[-1][0],V_2[-1][0]

if a==b:
    if V_1[-2][1]>V_2[-2][1]:a=V_1[-2][0]
    else:b=V_2[-2][0]

cnt=0
for i in range(n):
    if i%2==0:
        if V[i]!=a:cnt +=1
    else:
        if V[i]!=b:cnt +=1

print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
l = list(map(int, input().split(' ')))
l_odd = l[0::2]
l_even = l[1::2]
dict_odd = Counter(l_odd)
dict_even = Counter(l_even)
most_odd = dict_odd.most_common()[0][0]
most_even = dict_even.most_common()[0][0]

if dict_odd.most_common()[0][1] == dict_even.most_common()[0][1] == N / 2:
    if most_odd == most_even:
        ans = N / 2
    else:
        ans = 0

elif dict_odd.most_common()[0][1] == N / 2:
    ans = N / 2 - dict_even.most_common()[0][1]

elif dict_even.most_common()[0][1] == N / 2:
    ans = N / 2 - dict_odd.most_common()[0][1]

elif most_odd != most_even:
    ans = N - dict_odd.most_common()[0][1] - dict_even.most_common()[0][1]

else:
    ans = N - max(dict_odd.most_common()[0][1] + dict_even.most_common()[1][1], dict_even.most_common()[0][1] + dict_odd.most_common()[1][1])

print(int(ans))

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int,input().split()))
v_odd = []
v_even = []
d_odd = {}
d_even = {}
o = []
e = []
for i in range(n//2):
    a = v[2*i]
    b = v[2*i+1]
    if a in d_odd:
        d_odd[a] += 1
    else:
        d_odd[a] = 1
        o.append(a)
    if b in d_even:
        d_even[b] += 1
    else:
        d_even[b] = 1
        e.append(b)
O = 0
E = 0
o_max = 0
o_second = 0
e_max = 0
e_second = 0
for i in o:
    x = d_odd[i]
    if x >= o_max:
        O = i
        o_max = x
d_odd.pop(O)
o.remove(O)
for i in o:
    x = d_odd[i]
    if x >= o_second:
        o_second = x
for i in e:
    y = d_even[i]
    if y >= e_max:
        E = i
        e_max = y
d_even.pop(E)
e.remove(E)
for i in e:
    y = d_even[i]
    if y >= e_second:
        e_second = y
if O == E:
    m = max(o_max+e_second,e_max+o_second)
else:
    m = o_max + e_max
print(n-m)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = list(map(int, input().split()))

evenv, oddv = Counter(v[0::2]).most_common(), Counter(v[1::2]).most_common()
ans = 0
if evenv == oddv and len(evenv) == 1:
    ans = n // 2
elif evenv[0][0] == oddv[0][0]:
    ans = n - max(evenv[0][1] + oddv[1][1], evenv[1][1] + oddv[0][1])
else:
    ans = n - evenv[0][1] - oddv[0][1]
print(ans)

Submission status: Accepted

Solution code:
# ABC111 C - \/\/\/\
import collections
N=int(input())
V=list(map(int,input().split()))
a=[V[i] for i in range(N) if i%2==0]
b=[V[i] for i in range(N) if i%2==1]

a=collections.Counter(a)
b=collections.Counter(b)
a=a.most_common()
b=b.most_common()
if len(a)==len(b)==1:
    if a[0][0]==b[0][0]:
        print(int(N/2))
    else:
        print(0)
elif len(a)==1 and len(b)>=2:
    print(N-(b[0][1] if a[0][0]!=b[0][0] else b[1][1])-a[0][1])
elif len(a)>=2 and len(b)==1:
    print(N-(a[0][1] if a[0][0]!=b[0][0] else a[1][1])-b[0][1])
elif len(a)>=2 and len(a)>=2:
    print(min(N-(b[0][1] if a[0][0]!=b[0][0] else b[1][1])-a[0][1],N-(a[0][1] if a[0][0]!=b[0][0] else a[1][1])-b[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
array = list(map(int,input().split()))
if len(set(array)) == 1:
    print(n//2)
    exit()
gu = []
ki = []
for i in range(n):
    if i % 2 == 0:
        gu.append(array[i])
    else:
        ki.append(array[i])
gu_c = Counter(gu)
ki_c = Counter(ki)
gu_m = gu_c.most_common()
ki_m = ki_c.most_common()
if gu_m[0][0] != ki_m[0][0]:
    print(n-(gu_m[0][1]+ki_m[0][1]))
elif gu_m[0][1] < ki_m[0][1]:
    print(n-(gu_m[1][1]+ki_m[0][1]))
elif gu_m[0][1] > ki_m[0][1]:
    print(n-(gu_m[0][1]+ki_m[1][1]))
else:
    print(n-(gu_m[0][1]+max(gu_m[1][1],ki_m[1][1])))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
array = list(map(int,input().split()))
if len(set(array)) == 1:
    print(n//2)
    exit()
gu = []
ki = []
for i in range(n):
    if i % 2 == 0:
        gu.append(array[i])
    else:
        ki.append(array[i])
gu_c = Counter(gu)
ki_c = Counter(ki)
if gu_c.most_common()[0][0] != ki_c.most_common()[0][0]:
    print(n-(gu_c.most_common()[0][1]+ki_c.most_common()[0][1]))
elif gu_c.most_common()[0][1] < ki_c.most_common()[0][1]:
    print(n-(gu_c.most_common()[1][1]+ki_c.most_common()[0][1]))
elif gu_c.most_common()[0][1] > ki_c.most_common()[0][1]:
    print(n-(gu_c.most_common()[0][1]+ki_c.most_common()[1][1]))
else:
    print(n-(gu_c.most_common()[0][1]+max(gu_c.most_common()[1][1],ki_c.most_common()[1][1])))

Submission status: Accepted

Solution code:
import collections
N=int(input())
V=list(map(int,input().split()))
a=[V[i] for i in range(N) if i%2==0]
b=[V[i] for i in range(N) if i%2==1]

a=collections.Counter(a)
b=collections.Counter(b)
a=a.most_common()
b=b.most_common()
if len(a)==len(b)==1:
    if a[0][0]==b[0][0]:
        print(int(N/2))
    else:
        print(0)
elif len(a)==1 and len(b)>=2:
    print(N-(b[0][1] if a[0][0]!=b[0][0] else b[1][1])-a[0][1])
elif len(a)>=2 and len(b)==1:
    print(N-(a[0][1] if a[0][0]!=b[0][0] else a[1][1])-b[0][1])
elif len(a)>=2 and len(a)>=2:
    print(min(N-(b[0][1] if a[0][0]!=b[0][0] else b[1][1])-a[0][1],N-(a[0][1] if a[0][0]!=b[0][0] else a[1][1])-b[0][1]))

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int,input().split()))
ve = [v[2*i] for i in range(n//2)]
vo = [v[2*i + 1] for i in range(n//2)]
Cve = collections.Counter(ve)
Cvo = collections.Counter(vo)
Cve1 = Cve.most_common()[0]
Cvo1 = Cvo.most_common()[0]
if len(Cve) == 1 and len(Cvo) == 1: #eo両方1種類
    if Cve1[0] == Cvo1[0]: ans = n//2 #eo top同じ
    else: ans = 0 #eo異なる
elif len(Cve) == 1 and len(Cvo) > 1: #eのみ1種類
    Cvo2 = Cvo.most_common()[1]
    if Cve1[0] == Cvo1[0]: ans = n//2 - Cvo2[1] #eo top同じ
    else: ans = n//2 - Cvo1[1] #eo top異なる
elif len(Cve) > 1 and len(Cvo) == 1: #oのみ1種類
    Cve2 = Cve.most_common()[1]
    if Cve1[0] == Cvo1[0]: ans = n//2 - Cve2[1] #eo top同じ
    else: ans = n//2 - Cve1[1] #eo top異なる
else:
    Cve2 = Cve.most_common()[1]
    Cvo2 = Cvo.most_common()[1]
    if Cve1[0] != Cvo1[0]: ans = n - Cve1[1] - Cvo1[1] #top異なる
    if Cve1[0] == Cvo1[0]:
        if Cve1[1] < Cvo1[1]: ans = n - Cve2[1] - Cvo1[1]
        elif Cve2[1] > Cvo2[1] : ans = n - Cve2[1] - Cvo1[1]
        else: ans = n - Cve1[1] - Cvo2[1]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a1=[a[i] for i in range(0,len(a),2)]
a2=[a[i] for i in range(1,len(a),2)]

import collections
c1=collections.Counter(a1)
c2=collections.Counter(a2)

c1=sorted(c1.items(),key=lambda x:x[1],reverse=True)
c2=sorted(c2.items(),key=lambda x:x[1],reverse=True)
ans=0
if c1[0][0]==c2[0][0]:
  if len(c1)==1:
    ans+=(n//2)
  elif c1[1][1]<=c2[1][1]:
    ans+=(n//2)-c1[0][1]
    ans+=(n//2)-c2[1][1]
  else:
    ans+=(n//2)-c1[1][1]
    ans+=(n//2)-c2[0][1]
else:
  ans+=(n//2)-c1[0][1]
  ans+=(n//2)-c2[0][1]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a1=[a[i] for i in range(0,len(a),2)]
a2=[a[i] for i in range(1,len(a),2)]

import collections
c1=collections.Counter(a1)
c2=collections.Counter(a2)

c1=sorted(c1.items(),key=lambda x:x[1],reverse=True)
c2=sorted(c2.items(),key=lambda x:x[1],reverse=True)
ans=0
if c1[0][0]==c2[0][0]:
  if len(c1)==1:
    ans+=(n//2)
  elif c1[1][1]<=c2[1][1]:
    ans+=(n//2)-c1[0][1]
    ans+=(n//2)-c2[1][1]
  else:
    ans+=(n//2)-c1[1][1]
    ans+=(n//2)-c2[0][1]
else:
  ans=0
  ans+=(n//2)-c1[0][1]
  ans+=(n//2)-c2[0][1]

print(ans)

Submission status: Accepted

Solution code:
# 2019-11-10 17:11:42(JST)
import sys
import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
import operator as op
# from scipy.misc import comb # float
# import numpy as np
# import statistics

def main():
    
    n, *v = (int(x) for x in sys.stdin.read().split())

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


    o_c = collections.Counter(odds)
    e_c = collections.Counter(evens)
    
    o_c = sorted(list((k, v) for k, v in o_c.items()), key=op.itemgetter(1), reverse=True)
    e_c = sorted(list((k, v) for k, v in e_c.items()), key=op.itemgetter(1), reverse=True)

    ans = n
    if o_c[0][0] != e_c[0][0]:
        ans = n - o_c[0][1] - e_c[0][1]
    else:
        if len(o_c) == 1 and len(e_c) == 1:
            ans = n - n // 2
        elif len(o_c) == 1 and len(e_c) >= 2:
            ans = n - e_c[1][1]
        elif len(o_c) >= 2 and len(e_c) == 1:
            ans = n - o_c[1][1]
        else:
            ans = n - max(o_c[0][1] + e_c[1][1], o_c[1][1] + e_c[0][1])
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
ls=[int(s) for s in input().split()]
dic1={}
dic2={}
i=0
while i<N:
  if ls[i] in dic1:
    dic1[ls[i]]+=1
  else:
    dic1[ls[i]]=1
  if ls[i+1] in dic2:
    dic2[ls[i+1]]+=1
  else:
    dic2[ls[i+1]]=1  
  i+=2
ls1=sorted(list(dic1.items()),key=lambda x : -x[1])
ls2=sorted(list(dic2.items()),key=lambda x : -x[1])
#print(ls1)
#print(ls2)
#ls1とls2の一位の値が異なる場合
if ls1[0][0]!=ls2[0][0]:
  print(N-ls1[0][1]-ls2[0][1])
elif len(ls1)==1:
  #ls1,ls2がともに1種類の場合
  if len(ls2)==1:
    print(N//2)
  #ls1が1種類、ls2が複数種類の場合
  else:
    print(N//2-ls2[1][1])
#ls2が1種類、ls1が複数種類の場合
elif len(ls2)==1:
  print(N//2-ls1[1][1])
else:
  #ls1の2位の数がls2の2位の数より多いとき
  if ls1[1][1]>ls2[1][1]:
    #ls1を2位にそろえ、ls2を1位にそろえる
    print(N-ls1[1][1]-ls2[0][1])
  else:
    #逆なら逆
    print(N-ls2[1][1]-ls2[0][1])

Submission status: Accepted

Solution code:
import collections
N = int(input())
V = list(map(int,input().split()))
V_even = V[::2]
V_odd = V[1::2]
V_even_counter = collections.Counter(V_even)
V_odd_counter = collections.Counter(V_odd)
if V_even_counter.most_common()[0][0] != V_odd_counter.most_common()[0][0]:
    ans = N - V_even_counter.most_common()[0][1] - V_odd_counter.most_common()[0][1]
else:
    if len(V_even_counter) > 1 and len(V_odd_counter) > 1:
        ans = N - max(V_even_counter.most_common()[0][1] + V_odd_counter.most_common()[1][1],\
            V_even_counter.most_common()[1][1] + V_odd_counter.most_common()[0][1])
    elif len(V_even_counter) == 1 and len(V_odd_counter) > 1:
        ans = N - V_even_counter.most_common()[0][1] - V_odd_counter.most_common()[1][1]
    elif len(V_even_counter) > 1 and len(V_odd_counter) == 1:
        ans = N - V_even_counter.most_common()[1][1] + V_odd_counter.most_common()[0][1]
    else:
        ans = N//2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
v = [x for x in input().split()]
if len(set(v)) == 1:
  print(len(v)//2)
  exit()
v_even = Counter(v[::2])
v_odd = Counter(v[1::2])
v_even_mc = v_even.most_common()
v_odd_mc = v_odd.most_common()

if v_even_mc[0] != v_odd_mc[0]:
  ans = (len(v)//2 - v_even_mc[0][1]) + (len(v)//2 - v_odd_mc[0][1])
else:  
  ans_e = (len(v)//2 - v_even_mc[0][1]) + (len(v)//2 - v_odd_mc[1][1])
  ans_o = (len(v)//2 - v_even_mc[1][1]) + (len(v)//2 - v_odd_mc[0][1])
  ans = min(ans_e, ans_o)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
v = input().split()
o = {"":0}
e = {"":0}
for s in v[::2]:
    if s in o:
        o[s] += 1
    else:
        o[s] = 1

for s in v[1::2]:
    if s in e:
        e[s] += 1
    else:
        e[s] = 1

o = sorted(o.items(), key=lambda x:x[1])
e = sorted(e.items(), key=lambda x:x[1])

if o[-1][0] != e[-1][0]:
    print(n-o[-1][1]-e[-1][1])
else:
    print(n - max(o[-1][1]+e[-2][1], o[-2][1]+e[-1][1]))

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import groupby

n = int(stdin.readline().rstrip())

li = list(map(int,stdin.readline().rstrip().split()))

li1 = []
li2 = []

for i in range(n):
    if i % 2 == 0:
        li1.append(li[i])
    else:
        li2.append(li[i])

li1.sort()
li2.sort()

lin = []
lin2 = []

for key, value in groupby(li1):
    lin.append((key,len(list(value))))
for key, value in groupby(li2):
    lin2.append((key,len(list(value))))

lin.sort(key=lambda x:(x[1]))
lin2.sort(key=lambda x:(x[1]))

point = 0

if lin[-1][0] == lin2[-1][0] and lin[-1][1] == lin2[-1][1]:
    if len(lin) == 1:
        print(n//2)
    else:
        if lin[-2][1] >= lin2[-2][1]:
            print(n-lin[-2][1]-lin2[-1][1])
        else:
            print(n-lin[-1][1]-lin2[-2][1])

elif lin[-1][0] == lin2[-1][0]:
    if len(lin) == 1:
        print(n//2-lin2[-2][1])
    elif len(lin2) == 1:
        print(n//2-lin[-2][1])
    else:
        if lin[-2][1] >= lin2[-2][1]:
            print(n-lin[-2][1]-lin2[-1][1])
        else:
            print(n-lin[-1][1]-lin2[-2][1])
else:
    print(n-lin[-1][1]-lin2[-1][1])

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import groupby

n = int(stdin.readline().rstrip())

li = list(map(int,stdin.readline().rstrip().split()))

li1 = []
li2 = []

for i in range(n):
    if i % 2 == 0:
        li1.append(li[i])
    else:
        li2.append(li[i])

li1.sort()
li2.sort()

lin = []
lin2 = []

for key, value in groupby(li1):
    lin.append((key,len(list(value))))
for key, value in groupby(li2):
    lin2.append((key,len(list(value))))

lin.sort(key=lambda x:(x[1]))
lin2.sort(key=lambda x:(x[1]))

point = 0

if lin[-1][0] == lin2[-1][0] and lin[-1][1] == lin2[-1][1]:
    if len(lin) == 1:
        print(n//2)
    else:
        if lin[-2][1] >= lin2[-2][1]:
            print(n-lin[-2][1]-lin2[-1][1])
        else:
            print(n-lin[-1][1]-lin2[-2][1])

elif lin[-1][0] == lin2[-1][0]:
    if len(lin) == 1:
        print(n//2-lin2[-2][1])
    elif len(lin2) == 1:
        print(n//2-lin[-2][1])
    else:
        if lin[-2][1] >= lin2[-2][1]:
            print(n-lin[-1][1]-lin2[-2][1])
        else:
            print(n-lin[-2][1]-lin2[-1][1])
else:
    print(n-lin[-1][1]-lin2[-1][1])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
v = list(map(lambda x: int(x), input().split(' ')))


import collections
even_frq_dict = collections.defaultdict(int)
len_even = 0
for i in range(0, n, 2):
    even_frq_dict[v[i]] += 1
    len_even += 1

len_odd = 0
odd_frq_dict = collections.defaultdict(int)
for i in range(1, n, 2):
    odd_frq_dict[v[i]] += 1
    len_odd += 1


even_frqs = list(sorted(list(even_frq_dict.items()), key=lambda x: x[1]))
odd_frqs = list(sorted(list(odd_frq_dict.items()), key=lambda x: x[1]))

if len(even_frqs) == 1 and len(odd_frqs) == 1:
    if even_frqs[0][0] == odd_frqs[0][0]:
        print(min(len_even, len_odd))
    else:
        print(0)
    exit()
elif len(even_frqs) == 1:
    print(len_odd - odd_frqs[-1][1])
    exit()
elif len(odd_frqs) == 1:
    print(len_even - even_frqs[-1][1])
    exit()


e_num1, e_frq1 = even_frqs[-1]
e_num2, e_frq2 = even_frqs[-2]
o_num1, o_frq1 = odd_frqs[-1]
o_num2, o_frq2 = odd_frqs[-2]

ans = 0
if e_num1 == o_num1:
    cnd1 = (len_even - e_frq2) + (len_odd - o_frq1)
    cnd2 = (len_even - e_frq1) + (len_odd - o_frq2)
    ans = min(cnd1, cnd2)

else:
    ans = (len_even - e_frq1) + (len_odd - o_frq1)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
v = list(map(int, input().split()))
v1 = Counter(v[::2]).most_common(2)+[(0,0)]
v2 = Counter(v[::-2]).most_common(2)+[(0,0)]
if v1[0][0] == v2[0][0]:
    print(N -max(v1[0][1]+v2[1][1],
                 v1[1][1]+v2[0][1]))
else:
    print(N-v1[0][1]-v2[0][1])