标签:-- ext set problem group 组合 list lis 正整数
给定N组数字,每组数字组内数字之间互不相同,组间数字可能相同。这些数字都是正整数,现在从这N组数字中选择尽量多的数字,是的选出来的数字满足以下条件:
要求:使选出来的数字之和尽量大。
暴力想法显而易见,但是有没有更完美的方法呢?
import numpy as np
def generate_problem():
group_count = np.random.randint(5, 6)
groups = []
for i in range(group_count):
group_size = np.random.randint(2, 5)
groups.append(sorted(list(set(np.random.randint(1, 10, group_size))), reverse=True))
return groups
def brute_force(groups):
groups = [i for i in groups if i]
if not groups:
return 0
max_value = max(i[0] for i in groups)
big_groups = [i for i in groups if i[0] == max_value]
ans = 0
for big in big_groups:
next_groups = [j[1:] if j[0] == max_value else j for j in groups if j != big]
s = brute_force(next_groups) + max_value
ans = max(ans, s)
return ans
def main():
for i in range(100):
p = generate_problem()
s = brute_force(p)
print(p)
print(s)
print('=' * 10)
main()
标签:-- ext set problem group 组合 list lis 正整数
原文地址:https://www.cnblogs.com/weiyinfu/p/10539712.html