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:
from operator import itemgetter
from collections import Counter
def main():
    va = []
    vb = []
    n = int(input())
    *v, = map(int, input().split())
    for i in range(len(v)):
        if i % 2 == 0:
            va.append(v[i])
        else:
            vb.append(v[i])
    a1 = Counter(va).most_common()
    a2 = Counter(vb).most_common()
    how_many_a1 = a1[0][1]
    how_many_a2 = a2[0][1]
    if a1[0][0] != a2[0][0]:
        print(n - how_many_a1 - how_many_a2)
    else:
        if len(a1) == 1 and len(a2) == 1:
            print(int(n/2))
        elif len(a1) == 1:
            print(len(vb) - a2[1][1])
        elif  len(a2) == 1:
            print(len(va) - how_many_a1)
        elif (len(va) - how_many_a1) + (len(vb) - a2[1][1])  > (len(vb) - how_many_a2) + (len(va) - a1[1][1]):
            print((len(vb) - how_many_a2) + (len(va) - a1[1][1]))
        else:
            print((len(va) - how_many_a1) + (len(vb) - a2[1][1]))
main()

Submission status: Accepted

Solution code:
from collections import Counter

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

even = Counter(V[1::2]).most_common() + [(0, 0)]
odd = Counter(V[::2]).most_common() + [(0, 0)]

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

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
import collections
from operator import itemgetter
#以下てんぷら
def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
n = ip()                                     #入力整数1つ
#n,q= (int(i) for i in input().split())       #入力整数横2つ以上
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
#こっから本文
dlist = []
ulist = []
for i in range(n):
    if i%2:
        ulist.append(a[i])
    else: dlist.append(a[i])
ulist.sort()
dlist.sort()

firstupper = 0
secondupper = 0
maxupper = ulist[0]
maxlower = dlist[0]
c = 1
cnum = ulist[0]
for i in range(1,len(ulist)):
    if ulist[i] == cnum:
        c+=1
    else:
        cnum = ulist[i]
        if c > firstupper:
            maxupper = ulist[i-1]
            secondupper = firstupper
            firstupper = c
        elif c > secondupper and firstupper >= c:
            secondupper = c
        c = 1
if c > firstupper:
    maxupper = ulist[i-1]
    secondupper = firstupper
    firstupper = c
elif c > secondupper and firstupper >= c:
    secondupper = c


firstlower = 0
secondlower = 0
c = 1
cnum = dlist[0]
for i in range(1,len(dlist)):
 #   print("f",dlist[i])
    if dlist[i] == cnum:
        c+=1
    else:
        cnum = dlist[i]
        if c > firstlower:
            maxlower = dlist[i-1]
            secondlower = firstlower
            firstlower = c
        elif c > secondlower and firstlower >= c:
            secondlower = c
        c = 1
if c > firstlower:
  #  print("reqmet")
    maxlower = dlist[i-1]
    secondlower = firstlower
    firstlower = c
elif c > secondlower and firstlower >= c:
    secondlower = c
c = 0
"""
print(maxupper,maxlower)
print(firstupper,firstlower)
print(secondupper,secondlower)
"""
p = len(ulist)
if maxupper == maxlower:
    print(min(p-firstupper+p-secondlower,p-secondupper+p-firstlower))
else: print(2*p - firstupper - firstlower)

Submission status: Accepted

Solution code:
import collections

N = int(input())
V = list(int(i) for i in input().split())
even = V[0::2]
odd =  V[1::2]
all_length = len(V)
even = collections.Counter(even).most_common(2)
odd = collections.Counter(odd).most_common(2)
if (even[0][0] != odd[0][0]):
    print(all_length - even[0][1] - odd[0][1])
else:
    if(len(even) == 1 and len(odd) == 1):
        print(all_length // 2)
    elif(len(even)==1):
        print(all_length // 2 - odd[1][1])
    elif(len(odd)==1):
        print(all_length // 2 - even[1][1])    
    else:
        print(min(all_length - even[0][1] - odd[1][1], all_length - even[1][1] - odd[0][1]))

Submission status: Accepted

Solution code:
import collections
n = int(input())
v_ = list(map(int,input().split()))
v_g = v_[0:len(v_):2]
v_k = v_[1:len(v_):2]

counter_g = collections.Counter(v_g)
counter_g = sorted(counter_g.items(),key=lambda x:x[1])[::-1]
counter_k = collections.Counter(v_k)
counter_k = sorted(counter_k.items(),key=lambda x:x[1])[::-1]


if counter_g[0][0] != counter_k[0][0]:
  print(n - counter_g[0][1] - counter_k[0][1])
else:
  if len(counter_g) == 1 and len(counter_k) == 1:
    print(int(n/2))
  else:
    if len(counter_g) == 1:
      print(len(counter_k)-counter_k[1][1])
    elif len(counter_k) == 1:
      print(len(counter_g)-counter_g[1][1])
    else:
      print(min(n-counter_g[0][1]-counter_k[1][1],n-counter_g[1][1]-counter_k[0][1]))

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int, input().split()))
v1 = v[::2]
v2 = v[1::2]
c1 = collections.Counter(v1).most_common()
c2 = collections.Counter(v2).most_common()

if c1[0][0] != c2[0][0]:
  print(n - c1[0][1] - c2[0][1])
else:
  if len(c1) == len(c2) == 1:
    print(n // 2)
  elif len(c1) == 1:
    print(len(v2)-c2[1][1])
  elif len(c2) == 1:
    print(len(v1)-c1[1][1])
  else:
    print(min(n-c1[0][1]-c2[1][1], n-c1[1][1]-c2[0][1]))

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int, input().split()))
v1 = v[::2]
v2 = v[1::2]
c1 = collections.Counter(v1).most_common()
c2 = collections.Counter(v2).most_common()

if c1[0][0] != c2[0][0]:
  print(len(v1)-c1[0][1] + len(v2)-c2[0][1])
else:
  if len(c1) == len(c2) == 1:
    print(n // 2)
  elif len(c1) == 1:
    print(len(v2)-c2[1][1])
  elif len(c2) == 1:
    print(len(v1)-c1[1][1])
  else:
    print(min(len(v1)-c1[0][1]+len(v2)-c2[1][1], len(v1)-c1[1][1]+len(v2)-c2[0][1]))

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int, input().split()))
v1 = [v[i*2+1] for i in range(n//2)]
v2 = [v[i*2] for i in range(n//2)]
c1 = collections.Counter(v1).most_common()
c2 = collections.Counter(v2).most_common()

if len(set(v1)) == len(set(v2)) == 1:
  if c1[0][0] != c2[0][0]:
    print(0)
  else:
    print(min(len(v1), len(v2)))
elif len(set(v1))==1:
  if c1[0][0] != c2[0][0]:
    print(sum([i[1] for i in c2])-c2[0][1])
  else:
    print(sum([i[1] for i in c2])-c2[1][1])
elif len(set(v2))==1:
  if c1[0][0] != c2[0][0]:
    print(sum([i[1] for i in c1])-c1[0][1])
  else:
    print(sum([i[1] for i in c1])-c1[1][1])
else:
  if c1[0][0] != c2[0][0]:
    print((sum([i[1] for i in c1])-c1[0][1]) + (sum([i[1] for i in c2])-c2[0][1]))
  else:
    print(min((sum([i[1] for i in c1])-c1[0][1])+(sum([i[1] for i in c2])-c2[1][1]),
              (sum([i[1] for i in c1])-c1[1][1])+(sum([i[1] for i in c2])-c2[0][1])))

Submission status: Accepted

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline
n = int(input())
v = list(map(int, input().split()))
v1, v2 = [v[i] for i in range(n) if i % 2 == 0], [v[i] for i in range(n) if i % 2]
c1, c2 = Counter(v1).most_common(), Counter(v2).most_common()
l1, l2 = len(c1), len(c2)
if l1 == 1 and l2 == 1:
    if c1[0][0] == c2[0][0]:
        print(n // 2)
    else:
        print(0)
elif l1 == 1:
    if c1[0][0] == c2[0][0]:
        print(n//2 - c2[1][1])
    else:
        print(n//2 - c2[0][1])
elif l2 == 1:
    if c1[0][0] == c2[0][0]:
        print(n//2 - c1[1][1])
    else:
        print(n//2 - c1[0][1])
else:
    if c1[0][0] == c2[0][0]:
        if c1[0][1] + c2[1][1] < c1[1][1] + c2[0][1]:
            print(n - c1[1][1] - c2[0][1])
        else:
            print(n - c1[0][1] - c2[1][1])
    else:
        print(n - c1[0][1] - c2[0][1])

Submission status: Accepted

Solution code:
from collections import Counter

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

list_o = list[::2]
list_e = list[1::2]

cnt_o = Counter(list_o).most_common(2)
cnt_e = Counter(list_e).most_common(2)

#最頻値が同じ
if cnt_o[0][0] == cnt_e[0][0]:
    #値が1つ
    if len(cnt_o) == 1 and len(cnt_e) ==1:
        print(int(len(list) / 2))
    elif len(cnt_o) == 1 and len(cnt_e) != 1:
        print(len(list) - cnt_o[0][1] - cnt_e[1][1])
    elif len(cnt_o) != 1 and len(cnt_e) == 1:
        print(len(list) - cnt_o[0][1] - cnt_e[1][1])
    else:
        print(min(len(list) - cnt_o[1][1] - cnt_e[0][1], len(list) - cnt_o[0][1] - cnt_e[1][1]))
#最頻値が異なる
elif cnt_o[0][0] != cnt_e[0][0]:
    if len(cnt_o) == 1 and len(cnt_e) ==1:
        print(0)
    else:
        print(len(list) - cnt_o[0][1] - cnt_e[0][1])

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = [int(i) for i in input().split()]
l = collections.Counter(v[1:len(v):2])
r = collections.Counter(v[0:len(v):2])
r = r.most_common()
l = l.most_common()
if l[0][0] == r[0][0]:
    if (len(l) > 1) & (len(r) > 1):
        ans = min(n-l[1][1]-r[0][1], n-l[0][1]-r[1][1])
    else:
        ans = min(l[0][1], r[0][1])
elif l[0][0] != r[0][0]:
    ans = (n - l[0][1] - r[0][1])
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')


# globals
N = 0
V = []


def set_inputs():
    global N, V
    N = get_int()
    V = get_li()
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    x = V[0::2]
    y = V[1::2]
    x_counter = Counter(x)
    y_counter = Counter(y)
    x_val, x_cnt = x_counter.most_common(1)[0]
    y_val, y_cnt = y_counter.most_common(1)[0]
    ans = N - x_cnt - y_cnt
    if x_val == y_val:
        if len(x_counter) == 1:
            if len(y_counter) == 1:
                ans = N // 2
            else:
                _, y_cnt = y_counter.most_common(2)[1]
                ans = N - x_cnt - y_cnt
        else:
            if len(y_counter) == 1:
                _, x_cnt = x_counter.most_common(2)[1]
                ans = N - x_cnt - y_cnt
            else:
                _, x_cnt_2 = x_counter.most_common(2)[1]
                _, y_cnt_2 = y_counter.most_common(2)[1]
                ans = min(N - x_cnt - y_cnt_2, N - x_cnt_2 - y_cnt)
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return zip(*(
        [t(x) for t, x in zip(types, input().split())]
        for _ in range(n)
    ))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter
n = int(input())
v = list(map(int, input().split()))
if len(set(v)) == 1:
    print(len(v) // 2)
    sys.exit()

even = Counter(v[::2])
odd = Counter(v[1::2])

# 以下ifがない場合，n = 6 v = [1,1,1,1,1,2,2,1,1,1]でWA
# v[0],v[2],...,v[2//n]で最も多く出現する数字が
# v[1],v[3],...,v[2//n-1]で最も多く出現する数字と等しい場合
if even.most_common()[0][0] == odd.most_common()[0][0]:
    print(min(len(v) - odd.most_common()[0][1] - even.most_common()[1][1], len(v) - odd.most_common()[1][1] - even.most_common()[0][1]))
    sys.exit()

print(len(v) - even.most_common()[0][1] - odd.most_common()[0][1])

Submission status: Accepted

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

import collections


'''
奇数番目の数列
'''
v_odd = []
for i in range(0,n,2):
  v_odd.append(v[i])

c_odd = collections.Counter(v_odd)
c_common_odd = c_odd.most_common()


'''
偶数番目の数列
'''
v_even = []
for i in range(1,n,2):
  v_even.append(v[i])

c_even = collections.Counter(v_even)
c_common_even = c_even.most_common()


'''
後処理

下記の4種類でパターン別に出力
また、各パターンごとに一番カウント数が大きい数字を比較する。

a.奇数・偶数共に数字が２種類以上
b.奇数については数字が一種類
c.偶数については数字が一種類
d.両方の数字がそれぞれ一種類
'''

# a.奇数・偶数共に数字が２種類以上
if len(c_common_odd) >= 2 and len(c_common_even) >= 2:
  # 一番多くカウントした数字が異なる
  if c_common_odd[0][0] != c_common_even[0][0]:
    ans = n - c_common_odd[0][1] - c_common_even[0][1]
  # 一致する
  else:
    # 奇数の方がカウント数が大きい
    ans_1 = n - c_common_odd[1][1] - c_common_even[0][1]
    ans_2 = n - c_common_odd[0][1] - c_common_even[1][1]
    ans = min(ans_1, ans_2)

# b.奇数については数字が一種類
elif len(c_common_odd) < 2 and len(c_common_even) >= 2:
  # 一番カウント数が大きい数字が一致している
  if c_common_odd[0][0] == c_common_even[0][0]:
    ans = int(n / 2) - c_common_even[1][1]
  else:
    ans = int(n / 2) - c_common_even[0][1]

# c.偶数については数字が一種類
elif len(c_common_odd) >= 2 and len(c_common_even) < 2:
  # 一番カウント数が大きい数字が一致している
  if c_common_odd[0][0] == c_common_even[0][0]:
    ans = int(n / 2) - c_common_odd[1][1]
  else:
    ans = int(n / 2) - c_common_odd[0][1]

# d.両方の数字がそれぞれ一種類
else:
  if c_common_odd[0][0] == c_common_even[0][0]:
    ans = int(n / 2)
  else:
    ans = 0

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int,input().split()))
count=0
A=[[0,i] for i in range(10**5+1)]
Z=[[0,i] for i in range(10**5+1)]
for i in range(n):
  if i%2==0:
    A[v[i]][0]+=1
  else:
    Z[v[i]][0]+=1
A.sort()
Z.sort()
def wfs(x,y):
  if A[x][1]!=Z[y][1]:
    return n-A[x][0]-Z[y][0]
  else:
    return min(wfs(x,y-1),wfs(x-1,y))
print(wfs(-1,-1))

Submission status: Accepted

Solution code:
from collections import Counter

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

even = Counter(V[1::2]).most_common() + [(0, 0)]
odd = Counter(V[::2]).most_common() + [(0, 0)]

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

Submission status: Accepted

Solution code:
n=int(input())
v=list(map(int, input().split()))
import collections
v1=v[0::2]
v11=collections.Counter(v1)
v2=v[1::2]
v22=collections.Counter(v2)
o=v11.most_common()+([(0,0)])
e=v22.most_common()+([(0,0)])
if o[0][0]!=e[0][0]:
    print(n-o[0][1]-e[0][1])
else:
    print(min(n-o[0][1]-e[1][1],n-o[1][1]-e[0][1]))

Submission status: Accepted

Solution code:
import collections as col

N = int(input())
v = [int(x) for x in input().split()]
v1,v2 = sorted(v[::2]), sorted(v[1::2])

c1,n1 = col.Counter(v1).most_common()[0]
c2,n2 = col.Counter(v2).most_common()[0]

if c1 != c2:
    print(N - n1 - n2)
else:
    if n1 + n2 == N:
        print(int(N/2))
    else:
        c12,n12 = col.Counter(v1).most_common()[1]
        c22,n22 = col.Counter(v2).most_common()[1]
        if n1 + n22 > n12 + n2:
            print(N - n1 - n22)
        else:
            print(N - n12 - n2)

Submission status: Accepted

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

even = [[i,0] for i in range(10**5+1)]
odd = [[i,0] for i in range(10**5+1)]
for e,o in zip(a[::2],a[1::2]):
    even[e][1] += 1
    odd[o][1] += 1

even.sort(key=lambda x: -x[1])
odd.sort(key=lambda x: -x[1])

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

Submission status: Accepted

Solution code:
n = int(input())
V = list(map(int,input().split()))
even = [0] * 100001
odd  = [0] * 100001

for v in V[1::2]:
    even[v] += 1
for v in V[::2]:
    odd[v]  += 1
maxeven = max(even)
maxodd  = max(odd)
if(even.index(maxeven) != odd.index(maxodd)):
    print(n - maxeven - maxodd)
else:
    even.sort()
    odd.sort()
    print(min(n-even[-1]-odd[-2],n-even[-2]-odd[-1]))

Submission status: Accepted

Solution code:
from operator import itemgetter
n = int(input())
v = [int(x) for x in input().split()]
li = [[[0 for _ in range(2)] for _ in range(10**5)] for _ in range(2)]
for index,i in enumerate(v):
    li[index%2][i-1][0]+=1
    li[index%2][i-1][1]=i

li[0].sort(key=itemgetter(0),reverse=True)
li[1].sort(key=itemgetter(0),reverse=True)

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

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int,input().split()))
even = v[0::2]
odd = v[1::2]
ce = collections.Counter(even)
oe = collections.Counter(odd)
if len(set(odd)) ==1 and len(set(even)) == 1:
    if ce.most_common()[0][0] != oe.most_common()[0][0]:
        print("0")
    else:
        print(int(n/2))
elif len(set(odd)) ==1 and len(set(even)) != 1:
    print(int(n/2-ce.most_common()[0][1]))
elif len(set(odd)) !=1 and len(set(even)) == 1:
    print(int(n/2-oe.most_common()[0][1]))
else:
    if ce.most_common()[0][0] != oe.most_common()[0][0]:
        print(n-ce.most_common()[0][1]-oe.most_common()[0][1])
    else:
        print(n - max(ce.most_common()[1][1]+oe.most_common()[0][1], oe.most_common()[1][1]+ce.most_common()[0][1]))

Submission status: Accepted

Solution code:
n = int(input())
V = list(map(int,input().split()))
dict_V1,dict_V2 = {},{}
if(n%2 == 0):
  len_v1,len_v2 = n//2,n//2
else:
  len_v1,len_v2 = n//2 + 1,n//2

i = 0
for v in V:
  if(i%2 == 0):
   if(str(v) in dict_V1): dict_V1[str(v)] += 1
   else: dict_V1[str(v)] = 1
  else:
   if(str(v) in dict_V2): dict_V2[str(v)] += 1
   else: dict_V2[str(v)] = 1
  i += 1
 
min_cost = 0
max_k_v1 = max(dict_V1, key=dict_V1.get)
max_k_v2 = max(dict_V2, key=dict_V2.get)

#
'''
(ex) 
V1 = [3,3,3,4],V2 = [3,3,3,2]
->(書き換え) V1 = [3,3,3,3],V2 = [3,3,3,3]
-> 文字一種類に ->NG
のケースを考慮した場合分けが必要
'''
#

if(max_k_v1 != max_k_v2):
  min_cost += len_v1 - max(dict_V1.values())
  min_cost += len_v2 - max(dict_V2.values())
else:
  min_cost1,min_cost2 = 0,0
  tmp_dict = dict_V1.copy()
  tmp_dict[max_k_v1] = 0
  min_cost1 += len_v1 - max(tmp_dict.values())
  min_cost1 += len_v2 - max(dict_V2.values())
  
  dict_V2[max_k_v1] = 0
  min_cost2 += len_v1 - max(dict_V1.values())
  min_cost2 += len_v2 - max(dict_V2.values())
  min_cost = min(min_cost1,min_cost2)
  
print(min_cost)

Submission status: Accepted

Solution code:
import collections

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

e = v[::2]
o = v[1::2]

ce = collections.Counter(e)
co = collections.Counter(o)

if ce.most_common(1)[0][0] != co.most_common(1)[0][0]:
  print(n - ce.most_common(1)[0][1] - co.most_common(1)[0][1])
else:
  if len(ce) ==1 and len(co) ==1:
    print(n//2)
  elif len(ce) ==1:
    print(n//2 - co.most_common(2)[1][1])
  elif len(co) ==1:
    print(n//2 - ce.most_common(2)[1][1])
  else:
    a = ce.most_common(2)[0][1] + co.most_common(2)[1][1]
    b = ce.most_common(2)[1][1] + co.most_common(2)[0][1]
    print(min([n-a, n-b]))

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

input = sys.stdin.readline
n = int(input())
v_list = list(map(int, input().split()))
even_dic = defaultdict(int)
odd_dic = defaultdict(int)
for i in range(n):
    if i % 2 == 0:
        even_dic[v_list[i]] += 1
    else:
        odd_dic[v_list[i]] += 1
even = sorted(even_dic.items(), key=lambda x:x[1], reverse=True)
odd = sorted(odd_dic.items(), key=lambda x:x[1], reverse=True)
if even[0][0] == odd[0][0]:
    if len(even) > 1 and len(odd) > 1:
        ans = min((n-even[1][1]-odd[0][1]), (n-even[0][1]-odd[1][1]))
    elif len(even) > 1:
        ans = int(n/2) - even[1][1]
    elif len(odd) > 1:
        ans = int(n/2) - odd[1][1]
    else:
        ans = int(n/2)
else:
    ans = n - even[0][1] - odd[0][1]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import collections
n = int(input())
a_n = np.array(input().split(),dtype='int')
diff = a_n[1:] - a_n[:-1]
if len(set(diff))==2:
    print(0)
elif len(set(diff))==1:
    print(int(n/2))
else:
    odd_th = collections.Counter(a_n[1::2]).most_common()
    eve_th = collections.Counter(a_n[0::2]).most_common()
    if odd_th[0][0]==eve_th[0][0]:
        if odd_th[0][1]>eve_th[0][1]:
            odd_ = odd_th[0][1]
            eve_ = eve_th[1][1]
        elif odd_th[0][1]<eve_th[0][1]:
            odd_ = odd_th[1][1]
            eve_ = eve_th[0][1]
        else:
            if odd_th[1][1]>=eve_th[1][1]:
                odd_ = odd_th[1][1]
                eve_ = eve_th[0][1]
            else:
                odd_ = odd_th[0][1]
                eve_ = eve_th[1][1] 
    else:
            odd_ = odd_th[0][1]
            eve_ = eve_th[0][1]
    print(n-odd_-eve_)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input().strip())
Vs = list(map(int,input().strip().split()))


def get_word_num(Ss):
    hists = {}
    for v in Ss:
        if v in hists.keys():
            hists[v] += 1
        else:
            hists[v] = 1

    return sorted(hists.items(),reverse = True,key = lambda h: h[1])


even_ar = Vs[0::2]
odd_ar = Vs[1::2]

#even_hist = Counter(even_ar).most_common(2)
even_hist = get_word_num(even_ar)
#odd_hist = Counter(odd_ar).most_common(2)
odd_hist = get_word_num(odd_ar)

if even_hist[0][0] != odd_hist[0][0]:
    print(N - even_hist[0][1] - odd_hist[0][1])
else:
    if len(even_hist) == 1 and len(odd_hist) == 1:
        print(len(even_ar))
    else:
        print(N - max(even_hist[0][1] + odd_hist[1][1], even_hist[1][1] + odd_hist[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input().strip())
Vs = list(map(int,input().strip().split()))


def get_word_num(Ss):
    hists = {}
    for v in Ss:
        if v in hists.keys():
            hists[v] += 1
        else:
            hists[v] = 1

    return sorted(hists.items(),reverse = True)


even_ar = Vs[0::2]
odd_ar = Vs[1::2]

even_hist = Counter(even_ar).most_common(2)
odd_hist = Counter(odd_ar).most_common(2)

if even_hist[0][0] != odd_hist[0][0]:
    print(N - even_hist[0][1] - odd_hist[0][1])
else:
    if len(even_hist) == 1 and len(odd_hist) == 1:
        print(len(even_ar))
    else:
        print(N - max(even_hist[0][1] + odd_hist[1][1], even_hist[1][1] + odd_hist[0][1]))

Submission status: Accepted

Solution code:
from collections import Counter

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

Odd = Counter(S[::2]).most_common()+([(0,0)])
Even = Counter(S[1::2]).most_common()+([(0,0)])

if Odd[0][0] != Even[0][0]:
	print(n-Odd[0][1]-Even[0][1])
else:
	print(min(n-Odd[1][1]-Even[0][1],n-Odd[0][1]-Even[1][1]))

Submission status: Accepted

Solution code:
import collections

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

cnt1=collections.Counter(L[0::2]).most_common(2)
cnt2=collections.Counter(L[1::2]).most_common(2)

if len(cnt1)==1:
    cnt1.append((0,0))
if len(cnt2)==1:
    cnt2.append((0,0))

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

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int,input().split()))
v1 = [v[2*i] for i in range(n//2)]
v2 = [v[2*i+1] for i in range(n//2)]
c1 = collections.Counter(v1)
c2 = collections.Counter(v2)
c1most = c1.most_common()[0]
c2most = c2.most_common()[0]
if int(c1most[0]) != int(c2most[0]):
    print(n - int(c1most[1]) - int(c2most[1]))
elif c1most[1] == n//2 and c2most[1] == n//2:
    print(n//2)
elif c1most[1] == n//2:
    print(n//2 - int(c2.most_common()[1][1]))
elif c2most[1] == n//2:
    print(n//2 - int(c1.most_common()[1][1]))
else:
    c1most2 = c1.most_common()[1]
    c2most2 = c2.most_common()[1]
    if int(c1most2[1]) >= int(c2most2[1]):
        print(n - int(c1most2[1]) - int(c2most[1]))
    else:
        print(n - int(c2most2[1]) - int(c1most[1]))

Submission status: Accepted

Solution code:
from collections import Counter
 
n = int(input())
v = list(map(int,input().split()))
vo = v[::2]
ve = v[1::2]
co = Counter(vo).most_common()
ce = Counter(ve).most_common()
co.append((-1, 0)) #vが[1,1,1,1]のようなケースへの対応
ce.append((-2, 0)) #頻度の降順に並んでいるのでこの書き方で十分
if co[0][0] != ce[0][0]: #偶奇の最頻の要素が異なる場合
    print(n - co[0][1] - ce[0][1])
else:
    res1 = n - co[0][1] - ce[1][1] #偶数側を書き換える場合
    res2 = n - co[1][1] - ce[0][1] #奇数側を書き換える場合
    print(min(res1, res2))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

li_ki = li_input[0::2]
d_ki = defaultdict(int)
for ki in li_ki:
    d_ki[ki] += 1


li_gu = li_input[1::2]
d_gu = defaultdict(int)
for gu in li_gu:
    d_gu[gu] += 1

ki_sort = sorted([(k, v) for k,v in d_ki.items()], key=lambda x : x[1], reverse=True)
gu_sort = sorted([(k, v) for k,v in d_gu.items()], key=lambda x : x[1], reverse=True)

ki_cnt = 0
gu_cnt = 0

if ki_sort[0][0] != gu_sort[0][0]:
    print(n - ki_sort[0][1] - gu_sort[0][1])
else:
    ki_1 = ki_sort[0][1]
    ki_2 = ki_sort[1][1] if len(ki_sort) > 1 else 0

    gu_1 = gu_sort[0][1]
    gu_2 = gu_sort[1][1] if len(gu_sort) > 1 else 0

    print(min(n - ki_1 - gu_2, n - ki_2 - gu_1))

Submission status: Accepted

Solution code:
import bisect

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

a = sorted(v[::2])
b = sorted(v[1::2])	

def get_frequent_num(a):
  a_max = 0
  a_max_num = 0
  i=0
  prev_i = 0
  max_value, sec_value = 0, 0
  max_num, sec_num = 0, 0
  while i < len(a):
      i = bisect.bisect(a, a[i])
      num = (i - prev_i)
      if num > max_num:
        sec_num = max_num
        max_num = num
        sev_value = max_value
        max_value = a[i-1]
      elif num > sec_num:
        sec_num = num
        sec_value = a[i-1]
      prev_i = i
  return max_num, max_value, sec_num, sec_value

amn, amv, asn, asv = get_frequent_num(a)
bmn, bmv, bsn, bsv = get_frequent_num(b)

if amv != bmv:
  print(n-amn-bmn)
elif amv == bmv:
  if amn == asn or bmn == bsn:
    print(n-amn-bmn)
  elif asn < bsn:
    print(n-bsn-amn)
  else:
    print(n-bmn-asn)

Submission status: Accepted

Solution code:
import collections
 
def counter_top(c, exclude):
  for a in c.most_common():
    if a[0] != exclude:
      return a
  return (object(), 0)
 
def conv_seq(seq, exclude=None):
  c = collections.Counter(seq)
  mc = counter_top(c, exclude)
  return sum(c.values()) - mc[1], mc[0]
 
input()
vals = list(map(int, input().split()))
s1, a = conv_seq(vals[::2])
s2, b = conv_seq(vals[1::2])
if a != b:
  print(s1 + s2)
else:
  s1_new, _ = conv_seq(vals[::2], exclude=b)
  s2_new, _ = conv_seq(vals[1::2], exclude=a)
  print(min(s1_new + s2, s1 + s2_new))

Submission status: Accepted

Solution code:
import collections
n = int(input())
v = list(map(int, input().split()))
o = [v[i] for i in range(n) if i % 2 == 0]
e = [v[i] for i in range(n) if i % 2 == 1]
odd = collections.Counter(o)
even = collections.Counter(e)
odd_num = odd.most_common(2)
even_num = even.most_common(2)
odd_num.append(('', 0))
even_num.append(('', 0))
if odd_num[0][0] == even_num[0][0]:
    print(n - max(odd_num[0][1] + even_num[1][1], odd_num[1][1] + even_num[0][1]))
else:
    print(n - odd_num[0][1] - even_num[0][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()[:2]
A.append(("x",0))
B = b.most_common()[:2]
B.append(("x",0))
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:
# coding: utf-8
# Your code here!
# Your code here!
import collections
n=int(input())
e=[]
o=[]
a=input().split(" ")
for i in range(n):
    if(i%2==0):
        e.append(a[i])
    else:
        o.append(a[i])
#print(e,o)
e_c=collections.Counter(e)
e_m=e_c.most_common()
e1=e_m[0][1]

o_c=collections.Counter(o)
o_m=o_c.most_common()
o1=o_m[0][1]

ans=n-(e1+o1)
if(o_m[0][0]==e_m[0][0]):
    if(len(o_m)<2):
        o2=0
    if(len(e_m)<2):
        e2=0
    else:
        e2=e_m[1][1]
        o2=o_m[1][1]
    ans_list=[n-(o1+e2),n-(e1+o2)]
    ans=min(ans_list)
print(ans)

Submission status: Accepted

Solution code:
def get_second(a):
	if len(a) == 1:
		return 0
	else:
		return a[1][0]

n = int(input())
a = [int(v) for v in input().split()]
nums = [{}, {}]
for i, v in enumerate(a):
	if v in nums[i % 2]:
		nums[i % 2][v] += 1
	else:
		nums[i % 2][v] = 1
evens = sorted([(v, k) for k, v in nums[0].items()], reverse=True)
odds = sorted([(v, k) for k, v in nums[1].items()], reverse=True)
if evens[0][1] != odds[0][1]:
	print(n - evens[0][0] - odds[0][0])
else:
	print(n - max(evens[0][0] + get_second(odds), get_second(evens) + odds[0][0]))

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]
c_even = collections.Counter(v_even)
c_odd = collections.Counter(v_odd)
even_list = c_even.most_common()+[(-1, int(0))]
odd_list = c_odd.most_common()+[(-1, int(0))]
# print(even_list, odd_list)
for test in even_list:
    if odd_list[0][0] != test[0]:
        even_cd = test
        cost1 = odd_list[0][1] + test[1]
        break
for  test in odd_list:
    if even_list[0][0] != test[0]:
        odd_cd = test
        cost2 = even_list[0][1] + test[1]
        break

if cost1>=cost2:
    print(n-cost1)
else:
    print(n-cost2)

Submission status: Accepted

Solution code:
#Miccさんのコードを参考にさせていただきました
from collections import Counter

n = int(input())
v = list(map(int, input().split()))
e = v[1::2] #偶数番号
o = v[0::2] #奇数番号
ans = 0

a = Counter(e) #Counterオブジェクトを作成(dictのサブクラスでキーに要素、値に出現回数をもつ)
b = Counter(o)

c = a.most_common(2) #出現回数が2番目に多い数までのCounterオブジェクト作成
d = b.most_common(2)

if c[0][0] != d[0][0]: #出現回数が１番多いものが違う時
    ans = n -c[0][1] -d[0][1]
else:                  #出現回数が１番多いものが同じ時
    if len(c) == 1:
        if len(d) == 1:
            ans = n//2
        else:
            ans = n -d[0][1]
    elif len(d) == 1 and len(c) != 1:
        ans = n -c[0][1]
    elif c[0][1] > d[0][1]:
        ans = n -c[0][1] -d[1][1]
    else:
        if c[0][1]==d[0][1]:
            ans = min(n-c[0][1]-d[1][1],n-c[1][1]-d[0][1])
        else:
            ans = n -c[1][1] -d[0][1]
    
print(ans)