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:
n = int(input())

v = [i for i in input().split()]

v1 = {}

v2 = {}

for i in range(n//2):
    if  v[2 *i +1] in v1.keys():
        v1[v[2 * i +1]] += 1
    else:
        v1[v[2 * i + 1]] = 1

    if v[2 * i] in v2.keys():
        v2[v[2 * i]] += 1
    else:
        v2[v[2 * i]] = 1


v1 = sorted(v1.items(),key=lambda x:  x[1])
v2 = sorted(v2.items(),key=lambda x:  x[1])
v1.reverse()
v2.reverse()
v1.append((None,0))
v2.append((None,0))
#print(v1)
if v1[0][0] == v2[0][0]:
    ans = max(v1[0][1]+v2[1][1] , v1[1][1] + v2[0][1])
else:
    ans = v1[0][1] + v2[0][1]

print(n - ans)

Submission status: Accepted

Solution code:
#! /usr/bin/python3
from collections import Counter
import sys

def debug(*f):
    print(*f, file=sys.stderr)

def f(odd, even, N):
    debug(N, odd, even)


def main(N, V):
    debug("N=",N)
    debug("V=",V)
    N = int(N)
    V = [int(x) for x in V.split(" ")]

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

    if odd[0][0] != even[0][0]: # 異なる文字
        return N - odd[0][1] - even[0][1]
    else:
        if odd[0][1] > even[0][1]:
            return N - odd[0][1] - even[1][1]
        elif odd[0][1] < even[0][1]:
            return N - odd[1][1] - even[0][1]
        elif odd[0][1] == N // 2:
            return N // 2
        else:
            return N - odd[0][1]  - max(odd[1][1], even[1][1])



if 0==1:
    import sys
    assert main("4", "3 1 3 2") == 1
    assert main("4", "1 1 1 1") == 2
    assert main("6", "105 119 105 119 105 119") == 0
    assert main("14", "1 1 1 1 1 1 2 2 2 2 3 4 3 5") == 9

    print("OK", file=sys.stderr)


N = input()
V = input()
print(main(N, V))

Submission status: Accepted

Solution code:
from collections import Counter

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

v1 = v[::2]
v2 = v[1::2]

counter1 = dict(Counter(v1))
counter2 = dict(Counter(v2))
x1 = max(counter1, key=counter1.get)
x2 = max(counter2, key=counter2.get)
x1_c = counter1[x1]
x2_c = counter2[x2]
del counter1[x1]
del counter2[x2]
y1_c = 0
y2_c = 0
if len(counter1) != 0:
    y1_c = max(counter1.values())
if len(counter2) != 0:
    y2_c = max(counter2.values())

n = n//2
ans = 0
if x1 == x2:
    ans = min((n - x1_c) + (n - y2_c), (n - y1_c) + (n - x2_c))
else:
    ans = (n - x1_c) + (n - x2_c)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
array = [int(x) for x in input().split()]
tmp_dict = {}
tmp_dict2 = {}
tmp_array = array[::2]
tmp_array2 = array[1::2]
for i in tmp_array:
    if i in tmp_dict.keys():
        tmp_dict[i] += 1
    else:
        tmp_dict[i] = 1
for i in tmp_array2:
    if i in tmp_dict2.keys():
        tmp_dict2[i] += 1
    else:
        tmp_dict2[i] = 1

sorted_dict = sorted(tmp_dict.items(), key=lambda x: x[1], reverse=True)
sorted_dict2 = sorted(tmp_dict2.items(), key=lambda x: x[1], reverse=True)
tmp = sorted_dict[0][0]
tmp2 = sorted_dict2[0][0]

aaa = array[0]
if array.count(aaa) == len(array):
    print(len(array)//2)
    exit()

if tmp == tmp2:
    if len(sorted_dict) == 1:
        tmp2 = sorted_dict2[1][0]
    elif len(sorted_dict2) == 1:
        tmp = sorted_dict[1][0]
    else:
        if sorted_dict2[1][1] > sorted_dict[1][1]:
            tmp2 = sorted_dict2[1][0]
        elif sorted_dict2[1][1] <= sorted_dict[1][1]:
            tmp = sorted_dict[1][0]
res = 0
for i in tmp_array:
    if i != tmp:
       res += 1

for i in tmp_array2:
    if i != tmp2:
       res += 1
print(res)

Submission status: Accepted

Solution code:
from collections import Counter

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

v1 = v[::2]
v2 = v[1::2]

counter1 = dict(Counter(v1))
counter2 = dict(Counter(v2))
x1 = max(counter1, key=counter1.get)
x2 = max(counter2, key=counter2.get)
x1_c = counter1[x1]
x2_c = counter2[x2]
del counter1[x1]
del counter2[x2]
y1_c = 0
y2_c = 0
if len(counter1) != 0:
    y1_c = max(counter1.values())
if len(counter2) != 0:
    y2_c = max(counter2.values())

n = n//2
ans = 0
if x1 == x2:
    ans = min((n - x1_c) + (n - y2_c), (n - y1_c) + (n - x2_c))
    """
    if x1_c > x2_c:
        ans = (n - x1_c) + (n - y2_c)
    else:
        ans =  (n - y1_c) + (n - x2_c)
    """
else:
    ans = (n - x1_c) + (n - x2_c)

print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())
vs = [int(i) for i in input().split()]
evens = []
odds = []
for i,num in enumerate(vs):
    if i % 2 == 0:
        evens.append(num)
    elif i % 2 == 1:
        odds.append(num)

def plana():
    if (len(set(vs)) == 2) and (len(set(evens)) == 1) and (len(set(odds)) == 1):
        return 0
    elif (len(set(vs)) == 1):
        return len(evens)
    ec = collections.Counter(evens)
    oc = collections.Counter(odds)

    # check
    if ec.most_common()[0][0] == oc.most_common()[0][0]:
        if len(set(evens))==1:
            return len(odds)-oc.most_common()[1][1]
        elif len(set(odds))==1:
            return len(evens)-ec.most_common()[1][1]
        e1 = ec.most_common()[0][1]
        e2 = ec.most_common()[1][1]
        o1 = oc.most_common()[0][1]
        o2 = oc.most_common()[1][1]
        count1 = 0
        count2 = 0
        count1 += (len(evens) - e1)
        count1 += (len(odds) - o2)
        count2 += (len(odds) - o1)
        count2 += (len(evens) - e2)
        if (count1 >= count2):
            return count2
        else:
            return count1

        # if ec.most_common()[0][1] >= oc.most_common()[0][1]:
        #     count = 0
        #     count += (len(evens) - ec.most_common()[0][1])
        #     count += (len(odds) - oc.most_common()[1][1])
        #     return count
        # elif ec.most_common()[0][1] < oc.most_common()[0][1]:
        #     count = 0
        #     count += (len(evens) - ec.most_common()[1][1])
        #     count += (len(odds) - oc.most_common()[0][1])
        #     return count

    count = 0
    count += (len(evens) - ec.most_common()[0][1])
    count += (len(odds) - oc.most_common()[0][1])
    return count

print(int(plana()))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    v = list(map(int, input().split()))
    v_ki = v[1:N:2].copy()
    val_ki = [i+1 for i in range(10**5)]
    count_ki = [0] * (10**5)
    v_gu = v[0:N:2].copy()
    val_gu = [i+1 for i in range(10**5)]
    count_gu = [0] * (10**5)
    for i in range(N//2):
        count_ki[v_ki[i]-1] += 1
        count_gu[v_gu[i]-1] += 1
    
    ki = list(zip(count_ki, val_ki))
    ki.sort()
    gu = list(zip(count_gu, val_gu))
    gu.sort()
    if ki[-1][1] != gu[-1][1]:
        return N-ki[-1][0]-gu[-1][0]
    else:
        return min(N-ki[-2][0]-gu[-1][0], N-ki[-1][0]-gu[-2][0])

if __name__ == "__main__":
    print(main())

Submission status: Accepted

Solution code:
import collections

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

odd = [v[2 * i] for i in range(N // 2)]
even = [v[2 * i + 1] for i in range(N // 2)]

# print(odd, even)

odd_dict = sorted(collections.Counter(odd).items(), key=lambda x: x[1])[::-1]
even_dict = sorted(collections.Counter(even).items(), key=lambda x: x[1])[::-1]

# print(odd_dict)
# print(even_dict)

if odd_dict[0][0] != even_dict[0][0]:
    odd_sum = N // 2 - odd_dict[0][1]
    even_sum = N//2 - even_dict[0][1]
    # print(odd_sum)
    # print(even_sum)
    print(odd_sum + even_sum)

else:
    odd_len = len(odd_dict)
    even_len = len(even_dict)

    if (odd_len == 1) and (even_len == 1):
        print(N//2)
    elif (odd_len == 1) and (even_len != 1):
        even_val, even_sec_val = even_dict[0][1], even_dict[1][1]
        print(N//2 - even_sec_val)

    elif (odd_len != 1) and (even_len == 1):
        odd_val, odd_sec_val = odd_dict[0][1], odd_dict[1][1]
        print(N // 2 - odd_sec_val)

    else:
        odd_val, odd_sec_val = odd_dict[0][1], odd_dict[1][1]
        even_val, even_sec_val = even_dict[0][1], even_dict[1][1]
        print(min(N - odd_val - even_sec_val, N - odd_sec_val - even_val))

Submission status: Accepted

Solution code:
import collections
n=int(input())
N=list(map(int,input().split(" ")))
list_odd=N[::2]
list_even=N[1::2]
c_odd = collections.Counter(list_odd)
c_even = collections.Counter(list_even)
most_odd=c_odd.most_common()[0][1]
most_even=c_even.most_common()[0][1]
if (n<=2):
    print("0")
elif (len(list_even)==most_even and len(list_odd)==most_odd and c_odd.most_common()[0][0]==c_even.most_common()[0][0]):
    print(most_odd)
else:
    if(c_odd.most_common()[0][0]==c_even.most_common()[0][0]):
        M=max(c_odd.most_common()[1][1],c_even.most_common()[1][1])
        print(min((len(list_even)+len(list_odd)-M-most_odd),(len(list_even)+len(list_odd)-M-most_even)))
    else:
        print(len(list_even)+len(list_odd)-most_even-most_odd)
#c_odd.most_common()[0][0]==c_even.most_common()[0][0]

Submission status: Accepted

Solution code:
n = int(input())
nums = list(map(int,input().split()))
odd,even={},{}
for i in range(n):
  if i%2 == 0:
    if nums[i] in even:
      even[nums[i]] += 1
    else:
      even[nums[i]] = 1
  else:
    if nums[i] in odd:
      odd[nums[i]] += 1
    else:
      odd[nums[i]] = 1

if len(odd) == 1 and len(even) == 1:
  if odd == even:
    print(n//2)
  else:
    print(0)
elif len(even) == 1 and 1 < len(odd):
  for a,b in even.items():
    for k,v in sorted(odd.items(), key=lambda x: -x[1]):
      if a == k:
        continue
      else:
        print(n//2 - v)
        exit()
elif len(odd) == 1 and 1 < len(even):
  for a,b in odd.items():
    for k,v in sorted(even.items(), key=lambda x: -x[1]):
      if a == k:
        continue
      else:
        print(n//2 - v)
        exit()
else:
  a = sorted(odd.items(), key=lambda x: -x[1])
  b = sorted(even.items(), key=lambda x: -x[1])
  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:
from collections import Counter
n=int(input())
a=[int(i) for i in input().split()]

o=[]
e=[]


for i in range(n):
    if i%2==1:
        o.append(a[i])
    else:
        e.append(a[i])

oc = Counter(o)
ec = Counter(e)

ok=[]
ek=[]
ol=[]
el=[]
for word, cnt in oc.most_common():
    ok.append(cnt)
    ol.append(word)
for word, cnt in ec.most_common():
    ek.append(cnt)
    el.append(word)

if len(ok)==1:
    ok.append(0)
if len(ek)==1:
    ek.append(0)
if el[0]!=ol[0]:
    print(n-ok[0]-ek[0])
else:
    print(min(n-ok[1]-ek[0],n-ok[0]-ek[1]))

Submission status: Accepted

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

Vdic = [{}, {}]
# 0: even 1: odd
for i in range(len(V_list)):
    v = V_list[i]

    tmp = (i + 1) % 2
    Vdic[tmp][v] = Vdic[tmp].get(v, 0) + 1

even = sorted(Vdic[0].values())
odd = sorted(Vdic[1].values())

ans_even = [(-1, 0)]
ans_odd = [(-2, 0)]
if(len(even) == 1):
    t = max(Vdic[0], key=Vdic[0].get)
    ans_even.append((t, even[0]))
else:
    n1 = even[-1]
    n2 = even[-2]

    f1 = True
    f2 = True
    if(n1 == n2):
        for k, v in Vdic[0].items():
            if(v == n1):
                if(f1 is False and f2 is False):
                    continue
                if(f1 is False):
                    f2 = False
                f1 = False
                ans_even.append((k, v))
    else:
        for k, v in Vdic[0].items():
            if(v == n1 and f1 is True):
                f1 = False
                ans_even.append((k, v))
            if(v == n2 and f2 is True):
                f2 = False
                ans_even.append((k, v))

if(len(odd) == 1):
    t = max(Vdic[1], key=Vdic[1].get)
    ans_odd.append((t, odd[0]))
else:
    n1 = odd[-1]
    n2 = odd[-2]

    f1 = True
    f2 = True
    if(n1 == n2):
        for k, v in Vdic[1].items():
            if(v == n1):
                if(f1 is False and f2 is False):
                    continue
                if(f1 is False):
                    f2 = False
                f1 = False
                ans_odd.append((k, v))
    else:
        for k, v in Vdic[1].items():
            if(v == n1 and f1 is True):
                f1 = False
                ans_odd.append((k, v))
            if(v == n2 and f2 is True):
                f2 = False
                ans_odd.append((k, v))


# print(ans_even, ans_odd)

ans = []
for ke, ve in ans_even:
    for ko, vo in ans_odd:
        if(ke != ko):
            ans.append(n - ve - vo)

print(min(ans))

Submission status: Accepted

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

odd_cnt = {}
even_cnt = {}

for i,vi in enumerate(vn):
    if i % 2 == 0:
        if vi in even_cnt:
            even_cnt[vi] += 1
        else:
            even_cnt[vi] = 1
    else:
        if vi in odd_cnt:
            odd_cnt[vi] += 1
        else:
            odd_cnt[vi] = 1

# print(sorted(even_cnt.items(), key=lambda x: -x[1])[0:2])
# print(sorted(odd_cnt.items(), key=lambda x: -x[1])[0:2])


even_max = 0
even_max_num = 0
even_sum = 0
odd_max = 0
odd_max_num = 0
odd_sum = 0
ans = 0
for k,v in sorted(even_cnt.items(), key=lambda x: -x[1]):
    even_sum += v
    if v > even_max:
        even_max = v
        even_max_num = k

for k,v in sorted(odd_cnt.items(), key=lambda x: -x[1]):
    odd_sum += v
    if v > odd_max:
        odd_max = v
        odd_max_num = k

if len(even_cnt) > 1:
    even_2nd = sorted(even_cnt.items(), key=lambda x: -x[1])[1][1]
else:
    even_2nd = 0
if len(odd_cnt) > 1:
    odd_2nd = sorted(odd_cnt.items(), key=lambda x: -x[1])[1][1]
else:
    odd_2nd = 0

if odd_max_num == even_max_num:
    if odd_max == even_max:
        if len(odd_cnt) == 1 and len(even_cnt) == 1:
            ans = even_sum
        elif len(odd_cnt) == 1:
            ans = even_sum - even_2nd
        elif len(even_cnt) == 1:
            ans = odd_sum - odd_2nd
        else:
            if even_2nd > odd_2nd:
                ans = odd_sum - odd_max + even_sum - even_2nd
            else:
                ans = odd_sum - odd_2nd + even_sum - even_max
    elif odd_max > even_max:
        ans = odd_sum - odd_max + even_sum - even_2nd
    else:
        ans = odd_sum - odd_2nd + even_sum - even_max
else:
    ans = odd_sum - odd_max + even_sum - even_max
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

former = []
latter = []
counter = 0
for node in v:
    if(counter % 2 == 0):
        former.append(node)
    else:
        latter.append(node)
    counter += 1

forlen = len(former)
latlen = len(latter)

forfreq = [0] * forlen
latfreq = [0] * latlen

former_sorted = sorted(former)
latter_sorted = sorted(latter)

# 最頻値を求める
counter = 0
for_val_num = []
next_idx = 0
for i in range(forlen):
    if(former_sorted[i] == former_sorted[next_idx]):
        counter += 1
    else:
        for_val_num.append([former_sorted[next_idx], counter])
        next_idx = i
        counter = 1
    if (i == forlen - 1):
        for_val_num.append([former_sorted[next_idx], counter])

forfreqidx1 = 0

for i in range(len(for_val_num)):
    if(for_val_num[i][1] > for_val_num[forfreqidx1][1]):
        forfreqidx1 = i

# 第1位最頻値の出現回数
forfreqnum1 = for_val_num[forfreqidx1][1]

# 最頻値を求める
counter = 0
lat_val_num = []
next_idx = 0
for i in range(latlen):
    if(latter_sorted[i] == latter_sorted[next_idx]):
        counter += 1
    else:
        lat_val_num.append([latter_sorted[next_idx], counter])
        next_idx = i
        counter = 1
    if (i == latlen - 1):
        lat_val_num.append([latter_sorted[next_idx], counter])

latfreqidx1 = 0

for i in range(len(lat_val_num)):
    if(lat_val_num[i][1] > lat_val_num[latfreqidx1][1]):
        latfreqidx1 = i

# 第1位最頻値の出現回数
latfreqnum1 = lat_val_num[latfreqidx1][1]

# 同じ数だった時
if(for_val_num[forfreqidx1][0] == lat_val_num[latfreqidx1][0]):
    # 第1位最頻値の出現回数を0にする
    for_val_num[forfreqidx1][1] = 0
    lat_val_num[latfreqidx1][1] = 0

    forfreqidx2 = 0

    for i in range(len(for_val_num)):
        if (for_val_num[i][1] > for_val_num[forfreqidx2][1]):
            forfreqidx2 = i

    # 第2位最頻値の出現回数
    forfreqnum2 = for_val_num[forfreqidx2][1]

    latfreqidx2 = 0

    for i in range(len(lat_val_num)):
        if (lat_val_num[i][1] > lat_val_num[latfreqidx2][1]):
            latfreqidx2 = i

    # 第1位最頻値の出現回数
    latfreqnum2 = lat_val_num[latfreqidx2][1]

    if(forfreqnum1 - forfreqnum2 >= latfreqnum1 - latfreqnum2):
        print((forlen - forfreqnum1) + (latlen - latfreqnum2))
    else:
        print((forlen - forfreqnum2) + (latlen - latfreqnum1))
else:
    print((forlen - forfreqnum1) + (latlen - latfreqnum1))

Submission status: Accepted

Solution code:
import collections

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

def count_elem(x):
    coll = collections.Counter(x)
    return coll.most_common()

b = count_elem(a[::2])
c = count_elem(a[1::2])

if b[0][0] != c[0][0]:
    print(len(a) - b[0][1] - c[0][1])
else:
    if b[0][1] > c[0][1]:
        if len(c) > 1:
            print(len(a) - b[0][1] - c[1][1])
        else:
            print(len(a) - b[0][1])
    elif b[0][1] < c[0][1]:
        if len(b) > 1:
            print(len(a) - b[1][1] - c[0][1])
        else:
            print(len(a) - c[0][1])
    elif b[0][1] == c[0][1]:
        if len(a)/2 == b[0][1]:
            print(int(len(a)/2))
        else:
            if b[1][1] >= c[1][1]:
                print(len(a) - b[1][1] - c[0][1])
            else:
                print(len(a) - b[0][1] - c[1][1])

Submission status: Accepted

Solution code:
from collections import Counter

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

    even = vs[::2]
    odd = vs[1::2]

    def solve(even, odd):
        ce = Counter(even)
        co = Counter(odd)
        
        emode = ce.most_common()
        omode = co.most_common()
        
        result = 0
        if emode[0][0] != omode[0][0]:
            for i in range(1, len(emode)):
                result += emode[i][1]
            for i in range(1, len(omode)):
                result += omode[i][1]
            
            return result
        
        else:
            temp1 = 0
            temp2 = 0
            for i in range(1, len(emode)):
                temp1 += emode[i][1]
            
            if len(omode) == -1:
                temp1 += len(odd)
            else:
                for i in range(len(omode)):
                    if i == 1:
                        continue
                    else:
                        temp1 += omode[i][1]
                        
                        
            for i in range(1, len(omode)):
                temp2 += omode[i][1]
            
            if len(emode) == -1:
                temp2 += len(even)
            else:
                for i in range(len(emode)):
                    if i == 1:
                        continue
                    else:
                        temp2 += emode[i][1]
            result = min(temp1, temp2)
            
            return result
        
    print(solve(even, odd))

Submission status: Accepted

Solution code:
import collections

n = int(input())
v = list(map(int, input().split()))
a = v[::2]
b = v[1::2]
ac = collections.Counter(a)
bc = collections.Counter(b)
ac = sorted(ac.items(), key=lambda x: x[1], reverse=True)
bc = sorted(bc.items(), key=lambda x: x[1], reverse=True)
if ac[0][0] == bc[0][0]:
    if len(ac) == len(bc) and len(ac) == 1:
        print(ac[0][1])
        exit()
    i = 0
    while ac[i][1] == bc[i][1]:
        i += 1
        if i == len(ac) - 1 or i == len(bc) - 1:
            break
    if ac[i][1] > bc[i][1]:
        ac.remove(ac[i])
        bc.remove(bc[0])
    elif ac[i][1] < bc[i][1]:
        ac.remove(ac[0])
        bc.remove(bc[i])
    else:
        ac.remove(ac[0])
        bc.remove(bc[1])
else:
    ac.remove(ac[0])
    bc.remove(bc[0])
ans = 0
for i in range(len(ac)):
    ans += ac[i][1]
for i in range(len(bc)):
    ans += bc[i][1]
print(ans)

Submission status: Accepted

Solution code:
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
from collections import defaultdict

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

d0 = defaultdict(int)
d1 = defaultdict(int)
for i in range(len(v)):
    if i % 2 == 1:
        d1[v[i]] += 1
    else:
        d0[v[i]] += 1

max1 = 0
max12 = 0
for i in d1.keys():
    if max1 < d1[i]:
        max12 = max1
        max1 = d1[i]
        max1n = i
    elif max12 < d1[i]:
        max12 = d1[i]
max0 = 0
max02 = 0
for i in d0.keys():
    if max0 < d0[i]:
        max02 = max0
        max0 = d0[i]
        max0n = i
    elif max02 < d0[i]:
        max02 = d0[i]
if max1n == max0n:
    if max0 < max1:
        print(int(n / 2 - max02 + n / 2 - max1))
    elif max1 < max0:
        print(int(n / 2 - max0 + n / 2 - max12))
    else:
        if max12 < max02:
            print(int(n / 2 - max02 + n / 2 - max1))
        else:
            print(int(n / 2 - max0 + n / 2 - max12))
else:
    print(int(n / 2 - max0 + n / 2 - max1))

Submission status: Accepted

Solution code:
import collections
import sys

n = input()
n = int(n)
v = [0]*int(n)
v[:] = map(int, input().split())
#print(n)
#print(v)
v_o = [0]*int((n/2))
v_e = [0]*int((n/2))
for i in range(n):
    if i%2 == 0:
        v_o[i//2] = v[i]
    else:
        v_e[i//2] = v[i]

cnt_o = collections.Counter(v_o).most_common(2)
#print(cnt_o[0][1])
collections.Counter(v_e).most_common(1)
cnt_e = collections.Counter(v_e).most_common(2)
#print(cnt_e[0][1])

if cnt_o[0][0] == cnt_e[0][0]:
    if (cnt_o[0][1] == n//2) and (cnt_e[0][1] == n//2):
        print(n//2)
        sys.exit()
    if cnt_o[0][1] >= cnt_e[0][1]:
        n_min = n -cnt_o[0][1] - cnt_e[1][1]
        if (n_min > n -cnt_o[1][1] - cnt_e[0][1]):
            n_min = n -cnt_o[1][1] - cnt_e[0][1]
        print(n_min)
        sys.exit()
    elif cnt_o[0][1] < cnt_e[0][1]:
        n_min = n -cnt_o[1][1] - cnt_e[0][1]
        if (n_min > n -cnt_o[0][1] - cnt_e[1][1]):
            n_min = n -cnt_o[0][1] - cnt_e[1][1]
        print(n_min)
        sys.exit()
else:
    n_min = n -cnt_o[0][1] - cnt_e[0][1]
    print(n_min)
    sys.exit()

Submission status: Accepted

Solution code:
n = int(input())
v = list(map(int, input().split()))
even = {}
odd = {}
for i in range(n):
    if i % 2 == 0:
        if v[i] in even:
            even[v[i]] += 1
        else:
            even[v[i]] = 1
    else:
        if v[i] in odd:
            odd[v[i]] += 1
        else:
            odd[v[i]] = 1

def check(dic1, dic2):
    if len(dic1) == 1 and len(dic2) == 1 and list(dic1.keys()) != list(dic2.keys()):
        return True
    else:
        return False

def maxkey(dic):
    maxkey = 0
    maxvalue = 0
    for key in dic:
        value = dic[key]
        if maxvalue < value:
            maxvalue = value
            maxkey = key
    return maxkey

if check(even, odd):
    print(0)
elif len(list(set(v))) == 1:
    print(int(n/2))
else:
    evenlist = sorted(list(even.values()))
    oddlist = sorted(list(odd.values()))
    evenlist.reverse()
    oddlist.reverse()
    if maxkey(even) != maxkey(odd):
        ans = n/2 - evenlist[0] + n/2 - oddlist[0]
        print(int(ans))
    else:
        if len(evenlist) == 1:
            ans = n/2-evenlist[0] + n/2-oddlist[1]
        elif len(oddlist) == 1:
            ans = n/2-evenlist[1] + n/2-oddlist[0]
        else:
            ans = min(n/2-evenlist[0] + n/2-oddlist[1], n/2-evenlist[1] + n/2-oddlist[0])
        print(int(ans))

Submission status: Accepted

Solution code:
ai = lambda: list(map(int,input().split()))
ai_ = lambda: [int(x)-1 for x in input().split()]

n = int(input())
a = ai()
if all(i == a[0] for i in a):
    print(n//2)
    exit()

a1 = a[::2]
a2 = a[1::2]

from collections import Counter
c1 = Counter(a1)
c2 = Counter(a2)
m1 = c1.most_common(2)
m2 = c2.most_common(2)
if m1[0][0] == m2[0][0]:
    if len(c1) == 1:
        print(n-m1[0][1]-m2[1][1])
    elif len(c2) == 1:
        print(n - m1[0][1] - m2[1][1])
    else:
        print(min(n-m1[0][1]-m2[1][1], n-m1[1][1]-m2[0][1]))
else:
    print(n-m1[0][1] - m2[0][1])

Submission status: Accepted

Solution code:
import bisect

n=int(input())
v = list(map(int,input().split()))
x = []
y = []

for i in range(n):
    if i %2 ==0:
        x.append(v[i])
    else:
        y.append(v[i])
x.sort()
y.sort()
xk = list(set(x))
yk= list(set(y))
xk.sort()
yk.sort()

a = []
b = []
k = 0
l = 0
while k < len(x) :
    aa = bisect.bisect_right(x,x[k])
    a.append(aa-k)
    k = aa
while l < len(y) :
    bb = bisect.bisect_right(y,y[l])
    b.append(bb-l)
    l = bb

c = sorted(a)
d = sorted(b)

aaa = [i for i, xx in enumerate(a) if xx == c[-1]]
bbb = [i for i, xx in enumerate(b) if xx == d[-1]]

#print(x,xk,y,yk)
#print(a,b)
#print(c,d)
#print(aaa,bbb)

if len(aaa)>= 2 or len(bbb)>= 2:
    print(n-c[-1]-d[-1])
else:
    jkl = a.index(c[-1])
    jkll= b.index(d[-1])
    if xk[jkl] == yk[jkll]:
        ans = 100000000000000
        if len(d) >= 2:
            ans = n-c[-1]-d[-2]
        if len(c) >= 2:
            ans = min(ans,n-c[-2]-d[-1])
        if ans == 100000000000000:
            ans = n//2
        print(ans)
    else:
        print(n-c[-1]-d[-1])

Submission status: Accepted

Solution code:
import collections
#import numpy as np
#print(np.diff(suuretsu))

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

odd = [suuretsu[i] for i in range(n) if i % 2 != 0]
even = [suuretsu[i] for i in range(n) if i % 2 == 0]
ans = 0

#print(odd)
#print(even)

count_dict_odd = collections.Counter(odd)
count_dict_even = collections.Counter(even)

if count_dict_odd.most_common(n)[0][0] == count_dict_even.most_common(n)[0][0]:
  
  if count_dict_odd.most_common(n)[0][1] == count_dict_even.most_common(n)[0][1]:
    if len(count_dict_odd.most_common(n))== 1 and len(count_dict_even.most_common(n)) == 1:
      ans = int(n/2)
    elif len(count_dict_odd.most_common(n)) > len(count_dict_even.most_common(n)):
      ans = n-count_dict_odd.most_common(n)[0][1]-count_dict_even.most_common(n)[1][1]
    elif len(count_dict_odd.most_common(n)) < len(count_dict_even.most_common(n)):
      ans = n-count_dict_odd.most_common(n)[1][1]-count_dict_even.most_common(n)[0][1]
    else:
      if count_dict_odd.most_common(n)[1][1] >= count_dict_even.most_common(n)[1][1]:
        ans = n-count_dict_odd.most_common(n)[1][1]-count_dict_even.most_common(n)[0][1]
      else:
        ans = n-count_dict_odd.most_common(n)[0][1]-count_dict_even.most_common(n)[1][1]	      
  elif count_dict_odd.most_common(n)[0][1] > count_dict_even.most_common(n)[0][1]:
    ans = n-count_dict_odd.most_common(n)[0][1]-count_dict_even.most_common(n)[1][1]
  else:
    ans = n-count_dict_odd.most_common(2)[0][1]-count_dict_even.most_common(1)[0][1]
else:
    ans = n-count_dict_odd.most_common(n)[0][1]-count_dict_even.most_common(n)[0][1]

    
print(ans)
#print(len(count_dict_even.most_common(2)))
#print(len(count_dict_even.most_common(n)))
#print(count_dict_even.most_common(n))

Submission status: Accepted

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

kisu = [str(v) for i,v in enumerate(V) if i % 2 == 1]
gusu = [str(v) for i,v in enumerate(V) if i % 2 == 0]

from collections import Counter

kisu_c = Counter(kisu).most_common()
gusu_c = Counter(gusu).most_common()

ans = 0


if len(kisu_c) == 1 and len(gusu_c) == 1:
    if kisu_c[0][0] == gusu_c[0][0]:
        print(kisu_c[0][1])
    else:
        print(0)
    exit()
elif len(kisu_c) == 1:
    gusu_c.pop(0)

    for i in gusu_c:
        ans += i[1]
    print(ans)
    exit()
elif len(gusu_c) == 1:
    kisu_c.pop(0)

    for i in kisu_c:
        ans += i[1]
    print(ans)
    exit()



if kisu_c[0][0] == gusu_c[0][0]:
    if kisu_c[0][1] < gusu_c[0][1]:
        gusu_c.pop(0)
        kisu_c.pop(1)
    elif kisu_c[0][1] > gusu_c[0][1]:
        gusu_c.pop(1)
        kisu_c.pop(0)
    else:
        if kisu_c[1][1] > gusu_c[1][1]:
            kisu_c.pop(1)
            gusu_c.pop(0)
        else:
            kisu_c.pop(0)
            gusu_c.pop(1)

else:
    kisu_c.pop(0)
    gusu_c.pop(0)


for i in kisu_c:
    ans += i[1]
for i in gusu_c:
    ans += i[1]

print(ans)

Submission status: Accepted

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

A1=[]
A2=[]
for i in range(len(A)):
    if i%2==0:
        A1.append(A[i])
    else:
        A2.append(A[i])
        
B1=collections.Counter(A1)
B2=collections.Counter(A2)

if N==2:
    print(0)

elif B1.most_common(2)[0][1]==B2.most_common(2)[0][1] and B1.most_common(2)[0][1]==int(N/2):
    if B1.most_common(1)==B2.most_common(1):
        print(int(N/2))
    else:
        print(0)
else:
    
    
    if B1.most_common(1)==B2.most_common(1):
        
        if B1.most_common(2)[1][1]>=B2.most_common(2)[1][1]:
            ans=N-B1.most_common(2)[1][1]-B2.most_common(2)[0][1]
        else:
            ans=N-B2.most_common(2)[1][1]-B1.most_common(2)[0][1]
    elif B1.most_common(2)[0][0]==B2.most_common(2)[0][0]:
        if B1.most_common(2)[0][1]>=B2.most_common(2)[0][1]:
            ans=N-B1.most_common(2)[0][1]-B2.most_common(2)[1][1]
        else:
            ans=N-B2.most_common(2)[0][1]-B1.most_common(2)[1][1]
    else:
        ans=N-B1.most_common(2)[0][1]-B2.most_common(2)[0][1]
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter 

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

x=[]
y=[]

for i in range(n):
    if i%2==0:
        x.append(a[i])
    else:
        y.append(a[i])

cx=Counter(x)
cy=Counter(y)

ax=cx.most_common(2)
ay=cy.most_common(2)

if len(ax)==1:
    ax=ax+ax

if len(ay)==1:
    ay=ay+ay
   

if not ax[0][0]==ay[0][0]:
    print(n-ax[0][1]-ay[0][1])
else:
    if ax[0][1]==n//2:
        if ay[0][1]==n//2:
            print(n-ay[0][1])
        else:
            print(n//2-ay[1][1])
    elif ay[0][1]==n//2:
        print(n//2-ax[1][1])
    else:
        if ax[0][1]<ay[0][1]:
            print(n-ax[1][1]-ay[0][1])
        elif ax[0][1]>ay[0][1]:  
            print(n-ax[0][1]-ay[1][1])
        else:
            if ax[1][1]<ay[1][1]: 
                print(n-ax[0][1]-ay[1][1])
            else:
                print(n-ay[0][1]-ax[1][1])