Editorial for Facebook Hacker Cup '15 Round 1 P3 - Winning at Sports
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.
Submitting an official solution before solving the problem yourself is a bannable offence.
This problem has a fairly standard dynamic programming formulation:
Let be the number of ways to achieve a stress-free victory when we currently have points, the opponent has points, and the final score will be -. The answer we're looking for is then . We can define recursively as follows, assuming and :
- (we're done!)
- if (all that's left is for us to finish scoring)
- otherwise (this victory is not stress-free)
- (all that's left is for them to finish scoring)
- if and (this victory is not stress-free)
- otherwise (either team can score next)
Similarly, let be the number of ways to achieve a stressful victory:
- (we're done!)
- (all that's left is for us to finish scoring)
- (this victory is not stressful)
- if (this victory is not stressful)
- otherwise (either team can score next)
Obviously, the latter two parameters don't change, so we just need memory to memoize the results, and the time complexity will also be .
Comments