code là:

from collections import Counter

def mex(s):
i = 0
while i in s:
i += 1
return i

def valid_partition(partition, A):
# Kiểm tra phân hoạch hợp lệ
count_A = Counter(A)
for subset in partition:
count_subset = Counter(subset)
if count_A != count_subset:
return False
return True

def smallest_mex(A):
n = len(A)
A.sort() # Sắp xếp A để dễ dàng xử lý
min_mex = float('inf')

# Duyệt qua các phân hoạch của A (tạo ra các phân hoạch hợp lệ)
for partition in generate_partitions(A):
    if valid_partition(partition, A):
        partition_mex = mex(partition[0])  
        min_mex = min(min_mex, partition_mex)

return min_mex

Hàm sinh tất cả phân hoạch của A

def generate_partitions(A):
# Implement code to generate all partitions of A
pass

Ví dụ sử dụng

A = [1, 2, 3, 3]
print(smallest_mex(A))

Bình luận

Mới nhất
Tải bình luận...

Không có bình luận nào.