Allowance
Solution sketch
The sentence each denomination of coin evenly divides the next-larger denomination
is a good lead for you to safely assume that using greedy strategy is fine.
If the coin value is greater than $C$, then there is no need to combine two coins into one. Just distribute them out.
For all other remaining coin values, use greedy strategy (just like coin change), and start with the largest coin value to try to get the total value as close to $C$ using minimal coins as possible. If the total value we can get is not exactly $C$, then we run the greedy algorithm from the smallest coin to the largest one. There is a little trick though, $min(\frac{remaining\ value + current\ coin\ value\ - 1}{current\ coin\ value}, all\ remaining\ coins\ for\ this\ coin\ value)$ can get you the minimal amount of coins by exceeding $C$ no more than the value of the minimal coin value that still have coins available.
This tutorial have a casual prove on this method.
AC code
|
|