这是一个有趣的动态规划问题。让我分析一下黑塔女士的攻击机制:
- 普通攻击对单个敌人造成1点伤害
- 当敌人生命值首次降到初始值的一半(向下取整)时,触发追加攻击对所有敌人造成1点伤害
- 追加攻击可以连锁触发更多追加攻击
关键洞察是:我们需要找到一个攻击序列,使得追加攻击的连锁反应能够最大化地减少所需的普通攻击次数。
让我用动态规划来解决:
def min_attacks(enemies):
n = len(enemies)
if n == 0:
return 0
# 使用记忆化搜索
from functools import lru_cache
@lru_cache(maxsize=None)
def dp(state):
# state是一个元组,表示每个敌人当前的生命值
if all(hp <= 0 for hp in state):
return 0
min_cost = float('inf')
# 尝试攻击每个还活着的敌人
for i in range(n):
if state[i] <= 0:
continue
# 攻击敌人i
new_state = list(state)
new_state[i] -= 1
# 检查是否触发追加攻击
triggered = []
original_hp = enemies[i]
threshold = original_hp // 2
if new_state[i] == threshold and state[i] > threshold:
triggered.append(i)
# 处理追加攻击的连锁反应
while triggered:
next_triggered = []
for j in range(n):
if new_state[j] > 0:
new_state[j] -= 1
original_hp_j = enemies[j]
threshold_j = original_hp_j // 2
if new_state[j] == threshold_j and new_state[j] + 1 > threshold_j:
next_triggered.append(j)
triggered = next_triggered
cost = 1 + dp(tuple(new_state))
min_cost = min(min_cost, cost)
return min_cost
initial_state = tuple(enemies)
return dp(initial_state)
# 但是上面的方法对于大数据会超时,我们需要更高效的方法
def min_attacks_optimized(enemies):
n = len(enemies)
# 计算每个敌人需要的最少普通攻击次数(不考虑追加攻击)
def attacks_needed(hp):
if hp <= 0:
return 0
threshold = hp // 2
return hp - threshold
# 贪心策略:优先攻击能触发最多连锁反应的敌人
total_attacks = 0
current_hp = enemies[:]
while any(hp > 0 for hp in current_hp):
best_target = -1
best_benefit = -1
for i in range(n):
if current_hp[i] <= 0:
continue
# 计算攻击敌人i的收益
benefit = 0
temp_hp = current_hp[:]
temp_hp[i] -= 1
# 模拟追加攻击
if temp_hp[i] == enemies[i] // 2 and current_hp[i] > enemies[i] // 2:
# 会触发追加攻击
queue = [i]
while queue:
next_queue = []
for j in range(n):
if temp_hp[j] > 0:
temp_hp[j] -= 1
if temp_hp[j] == enemies[j] // 2 and temp_hp[j] + 1 > enemies[j] // 2:
next_queue.append(j)
queue = next_queue
# 计算总伤害
total_damage = sum(current_hp[j] - temp_hp[j] for j in range(n))
benefit = total_damage
else:
benefit = 1
if benefit > best_benefit:
best_benefit = benefit
best_target = i
# 执行最佳攻击
current_hp[best_target] -= 1
if current_hp[best_target] == enemies[best_target] // 2 and current_hp[best_target] + 1 > enemies[best_target] // 2:
# 触发追加攻击
queue = [best_target]
while queue:
next_queue = []
for j in range(n):
if current_hp[j] > 0:
current_hp[j] -= 1
if current_hp[j] == enemies[j] // 2 and current_hp[j] + 1 > enemies[j] // 2:
next_queue.append(j)
queue = next_queue
total_attacks += 1
return total_attacks
# 测试用例
test_cases = [
[5, 6],
[1, 2, 3, 4, 5],
[1, 3, 6],
[6, 6, 6, 6, 6, 6],
[4, 4, 4, 4, 4]
]
for case in test_cases:
result = min_attacks_optimized(case)
print(f"{case} -> {result}")
实际上,这个问题有一个更简洁的数学解法。关键观察是:
对于生命值为h的敌人,触发追加攻击的阈值是h//2。要杀死这个敌人,至少需要h - h//2次普通攻击(因为追加攻击可以处理剩下的伤害)。
最优策略是按照生命值从大到小的顺序攻击敌人,这样可以最大化追加攻击的连锁效应。
def min_attacks_final(enemies):
# 按生命值降序排序,但保持原始索引
indexed_enemies = [(enemies[i], i) for i in range(len(enemies))]
indexed_enemies.sort(reverse=True)
total = 0
for hp, _ in indexed_enemies:
if hp > 0:
attacks_needed = hp - hp // 2
total += attacks_needed
return total
对于给定的测试用例,这个算法应该能给出正确答案。