update
This commit is contained in:
61
luogu_py/p4530.py
Normal file
61
luogu_py/p4530.py
Normal file
@@ -0,0 +1,61 @@
|
||||
# TODO: 未完成
|
||||
def solve(n, m, balls):
|
||||
# 将球按正负数和零分类并排序
|
||||
pos = sorted([x for x in balls if x > 0], reverse=True) # 正数降序
|
||||
neg = sorted([x for x in balls if x < 0]) # 负数升序
|
||||
zeros = [x for x in balls if x == 0]
|
||||
|
||||
# 如果正数不够分配给每个篮筐,需要特殊处理
|
||||
if len(pos) < m:
|
||||
# 需要用负数或零来确保每个篮筐都有球
|
||||
return None # 这里需要根据具体情况处理
|
||||
|
||||
# 初始化篮筐
|
||||
baskets = [1] * m
|
||||
|
||||
# 优先处理负数对
|
||||
neg_pairs = []
|
||||
for i in range(0, len(neg) - 1, 2):
|
||||
neg_pairs.append(neg[i] * neg[i + 1])
|
||||
|
||||
# 如果有单独的负数,与最小的正数配对或单独放在一个篮筐
|
||||
if len(neg) % 2 == 1:
|
||||
last_neg = neg[-1]
|
||||
if pos and last_neg * pos[-1] > last_neg:
|
||||
neg_pairs.append(last_neg * pos.pop())
|
||||
else:
|
||||
# 把单独的负数放在一个新的篮筐
|
||||
smallest_basket_idx = 0
|
||||
baskets[smallest_basket_idx] *= last_neg
|
||||
|
||||
# 将负数对的结果按从大到小排序
|
||||
neg_pairs.sort(reverse=True)
|
||||
|
||||
# 分配负数对结果
|
||||
for i, pair_product in enumerate(neg_pairs):
|
||||
baskets[i % m] *= pair_product
|
||||
|
||||
# 分配零
|
||||
zero_idx = 0
|
||||
while zeros and zero_idx < m:
|
||||
if baskets[zero_idx] == 1: # 只在篮筐未使用时放零
|
||||
baskets[zero_idx] *= zeros.pop()
|
||||
zero_idx += 1
|
||||
|
||||
# 分配剩余的正数
|
||||
pos_idx = 0
|
||||
for num in pos:
|
||||
baskets[pos_idx % m] *= num
|
||||
pos_idx += 1
|
||||
|
||||
return sum(baskets)
|
||||
|
||||
|
||||
# 读取输入并处理
|
||||
while True:
|
||||
n, m = map(int, input().split())
|
||||
if n == 0 and m == 0:
|
||||
break
|
||||
balls = list(map(int, input().split()))
|
||||
result = solve(n, m, balls)
|
||||
print(result)
|
||||
Reference in New Issue
Block a user