【发布时间】:2017-01-19 14:41:52
【问题描述】:
Polycrap想要杀死一排弓箭手,他唯一的武器是火球。如果 Polycarp 用他的火球击中第 i 个弓箭手(从左到右编号),弓箭手会失去 a 个生命值。
同时该法术会伤害与第 i 个(如果有的话)相邻的弓箭手——他们失去 b (1 ≤ b ) 生命值各分。
由于极限弓箭手(即编号为 1 和 n 的弓箭手)距离很远,火球无法到达他们。 Polycarp 可以用他的火球击中任何其他弓箭手。
每个弓箭手的生命值是已知的。当此数量小于 0 时,将杀死一名弓箭手。Polycarp 可以使用多少法术来杀死所有敌人?
如果射手已经被杀死,Polycarp 可以将他的火球扔到射手身上。
I/O 说明
INPUT -The first line of the input contains three integers n, a, b (3 ≤ n ≤ 10; 1 ≤ b < a ≤ 10).
The second line contains a sequence of n integers — h1, h2, ..., hn (1 ≤ hi ≤ 15), where hi is the amount of health points the i-th archer has.
OUTPUT- In the first line print t — the required minimum amount of fire balls.
In the second line print t numbers — indexes of the archers that Polycarp should hit to kill all the archers in t shots.
现在我已经编写了一个递归函数,该函数采用一组弓箭手当前的健康状况,并生成每次可能的攻击,直到所有弓箭手都死亡并返回最小值。
但是这种方式太慢了,如何高效解决呢?
注意:我不一定对优化自己的解决方案感兴趣,但对任何其他可能更快的解决方案持开放态度。
【问题讨论】:
-
你应该在这里发布你的代码而不是链接(因为只要这个问题链接可能不存在的风险)
-
提示:想想如何在每一步都杀死第一个活着的弓箭手。如果你遵循这个策略,会有多少受伤的弓箭手
-
我不明白这句话:“如果射手已经被杀死,Polycarp 可以将他的火球扔给射手。”所以即使弓箭手的生命值
-
@shole 这个条件是必要的,因为他不能在极端情况下攻击某人,所以如果四肢旁边的玩家已经死亡,他仍然可以攻击他以破坏极端情况。
标签: algorithm recursion optimization dynamic-programming