Problem p03244 (python)

Fixed submission:
from collections import Counter
def main(n,v):
    # n = int(input())
    # v = list(map(int, input().split()))
    odd = Counter(v[0::2]).most_common(2) + [(0, 0)]
    even = Counter(v[1::2]).most_common(2) + [(0, 0)]

    n_half = int(n / 2)

    if (len(odd) == 1 and len(even) == 1) and odd[0][0] == even[0][0]:
        ans = n_half
    elif odd[0][0] == even[0][0]:
        ans = min(n_half - odd[0][1] + n_half - even[1][1],\
                n_half - odd[1][1] + n_half - even[0][1])
    else:
        ans = n_half - odd[0][1] + n_half - even[0][1]

    print(ans)
    return 0

if __name__ == "__main__":
    n = int(input())
    v = list(map(int, input().split()))
    main(n,v)

Buggy submission (verdict: Runtime Error):
#encoding utf-8
def main(n,v):
    # n = int(input())
    # v = list(int(i) for i in input().split())
    even_v = []
    odd_v = []

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

    odd_dic = {}
    even_dic = {}
    for i in range(len(odd_v)):
        if odd_v[i] in odd_dic.keys():
            odd_dic[odd_v[i]] += 1
        else:
            odd_dic[odd_v[i]] = 1

    for i in range(len(even_v)):
        if even_v[i] in even_dic.keys():
            even_dic[even_v[i]] += 1
        else:
            even_dic[even_v[i]] = 1

    tem_dic = {}
    for k, v in sorted(even_dic.items(), key=lambda x: -x[1]):
        tem_dic[k] = v
    even_dic = tem_dic
    tem_dic = {}
    for k, v in sorted(odd_dic.items(), key=lambda x: -x[1]):
        tem_dic[k] = v
    odd_dic = tem_dic

    # odd_dic = sorted(odd_dic.items(),key=lambda x:x[1])
    # even_dic = sorted(even_dic.items(),key=lambda x:x[1])

    odd_keys = list(odd_dic.keys())
    even_keys = list(even_dic.keys())

    for i in range(len(odd_keys)-1):
        if odd_dic[odd_keys[i]] == odd_dic[odd_keys[i+1]]:
            if odd_keys[i] > odd_keys[i+1]:
                tem = odd_keys[i]
                odd_keys[i] = odd_keys[i+1]
                odd_keys[i+1] = tem

    for i in range(len(even_keys)-1):
        if even_dic[even_keys[i]] == even_dic[even_keys[i+1]]:
            if even_keys[i] > even_keys[i+1]:
                tem = even_keys[i]
                even_keys[i] = even_keys[i+1]
                even_keys[i+1] = tem


    # print(even_dic,odd_dic)
    # print(even_keys,odd_keys)
    if (len(odd_keys) == 1 and len(even_keys) == 1) and odd_keys[0] == even_keys[0]:
        answer = n/2
    elif odd_dic[odd_keys[0]] == even_dic[even_keys[0]]:
        ans1 = n-int(odd_dic[odd_keys[0]])-int(even_dic[even_keys[1]])
        ans2 = n-int(odd_dic[odd_keys[1]])-int(even_dic[even_keys[0]])
        answer = min(ans1, ans2)
    else:
        answer = n-int(odd_dic[odd_keys[0]])-int(even_dic[even_keys[0]])

    print(int(answer))
    # return(int(answer))

if __name__ == "__main__":
    n = int(input())
    v = list(int(i) for i in input().split())
    main(n,v)

Buggy submission (verdict: Runtime Error):
#encoding utf-8
def main(n,v):
    n = int(input())
    v = list(int(i) for i in input().split())
    even_v = []
    odd_v = []

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

    odd_dic = {}
    even_dic = {}
    for i in range(len(odd_v)):
        if odd_v[i] in odd_dic.keys():
            odd_dic[odd_v[i]] += 1
        else:
            odd_dic[odd_v[i]] = 1

    for i in range(len(even_v)):
        if even_v[i] in even_dic.keys():
            even_dic[even_v[i]] += 1
        else:
            even_dic[even_v[i]] = 1

    tem_dic = {}
    for k, v in sorted(even_dic.items(), key=lambda x: -x[1]):
        tem_dic[k] = v
    even_dic = tem_dic
    tem_dic = {}
    for k, v in sorted(odd_dic.items(), key=lambda x: -x[1]):
        tem_dic[k] = v
    odd_dic = tem_dic

    # odd_dic = sorted(odd_dic.items(),key=lambda x:x[1])
    # even_dic = sorted(even_dic.items(),key=lambda x:x[1])

    odd_keys = list(odd_dic.keys())
    even_keys = list(even_dic.keys())

    # for i in range(len(odd_keys)-1):
    #     if odd_dic[odd_keys[i]] == odd_dic[odd_keys[i+1]]:
    #         if odd_keys[i] > odd_keys[i+1]:
    #             tem = odd_keys[i]
    #             odd_keys[i] = odd_keys[i+1]
    #             odd_keys[i+1] = tem
    #
    # for i in range(len(even_keys)-1):
    #     if even_dic[even_keys[i]] == even_dic[even_keys[i+1]]:
    #         if even_keys[i] > even_keys[i+1]:
    #             tem = even_keys[i]
    #             even_keys[i] = even_keys[i+1]
    #             even_keys[i+1] = tem


    # print(even_dic,odd_dic)
    # print(even_keys,odd_keys)

    if odd_keys[0] != even_keys[0]:
        answer = n-int(odd_dic[odd_keys[0]])-int(even_dic[even_keys[0]])
    else:
        if len(odd_keys) == 1 and len(even_keys) == 1:
            answer = len(odd_v)
        elif len(odd_keys) == 1:
            answer = n-int(odd_dic[odd_keys[0]])-int(even_dic[even_keys[1]])
        elif len(even_keys) == 1:
            answer = n-int(odd_dic[odd_keys[1]])-int(even_dic[even_keys[0]])
        else:
            # if odd_dic[odd_keys[1]] > even_dic[even_keys[1]]:
            #     answer = n-int(odd_dic[odd_keys[1]])-int(even_dic[even_keys[0]])
            # else:
            #     answer = n-int(odd_dic[odd_keys[0]])-int(even_dic[even_keys[1]])
            ans1 = n-int(odd_dic[odd_keys[0]])-int(even_dic[even_keys[1]])
            ans2 = n-int(odd_dic[odd_keys[1]])-int(even_dic[even_keys[0]])
            answer = min(ans1, ans2)
    print(int(answer))
    # return(int(answer))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):

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

ki = v[::2]
gu = v[1::2]

ki = Counter(ki)
gu = Counter(gu)

ki = ki.most_common()
gu = gu.most_common()

ans = 0

if ki[0][0] == gu[0][0]:
    if len(ki) == 1 and len(gu) == 1:
        ans = int(n / 2)
    else:
        if ki[1][1] > gu[1][1]:
            ans = n - ki[1][1] - gu[0][1]
        else:
            ans = n - ki[0][1] - gu[1][1]
else:
    ans = n - ki[0][1] - gu[0][1]

print(ans)

Buggy submission (verdict: Runtime Error):

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

ki = v[::2]
gu = v[1::2]

ki = Counter(ki)
gu = Counter(gu)

ki = ki.most_common()
gu = gu.most_common()

ans = 0

if ki[0][0] == gu[0][0]:
    if len(ki) == 1 and len(gu) == 1:
        ans = int(n / 2)
    else:
        if ki[1][1] > gu[1][1]:
            ans = n - ki[1][1] - gu[0][1]
        else:
            ans = n - ki[0][1] - gu[1][1]
else:
    ans = n - ki[0][1] - gu[0][1]

print("答え",ans)

Buggy submission (verdict: Runtime Error):
#encoding utf-8

n = int(input())
v = list(int(i) for i in input().split())

even_v = []
odd_v = []

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

odd_dic = {}
even_dic = {}
for i in range(len(odd_v)):
    if odd_v[i] in odd_dic.keys():
        odd_dic[odd_v[i]] += 1
    else:
        odd_dic[odd_v[i]] = 1

for i in range(len(even_v)):
    if even_v[i] in even_dic.keys():
        even_dic[even_v[i]] += 1
    else:
        even_dic[even_v[i]] = 1


for k, v in sorted(even_dic.items(), key=lambda x: -x[1]):pass
for k, v in sorted(odd_dic.items(), key=lambda x: -x[1]):pass

# odd_dic = sorted(odd_dic.items(),key=lambda x:x[1])
# even_dic = sorted(even_dic.items(),key=lambda x:x[1])

odd_keys = list(odd_dic.keys())
even_keys = list(even_dic.keys())

if odd_keys[0] != even_keys[0]:
    answer = n-int(odd_dic[odd_keys[0]])-int(even_dic[even_keys[0]])
else:
    if len(odd_keys) == 1 and len(even_keys) == 1:
        answer = len(odd_v)
    else:
        answer = mins(int(odd_dic[odd_keys[1]])-int(even_dic[even_keys[0]]), int(odd_dic[odd_keys[0]])-int(even_dic[even_keys[1]]))

print(int(answer))


































# #encoding utf-8
#
# n = int(input())
# v = list(int(i) for i in input().split())
#
#
# v_odd = []
# v_even = []
# for i in range(n):
#     if i%2 == 0:
#         v_odd.append(v[i])
#     else:
#         v_even.append(v[i])
#
# #odd part
# count={}
# for i in range(len(v_odd)):
#     if v_odd[i] in count.keys():
#         count[v_odd[i]] += 1
#     else:
#         count[v_odd[i]] = 1
#
# for k, v in sorted(count.items(), key=lambda x: -x[1]):
#     pass
# keys = list(count.keys())
#
# #even part
# even_count={}
# for i in range(len(v_even)):
#     if v_even[i] in even_count.keys():
#         even_count[v_even[i]] += 1
#     else:
#         even_count[v_even[i]] = 1
#
# # print(even_count)
# for k, v in sorted(even_count.items(), key=lambda x: -x[1]):
#     pass
# even_keys = list(even_count.keys())
#
# if even_keys[0] == keys[0]:
#     if len(keys) == 1 and len(even_keys) == 1:
#         answer = int(n/2)
#     elif len(keys) == 1:
#         answer = int(n/2)-int(even_count[even_keys[1]])
#     elif len(even_keys) == 1:
#         answer = int(n/2)-int(count[keys[1]])
#     else:
#         if int(even_count[even_keys[0]]) > int(count[keys[0]]):
#             answer = n-int(count[keys[1]])-int(even_count[even_keys[0]])
#         elif int(even_count[even_keys[0]]) < int(count[keys[0]]):
#             answer = n-int(count[keys[0]])-int(even_count[even_keys[1]])
#         elif int(even_count[even_keys[0]]) == int(count[keys[0]]):
#             if int(even_count[even_keys[1]]) > int(count[keys[1]]):
#                 answer = n-int(even_count[even_keys[1]])-int(count[keys[0]])
#             elif int(even_count[even_keys[1]]) < int(count[keys[1]]):
#                 answer = n-int(even_count[even_keys[0]])-int(count[keys[1]])
#             else:
#                 answer = n-int(even_count[even_keys[1]])-int(count[keys[0]])
# else:
#     answer = n-int(count[keys[0]])-int(even_count[even_keys[0]])
# print(int(answer))

Buggy submission (verdict: Runtime Error):
#encoding utf-8

n = int(input())
v = list(int(i) for i in input().split())


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

#odd part
count={}
for i in range(len(v_odd)):
    if v_odd[i] in count.keys():
        count[v_odd[i]] += 1
    else:
        count[v_odd[i]] = 1



for k, v in sorted(count.items(), key=lambda x: -x[1]):
    pass
keys = list(count.keys())
# num_odd = len(v_odd)-int(count[keys[0]])


#even part
even_count={}

for i in range(len(v_even)):
    if v_even[i] in even_count.keys():
        even_count[v_even[i]] += 1
    else:
        even_count[v_even[i]] = 1


for k, v in sorted(even_count.items(), key=lambda x: -x[1]):
    pass
even_keys = list(even_count.keys())
# num += len(v_even)-int(count[even_keys[0]])


if even_keys[0] == keys[0]:
    if len(keys) == 1 and len(even_keys) == 1:
        answer = int(n/2)
    elif len(keys) == 1:
        answer = int(n/2)-int(even_count[even_keys[1]])
    elif len(even_keys) == 1:
        answer = int(n/2)-int(count[keys[1]])
    else:
        if int(v_even[even_keys[0]]) >= int(v_odd[keys[0]]):
            answer = n-int(count[keys[1]])-int(even_count[even_keys[0]])
        else:
            answer = n-int(count[keys[0]])-int(even_count[even_keys[1]])
else:
    answer = n-int(count[keys[0]])-int(even_count[even_keys[0]])
print(int(answer))

Buggy submission (verdict: Runtime Error):
#encoding utf-8

n = int(input())
v = list(int(i) for i in input().split())


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

#odd part
count={}
for i in range(len(v_odd)):
    if v_odd[i] in count.keys():
        count[v_odd[i]] += 1
    else:
        count[v_odd[i]] = 1



for k, v in sorted(count.items(), key=lambda x: -x[1]):
    pass
keys = list(count.keys())
# num_odd = len(v_odd)-int(count[keys[0]])


#even part
even_count={}

for i in range(len(v_even)):
    if v_even[i] in even_count.keys():
        even_count[v_even[i]] += 1
    else:
        even_count[v_even[i]] = 1


for k, v in sorted(even_count.items(), key=lambda x: -x[1]):
    pass
even_keys = list(even_count.keys())
# num += len(v_even)-int(count[even_keys[0]])


if even_keys[0] == keys[0]:
    if len(keys) == 1 and len(even_keys) == 1:
        answer = int(len(v_odd))
    elif len(keys) == 1:
        answer = len(v_even)-int(even_count[even_keys[1]])
    elif len(even_keys) == 1:
        answer = len(v_odd)-int(count[keys[1]])
    else:
        if int(v_even[even_keys[0]]) >= int(v_odd[keys[0]]):
            answer = len(v_odd)-int(count[keys[1]])
            answer += len(v_even)-int(even_count[even_keys[0]])
        else:
            answer = len(v_odd)-int(count[keys[0]])
            answer += len(v_even)-int(even_count[even_keys[1]])

else:
    answer = n-int(count[keys[0]])-int(even_count[even_keys[0]])
print(int(answer))

Buggy submission (verdict: Runtime Error):
#encoding utf-8

n = int(input())
v = list(int(i) for i in input().split())


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

#odd part
count={}
for i in range(len(v_odd)):
    if v_odd[i] in count.keys():
        count[v_odd[i]] += 1
    else:
        count[v_odd[i]] = 1



for k, v in sorted(count.items(), key=lambda x: -x[1]):
    pass
keys = list(count.keys())
# num_odd = len(v_odd)-int(count[keys[0]])


#even part
even_count={}

for i in range(len(v_even)):
    if v_even[i] in even_count.keys():
        even_count[v_even[i]] += 1
    else:
        even_count[v_even[i]] = 1


for k, v in sorted(even_count.items(), key=lambda x: -x[1]):
    pass
even_keys = list(even_count.keys())
# num += len(v_even)-int(count[even_keys[0]])


if even_keys[0] == keys[0]:
    if len(keys) == 1 and len(even_keys) == 1:
        answer = int(len(v_odd))
    elif len(keys) == 1:
        answer = len(v_even)-int(v_even[even_keys[1]])
    elif len(even_keys) == 1:
        answer = len(v_odd)-int(v_odd[keys[1]])
    else:
        if int(v_even[even_keys[0]]) >= int(v_odd[keys[0]]):
            answer = len(v_odd)-int(count[keys[1]])
            answer += len(v_even)-int(even_count[even_keys[0]])
        else:
            answer = len(v_odd)-int(count[keys[0]])
            answer += len(v_even)-int(even_count[even_keys[1]])

else:
    answer = len(v_odd)-int(count[keys[0]])+len(v_even)-int(even_count[even_keys[0]])
print(int(answer))

Buggy submission (verdict: Runtime Error):
#encoding utf-8

n = int(input())
v = list(int(i) for i in input().split())


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

#odd part
count={}
for i in range(len(v_odd)):
    if v_odd[i] in count.keys():
        count[v_odd[i]] += 1
    else:
        count[v_odd[i]] = 1

if len(count) == 1:
    pass
    # num_odd = 0
else:
    for k, v in sorted(count.items(), key=lambda x: -x[1]):
        pass
    keys = list(count.keys())
    # num_odd = len(v_odd)-int(count[keys[0]])
# print(num)

#even part
even_count={}

for i in range(len(v_even)):
    if v_even[i] in even_count.keys():
        even_count[v_even[i]] += 1
    else:
        even_count[v_even[i]] = 1

if len(even_count) == 1:
    pass
    # num += 0
else:
    for k, v in sorted(even_count.items(), key=lambda x: -x[1]):
        pass
    even_keys = list(even_count.keys())
    # num += len(v_even)-int(count[even_keys[0]])

if even_keys[0] == keys[0]:
    if len(keys) == 1 and len(even_keys) == 1:
        answer = int(len(keys))
    elif len(keys) == 1:
        answer = len(v_even)-int(v_even[even_keys[1]])
    elif len(even_keys) == 1:
        answer = len(v_odd)-int(v_odd[keys[1]])
    else:
        if int(v_even[even_keys[0]]) >= int(v_odd[keys[0]]):
            answer = len(v_odd)-int(count[keys[1]])
            answer += len(v_even)-int(even_count[even_keys[0]])
        else:
            answer = len(v_odd)-int(count[keys[0]])
            answer += len(v_even)-int(even_count[even_keys[1]])
print(int(answer))