Editorial for WC '18 Contest 2 J4 - Ammunition


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Let's refer to guards whose B values are 0 as zero-guards, and to the remaining guards as positive-guards. A few useful observations can be made:

  • If Ethan has at least 2 bullets, and he tranquilizes a zero-guard, he'll still have at least 1 bullet afterwards. There's no harm in doing this whenever possible.
  • If Ethan has exactly 1 bullet, and he tranquilizes a zero-guard, he'll have 0 bullets afterwards and will need to stop. This should only be done when no other options remain.
  • If Ethan has at least 1 bullet, and he tranquilizes a positive-guard, he'll still have at least 1 bullet afterwards. The only harm in doing this is based on how many excess bullets are wasted due to the gun's capacity being reached (which is worse the more bullets Ethan has). Therefore, this should only be done when Ethan has exactly 1 bullet, unless no other options remain.

This suggests an optimal greedy strategy for Ethan to follow at each point in time:

  • If Ethan has 0 bullets remaining or there are 0 guards left, stop.
  • If all remaining guards are positive-guards, tranquilize all of them in any order.
  • If Ethan has 1 bullet remaining and there's at least 1 positive-guard, tranquilize any positive-guard.
  • Otherwise, tranquilize any zero-guard.

It's possible to simulate this strategy in \mathcal O(N^2) time.

It's also possible to consider what exactly will occur over the course of the strategy and come up with a closed-form answer without needing to simulate it. Firstly, if S = 0, then the answer is 0. Otherwise, the answer is at most N, and at most the total number of bullets which Ethan loads into his gun (including the initial S bullets). For each positive-guard i, he can tranquilize them when he has exactly 1 bullet and thus load \min(B_i, M) bullets into his gun (unless he has so many bullets that he's forced to shoot a positive-guard while having 2 or more bullets, in which case the answer will come out to N either way). This gives us an answer of:

\displaystyle \min\left(S+\sum_{i=1}^N \min(B_i, M), N\right)

when S > 0, which may be evaluated in \mathcal O(N) time.


Comments

There are no comments at the moment.