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 sys
input = sys.stdin.readline
from collections import Counter
n = int(input())
v = list(map(int,input().split()))
a = []
b = []
for i in range(n):
    if i % 2 == 0:
        a.append(v[i])
    else:
        b.append(v[i])
 
a = Counter(a)
amc = a.most_common()
b = Counter(b)
bmc = b.most_common()
amc.append((0,0))
bmc.append((0,0))
rewrite = 0
 
if amc[0][0] == bmc[0][0]:
    rewrite = max(amc[0][1]+bmc[1][1],amc[1][1]+bmc[0][1])
else:
    rewrite = amc[0][1]+bmc[0][1]
print(n-rewrite)

Submission status: Accepted

Solution code:
from collections import Counter

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

# most_commonを使うためにリストをCounterオブジェクトに渡す
Counter_evens = Counter(vlist[::2])
Counter_odds = Counter(vlist[1::2])


# even_indexes
top2_even = Counter_evens.most_common(2)  # [(一番多い要素,その出現回数),(2番目に多い要素,その出現回数)]

E_most_common = top2_even[0][0]
Emnum = top2_even[0][1]

if len(top2_even) == 2:
    E_2nd_common = top2_even[1][0]
    E2num = top2_even[1][1]
else:
    E_2nd_common = 'foo'
    E2num = 0

# odd_indexes
top2_odd = Counter_odds.most_common(2)

O_most_common = top2_odd[0][0]
Omnum = top2_odd[0][1]

if len(top2_odd) == 2:
    O_2nd_common = top2_odd[1][0]
    O2num = top2_odd[1][1]
else:
    O_2nd_common = 'foo'
    O2num = 0


if E_most_common == O_most_common:  # most commonどうしが衝突してしまっている。
    # もし偶数番目・奇数番目をどちらもmost commonに書き換えると、3つ目の条件「数列は1種類の数ではだめ」が満たされない。
    # なので奇数番目・偶数番目のどちらかは2番目に多いやつに書き換える必要がある

    # 偶数番目をmost commonに、oddsを2nd commonにそろえる場合に書き換えないといけない要素の数
    x = len(vlist[::2]) - Emnum + len(vlist[1::2]) - O2num
    # 逆の場合
    y = len(vlist[::2]) - E2num + len(vlist[1::2]) - Omnum

    print(min(x, y))
else:
    # most common は衝突していない、つまり異なる数。よって3つ目の条件「数列は1種類の数ではだめ」は満たされている。
    # 両方とも most commonに書き換えてOK
    print(len(vlist[::2]) - Emnum + len(vlist[1::2]) - Omnum)

Submission status: Accepted

Solution code:
from collections import*
n,*v=map(int,open(0).read().split())
a=Counter(v[::2]).most_common()+[(0,0)]
b=Counter(v[1::2]).most_common()+[(0,0)]
if a[0][0]==b[0][0]:
    print(min(n-a[0][1]-b[1][1],n-a[1][1]-b[0][1]))
else:
    print(n-a[0][1]-b[0][1])

Submission status: Accepted

Solution code:
from collections import*
n,*v=map(int,open(0).read().split())
a=Counter(v[::2]).most_common()
b=Counter(v[1::2]).most_common()
if a[0][0]==b[0][0]:
    if len(a)>1and len(b)>1:
        print(min(n-a[0][1]-b[1][1],n-a[1][1]-b[0][1]))
    elif len(a)>1:
        print(n-a[1][1]-b[0][1])
    elif len(b)>1:
        print(n-a[0][1]-b[1][1])
    else:
        print(n//2)
else:
    print(n-a[0][1]-b[0][1])

Submission status: Accepted

Solution code:

import sys
from collections import *

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

if not n:
    print(0)
    sys.exit()

n1 = Counter(v[::2])
n2 = Counter(v[1::2])

max1 = 0

n1 = sorted([(count, k) for k, count in n1.items()], reverse=True)
n2 = sorted([(count, k) for k, count in n2.items()], reverse=True)

if n1[0][1] == n2[0][1]:
    if len(n1) == 1 and len(n2) == 1:
        print(n//2)
        sys.exit()
    if len(n1) == 1 or n1[0][0] > n2[0][0]:
        print(n - n1[0][0] - n2[1][0])
        sys.exit()
    if len(n2) == 1 or n1[0][0] < n2[0][0]:
        print(n - n1[1][0] - n2[0][0])
        sys.exit()
    if n1[0][0] == n2[0][0]:
        if n1[1][0] >= n2[1][0]:
            print(n - n1[1][0] - n2[0][0])
        elif n1[1][0] < n2[1][0]:
            print(n - n1[0][0] - n2[1][0])
        sys.exit()

count = n - n1[0][0] - n2[0][0]
print(count)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
cnt = [[0 for j in range(10**5)] for i in range(2)]
for i in range(n):
  cnt[i%2][v[i]-1] += 1
maxce = max(cnt[0])
maxco = max(cnt[1])
maxve = -1
maxvo = -1
for i in range(len(cnt[0])):
  if(cnt[0][i] == maxce):
    maxve = i
    break
for i in range(len(cnt[1])):
  if(cnt[1][i] == maxco):
    maxvo = i
    break
if(maxvo != maxve):
  print((n//2 - maxco) + (n - n//2 - maxce))
else:
  cnt[0].pop(maxve)
  cnt[1].pop(maxvo)
  maxce2 = max(cnt[0])
  maxco2 = max(cnt[1])
  print(min((n//2 - maxco2) + (n - n//2 - maxce), (n//2 - maxco) + (n - n//2 - maxce2)))

Submission status: Accepted

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

nums1 = nums[0::2]
nums2 = nums[1::2]

bug1 = [0] * 100001
bug2 = [0] * 100001

for num in nums1:
    bug1[num] += 1

for num in nums2:
    bug2[num] += 1

max_v_1 = max(bug1)
max_i_1 = bug1.index(max_v_1)
max_v_2 = max(bug2)
max_i_2 = bug2.index(max_v_2)

if max_i_1 == max_i_2:
    bug1[max_i_1] = 0
    bug2[max_i_2] = 0
    sec_max_v_1 = max(bug1)
    sec_max_v_2 = max(bug2)
    print(n - min(max_v_1, max_v_2) - max(sec_max_v_1, sec_max_v_2))
else:
    print(n - max_v_1 - max_v_2)

Submission status: Accepted

Solution code:
import collections
N = int(input())
a = input().split( )

ret = [0]

odds = []
evens = []
for i in range(N):
    if i % 2 == 0:
        odds.append(int(a[i]))
    else:
        evens.append(int(a[i]))

codds = collections.Counter(odds)
cevens = collections.Counter(evens)

if codds.most_common()[0][0] ==cevens.most_common()[0][0]:
    if codds.most_common()[0][1] > cevens.most_common()[0][1]:
        codds = sorted(list(codds.values()))
        cevens = sorted(list(cevens.values()))
        if len(codds) > 1:
            codds.pop(-1)
        else:
            codds = [0]
        if len(cevens) > 1:
            cevens.pop(-2)
    elif codds.most_common()[0][1] < cevens.most_common()[0][1]:
        codds = sorted(list(codds.values()))
        cevens = sorted(list(cevens.values()))
        if len(codds) > 1:
            codds.pop(-2)
        if len(cevens) > 1:
            cevens.pop(-1)
        else:
            cevens = [0]
    else:
        if len(codds) == 1 and len(cevens) == 1:
            codds = [0]
            cevens = sorted(list(cevens.values()))
        elif codds.most_common()[1][1] > cevens.most_common()[1][1]:
            codds = sorted(list(codds.values()))
            cevens = sorted(list(cevens.values()))
            codds.pop(-2)
            cevens.pop(-1)
        else:
            codds = sorted(list(codds.values()))
            cevens = sorted(list(cevens.values()))
            codds.pop(-1)
            cevens.pop(-2)

else:
    codds = sorted(list(codds.values()))
    cevens = sorted(list(cevens.values()))
    if len(codds) > 1:
        codds.pop(-1)
    else:
        codds= [0]
    if len(cevens) > 1:
        cevens.pop(-1)
    else:
        cevens = [0]

print(sum(codds) + sum(cevens))

Submission status: Accepted

Solution code:
import collections
n=int(input())
list=list(map(int,input().split()))
#print(list)
list_1=list[::2]
list_2=list[1::2]
#print(list_1)
#print(list_2)
a=collections.Counter(list_1)
b=collections.Counter(list_2)
#print(a.most_common()[0]==b.most_common()[0])
#print(len(list_1),a.most_common()[0][1])
if((a.most_common()[0]==b.most_common()[0]) & (a.most_common()[0][1]!=len(list_1))):#2つの数列の最大をとる数が等しい場合どちらかは最大個数の数に、もう１つはその次に多い数に置き換える。
  #print('test2')
  if(n-a.most_common()[0][1]-b.most_common()[1][1]>n-a.most_common()[1][1]-b.most_common()[0][1]):
  	print(n-a.most_common()[1][1]-b.most_common()[0][1])
  else:
    print(n-a.most_common()[0][1]-b.most_common()[1][1])
elif ((a.most_common()==b.most_common()) & (a.most_common()[0][1]==len(list_1))):#２つの数列の全要素が一致する場合どちらかの数字を変更
  #print('test1')
  print(n//2)
else:
  #print(a)
  #print(b)
  #print(a.most_common())
  #print(b.most_common())
  #print(a.most_common()[0][1])
  #print(b.most_common()[0][1])

  print(n-a.most_common()[0][1]-b.most_common()[0][1])

Submission status: Accepted

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

os=dict()
es=dict()
for i in range(N//2):
    os[v[2*i]] = os.get(v[2*i], 0) + 1
    es[v[2*i+1]] = es.get(v[2*i+1], 0) + 1

osr=sorted(os.items(), key=lambda x: (x[1], x[0]))
esr=sorted(es.items(), key=lambda x: (x[1], x[0]))

osv = osr.pop()
esv = esr.pop()

o1n = N//2 - osv[1]
e1n = N//2 - esv[1]

count = o1n + e1n
if osv[0] == esv[0]:
    if len(osr) == 0:
        o2n = N//2
    else:
        o2n = N//2 - osr.pop()[1]
    if len(esr) == 0:
        e2n = N//2
    else:
        e2n = N//2 - esr.pop()[1]

    count = min(o1n + e2n, o2n + e1n)

print(count)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
NN=N//2
l=list(map(int,input().split()))
l1=[l[2*i] for i in range(NN)]
l2=[l[2*i+1] for i in range(NN)]
l1c=Counter(l1)
l2c=Counter(l2)
r1=l1c.most_common()
r2=l2c.most_common()
if r1[0][0]==r2[0][0]:
  if r1[0][1]==NN and r2[0][1]==NN:
    ans=NN
  elif r1[0][1]>r2[0][1]:
    ans=N-r1[0][1]-r2[1][1]
  elif r1[0][1]<r2[0][1]:
    ans=N-r1[1][1]-r2[0][1]
  else:
    if r1[1][1]>r2[1][1]:
      ans=N-r1[1][1]-r2[0][1]
    else:
      ans=N-r1[0][1]-r2[1][1]
else:
  ans=N-r1[0][1]-r2[0][1]
print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
all_list_str = input()

# n = 4
# all_list_str = "3 1 3 2"

# n = 6
# all_list_str = "105 119 105 119 105 119"

# # n = 4
# # all_list_str = "1 1 1 1"

all_list = list(map(int, all_list_str.split()))

odd_list = [all_list[i] for i in range(1, n, 2)]
even_list = [all_list[i] for i in range(0, n - 1, 2)]

c = collections.Counter(odd_list)
c_odd_list = c.most_common()

c = collections.Counter(even_list)
c_even_list = c.most_common()

if (c_odd_list[0][0] != c_even_list[0][0]):
    odd_target = c_odd_list[0][0]
    even_target = c_even_list[0][0]
elif (c_odd_list[0][1] > c_even_list[0][1]):
    odd_target = c_odd_list[0][0]
    even_target = c_even_list[1][0]
elif (c_odd_list[0][1] < c_even_list[0][1]):
    odd_target = c_odd_list[1][0]
    even_target = c_even_list[0][0]
else:
    if (len(c_odd_list) == 1 and len(c_even_list) == 1):
        odd_target = c_odd_list[0][0]
        even_target = odd_target + 1    #Set dummy num
    elif (len(c_odd_list) == 1):
        odd_target = c_odd_list[0][0]
        even_target = c_even_list[1][0]
    elif (len(c_even_list) == 1):
        odd_target = c_odd_list[1][0]
        even_target = c_even_list[0][0]
    elif (c_odd_list[1][1] > c_even_list[1][1]):
        odd_target = c_odd_list[1][0]
        even_target = c_even_list[0][0]
    else:
        odd_target = c_odd_list[0][0]
        even_target = c_even_list[1][0]

print(str(len(odd_list) - odd_list.count(odd_target) + len(even_list) - even_list.count(even_target)))

Submission status: Accepted

Solution code:
from collections import *
N = int(input())
As = list(map(int, input().split()))

def solve(arr1, arr2):
    cnt1 = Counter(arr1)
    cnt2 = Counter(arr2)

    most1 = cnt1.most_common()
    most2 = cnt2.most_common()

    # 1文字しかないときの処理を簡単にするためにダミーのエントリを追加
    most1.append(('_', 0))
    most2.append(('_', 0))

    # (i) 最頻出文字が異なる → 単に最頻出文字を採用すればよい
    if most1[0][0] != most2[0][0]:
        return len(arr1) * 2 - most1[0][1] - most2[0][1]
    # (ii) 最頻出文字が同じ → 2通り試して良い方を採用
    else:
        ans1 = len(arr1) * 2 - most1[0][1] - most2[1][1]
        ans2 = len(arr1) * 2 - most1[1][1] - most2[0][1]
        return min(ans1, ans2)

arr1 = As[0::2]
arr2 = As[1::2]
print(solve(arr1, arr2))

Submission status: Accepted

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

temp1 = [0] * (100000 + 1)

for i in range(N//2):
    a = V[2*i]
    temp1[a] += 1

te1 = []
for i in range(100001):
    te1.append((temp1[i], i))

temp2 = [0] * (100000 + 1)

for i in range(N//2):
    a = V[2*i+1]
    temp2[a] += 1

te2 = []
for i in range(100001):
    te2.append((temp2[i], i))

te1.sort()
te1.reverse()
te2.sort()
te2.reverse()

if te1[0][1] != te2[0][1]:
    res = N - te1[0][0] - te2[0][0]
else:
    res = N - max(te1[1][0] + te2[0][0], te1[0][0] + te2[1][0])

print(res)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
V = [int(x) for x in input().split()]
if all(v == V[0] for v in V):
    print(N // 2)
    quit()
even = Counter([v for i, v in enumerate(V) if i % 2 == 0])
odd = Counter([v for i, v in enumerate(V) if i % 2 != 0])
evens = even.most_common()
odds = odd.most_common()
if evens[0][0] != odds[0][0]:
    print(N - evens[0][1] - odds[0][1])
elif len(evens) == len(odds) == 1:
    print(N // 2)
elif len(evens) == 1:
    print(min(N - odds[0][1], N - len(evens) - odds[1][1]))
elif len(odds) == 1:
    print(min(N - evens[0][1], N - len(odds) - evens[1][1]))
else:
    print(min(N - evens[0][1] - odds[1][1], N - evens[1][1] - odds[0][1]))

Submission status: Accepted

Solution code:
n=int(input())
s=input().split()
s=[int(o) for o in s]
left=dict()
right=dict()
def count_nums(mydict,o):
  if o in mydict.keys():
    mydict[o]+=1
  else:
    mydict[o]=1
flag=True
for o in s:
  if flag:
    count_nums(left,o)
    flag=False
  else:
    count_nums(right,o)
    flag=True
myleft=sorted(left.keys(),key=lambda x: left[x])
myright=sorted(right.keys(),key=lambda x: right[x])
if myleft[-1]!=myright[-1]:
  print( len(s)-left[myleft[-1]]-right[myright[-1]])
else:
  a0,b0=left[myleft[-1]],right[myright[-1]]
  a1=0 if len(myleft)==1 else left[myleft[-2]]
  b1=0 if len(myright)==1 else right[myright[-2]]
  print( len(s)-max(a0+b1,a1+b0))

Submission status: Accepted

Solution code:
# Title: /\/\/\/
# Link: https://arc103.contest.atcoder.jp/tasks/arc103_a

import sys
import collections

sys.setrecursionlimit(10 ** 6)

read_single_int = lambda: int(sys.stdin.readline().strip())
read_list_int = lambda: list(map(int, sys.stdin.readline().strip().split(' ')))


def solution(n: int, vs: list):
    map_a = collections.defaultdict(lambda: 0)
    map_b = collections.defaultdict(lambda: 0)

    for i in range(0, n, 2):
        map_a[vs[i]] += 1
        map_b[vs[i+1]] += 1

    pair_a = [(k, map_a[k]) for k in map_a.keys()]        
    pair_b = [(k, map_b[k]) for k in map_b.keys()]

    pair_a = sorted(pair_a, key=lambda x: x[1], reverse= True)
    pair_b = sorted(pair_b, key=lambda x: x[1], reverse= True)

    first_a, second_a = pair_a[0], (0, 0) if len(pair_a) == 1 else pair_a[1]
    first_b, second_b = pair_b[0], (0, 0) if len(pair_b) == 1 else pair_b[1]

    if first_a[0] != first_b[0]:
        return n//2-first_a[1] + n//2-first_b[1]
    else:
        if first_a[1] + second_b[1] < second_a[1] + first_b[1]:
            return n//2-second_a[1] + n//2-first_b[1]

    return n//2-first_a[1] + n//2-second_b[1]


def main():
    n = read_single_int()
    vs = read_list_int()
    print(solution(n, vs))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# Title: /\/\/\/
# Link: https://arc103.contest.atcoder.jp/tasks/arc103_a

import sys
import collections

sys.setrecursionlimit(10 ** 6)

read_single_int = lambda: int(sys.stdin.readline().strip())
read_list_int = lambda: list(map(int, sys.stdin.readline().strip().split(' ')))


def solution(n: int, vs: list):
    map_a = collections.defaultdict(lambda: 0)
    map_b = collections.defaultdict(lambda: 0)

    for i in range(0, n, 2):
        map_a[vs[i]] += 1
        map_b[vs[i+1]] += 1


    pair_a = [(k, map_a[k]) for k in map_a.keys()]        
    pair_b = [(k, map_b[k]) for k in map_b.keys()]

    pair_a = sorted(pair_a, key=lambda x: x[1], reverse= True)
    pair_b = sorted(pair_b, key=lambda x: x[1], reverse= True)

    first_a, second_a = pair_a[0], (0, 0) if len(pair_a) == 1 else pair_a[1]
    first_b, second_b = pair_b[0], (0, 0) if len(pair_b) == 1 else pair_b[1]

    if first_a[0] != first_b[0]:
        return n//2-first_a[1] + n//2-first_b[1]
    else:
        if first_a[1] + second_b[1] < second_a[1] + first_b[1]:
            return n//2-second_a[1] + n//2-first_b[1]
        else:
            return n//2-first_a[1] + n//2-second_b[1]

    return 0


def main():
    n = read_single_int()
    vs = read_list_int()
    print(solution(n, vs))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input().strip())
vs = list(map(int, input().strip().split()))
oddv = [v for i, v in enumerate(vs) if i % 2 == 0]
evenv = [v for i, v in enumerate(vs) if i % 2 == 1]

oddc = Counter(oddv)
evenc = Counter(evenv)
if oddc.most_common(1)[0][0] != evenc.most_common(1)[0][0]:
  print(len(oddv) - oddc.most_common(1)[0][1] + len(evenv) - evenc.most_common(1)[0][1])
else:
  if len(oddc.most_common()) == 1 and len(evenc.most_common()) == 1:
    print(len(oddv))
  elif len(oddc.most_common()) == 1:
    print(len(evenv) - evenc.most_common(2)[1][1])
  elif len(evenc.most_common()) == 1:
    print(len(oddv) - oddc.most_common(2)[1][1])
  else:
    print(min(len(oddv) - oddc.most_common(2)[1][1] + len(evenv) - evenc.most_common(1)[0][1], len(oddv) - oddc.most_common(1)[0][1] + len(evenv) - evenc.most_common(2)[1][1]))

Submission status: Accepted

Solution code:
n = int(input())
v1 = [0 for i in range(n//2)]
v2 = [0 for i in range(n//2)] 

tmp = list(map(int, input().split()))
for i in range(n//2):
    v1[i], v2[i] = tmp[2 * i], tmp[2 * i + 1]

v1set = {n : 0}
v2set = {n+1 : 0}
for i in range(n//2):
    if v1[i] in v1set:
        v1set[v1[i]] += 1
    else:
        v1set[v1[i]] = 1
    if v2[i] in v2set:
        v2set[v2[i]] += 1
    else:
        v2set[v2[i]] = 1
lis1 = sorted(list(v1set.items()),reverse=True,key = lambda x: x[1])
lis2 = sorted(list(v2set.items()),reverse=True,key = lambda x: x[1])
i1 = 0
i2 = 0
tmp1 = lis1[0][0]
tmp2 = lis2[0][0]
if tmp1 == tmp2:
    print(min(n - lis1[0][1] - lis2[1][1],n - lis2[0][1] - lis1[1][1]))
else:
    print(n - lis1[0][1] - lis2[0][1])

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
v=list(map(int,input().split()))
odd=v[1::2]
even=v[0::2]
a=Counter(odd)
b=Counter(even)
if a.most_common()[0][0]!=b.most_common()[0][0]:
    print(n-a.most_common()[0][1]-b.most_common()[0][1])
else:
    if len(a)>1 and len(b)>1:
        print(min(n-a.most_common()[0][1]-b.most_common()[1][1],n-a.most_common()[1][1]-b.most_common()[0][1]))
    elif len(a)>1 and len(b)==1:
        print(n-a.most_common()[1][1]-b.most_common()[0][1])
    elif len(a)==1 and len(b)>1:
        print(n-a.most_common()[0][1]-b.most_common()[1][1])
    elif len(a)==1 and len(b)==1:
        print(n//2)

Submission status: Accepted

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

nums1 = nums[0::2]
nums2 = nums[1::2]

bug1 = [0] * 100001
bug2 = [0] * 100001

for num in nums1:
    bug1[num] += 1

for num in nums2:
    bug2[num] += 1

max_v_1 = max(bug1)
max_i_1 = bug1.index(max_v_1)
max_v_2 = max(bug2)
max_i_2 = bug2.index(max_v_2)

if max_i_1 == max_i_2:
    bug1[max_i_1] = 0
    bug2[max_i_2] = 0
    sec_max_v_1 = max(bug1)
    sec_max_v_2 = max(bug2)
    print(n - max(max_v_1, max_v_2) - max(sec_max_v_1, sec_max_v_2))
else:
    print(n - max_v_1 - max_v_2)

Submission status: Accepted

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

nums1 = nums[0::2]
nums2 = nums[1::2]

bug1 = [0] * 100001
bug2 = [0] * 100001

for num in nums1:
    bug1[num] += 1

for num in nums2:
    bug2[num] += 1

max_v_1 = max(bug1)
max_i_1 = bug1.index(max_v_1)
max_v_2 = max(bug2)
max_i_2 = bug2.index(max_v_2)

if max_i_1 == max_i_2:
    bug1[max_i_1] = 0
    bug2[max_i_2] = 0
    sec_max_v_1 = max(bug1)
    sec_max_v_2 = max(bug2)
    print(n - min(max_v_1, max_v_2) - max(sec_max_v_1, sec_max_v_2))
else:
    print(n - max_v_1 - max_v_2)

Submission status: Accepted

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

nums1 = nums[0::2]
nums2 = nums[1::2]

bug1 = [0] * 1000000
bug2 = [0] * 1000000

for num in nums1:
    bug1[num] += 1

for num in nums2:
    bug2[num] += 1

max_v_1 = max(bug1)
max_i_1 = bug1.index(max_v_1)
max_v_2 = max(bug2)
max_i_2 = bug2.index(max_v_2)

if max_i_1 == max_i_2:
    bug1[max_i_1] = 0
    bug2[max_i_2] = 0
    sec_max_v_1 = max(bug1)
    sec_max_v_2 = max(bug2)
    print(n - min(max_v_1, max_v_2) - max(sec_max_v_1, sec_max_v_2))
else:
    print(n - max_v_1 - max_v_2)

Submission status: Accepted

Solution code:
def I():return input()
def II():return int(input())
def LI():return list(map(int, input().split()))
def L():return input().split()

def count_num(lis):
    v = {}
    for i in lis:
        if i not in v.keys():
            v[i] = 1
        else:
            v[i] += 1
    return v
def dict_to_list(dic):
    ret = []
    for i in dic.keys():
        ret.append([i,dic[i]])
    return ret

def main():
    n = II()
    e = n//2
    o = e + n%2

    v = LI()
    v_o = count_num(v[::2])
    v_e = count_num(v[1::2])
    cnt_o = dict_to_list(v_o)
    cnt_e = dict_to_list(v_e)
    cnt_o.sort(key=lambda x: x[1], reverse=True)
    cnt_e.sort(key=lambda x: x[1], reverse=True)
    
    cnt_o.append([0,0])
    cnt_e.append([0,0])
    if cnt_e[0][0] != cnt_o[0][0]:
        ans = (e - cnt_e[0][1]) + (o - cnt_o[0][1])
    else:
        ans = min((e-cnt_e[0][1]) + (o-cnt_o[1][1]), (e-cnt_e[1][1]) + (o-cnt_o[0][1]))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
v=list(input().split())
v0={}
v1={}
for i in range(0,n,2):
    if v[i] in v0:
        v0[v[i]]+=1
    else:
        v0[v[i]]=1
for i in range(1,n,2):
    if v[i] in v1:
        v1[v[i]]+=1
    else:
        v1[v[i]]=1
v0=sorted(v0.items(), key=lambda x:x[1], reverse=True)
v1=sorted(v1.items(), key=lambda x:x[1], reverse=True)
if v0[0][0]==v1[0][0]:
    if len(v0)==1:
        print(int(n/2))
    else:
        print(n-v0[0][1]-(v0[1][1] if v0[1][1]>v1[1][1] else v1[1][1] ))
else:
    print(n-v0[0][1]-v1[0][1])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
vv = list(map(int, input().split()))
d1 = {}
d2 = {}

for i in range(n):
    v = vv[i]
    if i%2==0:
        if v not in d2:
            d2[v] = 0
        d2[v] += 1
    else:
        if v not in d1:
            d1[v] = 0
        d1[v] += 1

dl1 = sorted(list(d1.keys()), reverse=True, key=lambda k:d1[k])
dl2 = sorted(list(d2.keys()), reverse=True, key=lambda k:d2[k])
i1 = 0
i2 = 0

if len(dl1)==1 and len(dl2)==1:
    if dl1[0]==dl2[0]:
        print(n//2)
    else:
        print(0)
    exit()
elif len(dl1)==1:
    print(sum(sorted(d2.values(), reverse=True)[1:]))
    exit()
elif len(dl2)==1:
    print(sum(sorted(d1.values(), reverse=True)[1:]))
    exit()


if dl1[0]!=dl2[0]:
    res = 0
    res += sum(sorted(d1.values(), reverse=True)[1:])
    res += sum(sorted(d2.values(), reverse=True)[1:])
    print(res)
else:
    res1 = 0
    l = sorted(d1.values(), reverse=True)
    res1 += l[0]
    if len(l)>2:
        res1 += sum(l[2:])
    res1 += sum(sorted(d2.values(), reverse=True)[1:])

    res2 = 0
    l = sorted(d2.values(), reverse=True)
    res2 += l[0]
    if len(l)>2:
        res2 += sum(l[2:])
    res2 += sum(sorted(d1.values(), reverse=True)[1:])
    print(min(res1,res2))

Submission status: Accepted

Solution code:
n = int(input())
lst = list(map(int, input().split()))
evens = [lst[i] for i in range(0, n, 2)]
odds = [lst[i] for i in range(1, n, 2)]

dic1 = {}
for i in evens:
  if i in dic1:
    dic1[i] += 1
  else:
    dic1[i] = 1

dic2 = {}
for i in odds:
  if i in dic2:
    dic2[i] += 1
  else:
    dic2[i] = 1

lst1 = sorted(dic1.items(), key=lambda x:-x[1])
lst2 = sorted(dic2.items(), key=lambda x:-x[1])

if lst1[0][0] == lst2[0][0]:
  if len(lst1) == 1 and len(lst2) == 1:
    ans = n // 2
  elif len(lst1) == 1:
    ans = n - (lst1[0][1] + lst2[1][1])
  elif len(lst2) == 1:
    ans = n - (lst1[1][1] + lst2[0][1])
  else:
    ans = n - max(lst1[0][1] + lst2[1][1], lst2[0][1] + lst1[1][1])
else:
  ans = n - (lst1[0][1] + lst2[0][1])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
lst = list(map(int, input().split()))
lst_odd = Counter(lst[::2])
lst_even = Counter(lst[1::2])
odd = sorted(lst_odd.items(), key=lambda x:x[1], reverse=True)
even = sorted(lst_even.items(), key=lambda x:x[1], reverse=True)

if len(odd) == len(even) == 1:
	if odd[0][0] == even[0][0]:
		ans = int(n/2)
	else:
		ans = 0
else:
	if odd[0][0] != even[0][0]:
		ans = n - odd[0][1] - even[0][1]
	else:
		ans = n - max(odd[0][1] + even[1][1], odd[1][1] + even[0][1])

print(ans)

Submission status: Accepted

Solution code:
def solve(size=int(input()), ary=list(map(int, input().split())), max_range=100001):
    even = [0 for _ in range(max_range)]
    odd = [0 for _ in range(max_range)]

    (em, ei), (om, oi) = (0, 0), (0, 0)

    for i in range(size):
        if i % 2 == 0:
            even[ary[i]] += 1
            if even[ary[i]] > em:
                em, ei = even[ary[i]], ary[i]
        else:
            odd[ary[i]] += 1
            if odd[ary[i]] > om:
                om, oi = odd[ary[i]], ary[i]

    if ei == oi:
        even[ei], odd[oi] = 0, 0
        return size - max(max(even) + om, max(odd) + em)
    else:
        return size - (em + om)

print(solve())

Submission status: Accepted

Solution code:
from collections import Counter
size = int(input())
ary = list(map(int, input().split()))
even, odd = Counter(ary[::2]), Counter(ary[1::2])
even[0], odd[0] = 0, 0
(e, em), (_, es) = even.most_common(2)
(o, om), (_, os) = odd.most_common(2)
print(size - max(em + os, es + om)) if e == o else print(size - em - om)

Submission status: Accepted

Solution code:
from collections import Counter

def solve(size=int(input()), ary=list(map(int, input().split()))):
    even, odd = Counter(ary[::2]), Counter(ary[1::2])
    even[0], odd[0] = 0, 0
    (e, em), (_, es) = even.most_common(2)
    (o, om), (_, os) = odd.most_common(2)
    return size - max(em + os, es + om) if e == o else size - em - om

print(solve())

Submission status: Accepted

Solution code:
import collections as c
n = int(input())
P = list(map(int,input().split()))
ans = 0
x = []
y = []
for i in range(n):
    if i % 2 == 0:
        x.append(P[i])
    else:
        y.append(P[i])
p = c.Counter(x)
q = c.Counter(y)

values1, counts1 = zip(*p.most_common())
values2, counts2 = zip(*q.most_common())
if values1[0] == values2[0]:
    if len(values1) == 1 and len(values2) == 1:
        f = n // 2
        g = n // 2
    else:
        f = counts1[0] + counts2[1]
        g = counts1[1] + counts2[0]
    ans = n - max(f, g)
else:
    ans = n - (counts1[0] + counts2[0])

print(ans)

Submission status: Accepted

Solution code:
import collections


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

v1 = []
v2=[]

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

v1_1 = collections.Counter(v1).most_common()[0][1]
v2_1 = collections.Counter(v2).most_common()[0][1]

if collections.Counter(v1).most_common()[0][0]==collections.Counter(v2).most_common()[0][0]:
    if v1_1!=n//2 and v2_1!=n//2:
        v1_2 = collections.Counter(v1).most_common()[1][1]
        v2_2 = collections.Counter(v2).most_common()[1][1]
        print(min(n-v2_1-v1_2,n-v1_1-v2_2))
    elif v1_1==n//2 and v2_1==n//2:
        print(n//2)
    elif v1_1!=n//2 and v2_1==n//2:
        v1_2 = collections.Counter(v1).most_common()[1][1]
        print(n-v2_1-v1_2)
    elif v1_1==n//2 and v2_1!=n//2:
        v2_2 = collections.Counter(v2).most_common()[1][1]
        print(n-v2_2-v1_1)
else:
    print(n-v1_1-v2_1)

Submission status: Accepted

Solution code:
import collections
def inpl(): return [int(i) for i in input().split()]


n = int(input())
v = inpl()
ans = len(v)
v1 = [v[i] for i in range(ans) if i%2 == 0]
v2 = [v[i] for i in range(ans) if i%2 == 1]
cv1 = collections.Counter(v1)
cv2 = collections.Counter(v2)
c11 = cv1.most_common()[0]
if len(cv1) != 1:
    c12 = cv1.most_common()[1]
else: c12 = [0,0]
c21 = cv2.most_common()[0] 
if len(cv2) != 1:
    c22 = cv2.most_common()[1]
else: c22 = [0,0]

if c11[0] == c21[0]:
    ans -= max(c12[1]+c21[1],c11[1]+c22[1])
else:
    ans -= c11[1]+c21[1]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
v = [[i for e,i in enumerate(v) if e%2 == 0],[i for e,i in enumerate(v) if e%2 == 1]]
dic = [{},{}]
for i in range(2):
    for j in range(n//2):
        k = v[i][j]
        if k in dic[i].keys():
            dic[i][k] += 1
        else:
            dic[i][k] = 1
m1 = max(list(dic[0].values()))
m2 = max(list(dic[1].values()))
i1 = list(dic[0].keys())[list(dic[0].values()).index(m1)]
i2 = list(dic[1].keys())[list(dic[1].values()).index(m2)]
if i1 == i2:
    dic[0][i1] = 0
    dic[1][i2] = 0
    m3 = max(list(dic[0].values()))
    m4 = max(list(dic[1].values()))
    if m2+m3 >= m1+m4:
        print(n-m2-m3)
    elif m2+m3 < m1+m4:
        print(n-m1-m4)
else:
    print(n-m1-m2)

Submission status: Accepted

Solution code:
from collections import defaultdict

def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b

n = getN()
nums = getlist()
lis1 = defaultdict(int)
lis2 = defaultdict(int)

for i,s in enumerate(nums):
    if i % 2 == 0:
        lis1[s] += 1
    else:
        lis2[s] += 1

maxk1 = []
maxv1 = 0
for k in lis1.keys():
    if lis1[k] == maxv1:
        maxk1.append(k)
    elif lis1[k] > maxv1:
        maxv1 = lis1[k]
        maxk1 = [k]

maxk2 = []
maxv2 = 0
for k in lis2.keys():
    if lis2[k] == maxv2:
        maxk2.append(k)
    elif lis2[k] > maxv2:
        maxv2 = lis2[k]
        maxk2 = [k]

v1 = list(lis1.values())
v1.sort(reverse=True)
v1 = v1 + [0]
v2 = list(lis2.values())
v2.sort(reverse=True)
v2 = v2 + [0]

if (len(maxk1) == 1) and (len(maxk2) == 1) and (maxk1[0] == maxk2[0]):
    rem = max(v1[0] + v2[1], v2[0] + v1[1])
    ans = n-rem

else:
    ans = n - maxv1 - maxv2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
numbers = [int(x) for x in input().rstrip("\n").split(" ")]

from collections import defaultdict
odd = defaultdict(int)
even = defaultdict(int)
for i in range(0,n,2):
  odd[numbers[i]] += 1
  even[numbers[i+1]] += 1

odd_items = list(reversed(sorted([(count, odd_number) for (odd_number, count) in odd.items()])))

even_items = list(reversed(sorted([(count, even_number) for (even_number, count) in even.items()])))

if odd_items[0][1] != even_items[0][1]:
  print(n - odd_items[0][0] - even_items[0][0])
else:
  if len(odd_items) == 1 and len(even_items) == 1:
    print(n // 2)
  else:
    if len(odd_items) == 1 and len(even_items) == 2:
      print(n - odd_items[0][0] - even_items[1][0])
    elif len(odd_items) == 2 and len(even_items) == 1:
      print(n - odd_items[1][0] - even_items[0][0])
    else:
      second_best1 = n - odd_items[0][0] - even_items[1][0]
      second_best2 = n - odd_items[1][0] - even_items[0][0]
      print(min(second_best1,second_best2))