Find the fewest feedings over D days so no gap exceeds X seconds while skipping work and sleep hours.
Medium7GreedyMathIntervalsNo attempts yetTime limit5sMemory limit512 MBAlice just bought a virtual pet rabbit. The rabbit hops around on a screen, and pressing a button feeds it. Alice is fond of the rabbit, but she is also busy and does not want to spend much time taking care of it. If the rabbit goes without food for too long it dies, and Alice loses the game.
Every day Alice gets up at time G, leaves for work at time W, comes home at time H, and goes to bed at time B. She cannot feed the rabbit while she is at work or asleep, that is, during the intervals [W,H) and [B,G). The seconds W and B are not valid feeding times, while the seconds H and G are. In every other second Alice either presses the button, which feeds the rabbit instantly, or does nothing. Between every two consecutive seconds the rabbit counts how many seconds in a row it has gone without food, and it dies at the moment that count equals X. The gap between two consecutive feedings is therefore at most X seconds.
It is now 00:00:00 on day 0, and the mail carrier has just delivered the rabbit to Alice's house. The carrier presses the button at 00:00:00 even if Alice is asleep, and then leaves. That press is not counted as one of Alice's feedings. Alice wants the rabbit to still be alive at 00:00:00 on day D. By the same rule, the gap between the last feeding and 00:00:00 on day D must not exceed X seconds either. Find the smallest number of times Alice has to feed the rabbit, if she can keep it alive at all.
The first line holds the number of test cases T. The T test cases follow, each of them 6 lines long. The first 5 lines hold the times G, W, H, B and X in "hh:mm:ss" format. The last line holds one integer D.
For each test case print one line of the form "Case #x: y", where x is the test case number starting from 1 and y is the smallest number of times Alice needs to feed the rabbit. If the rabbit cannot be alive at 00:00:00 on day D, print -1 in place of y.
In the first case of the example, Alice can feed the rabbit at 08:00:00 and 20:00:00 every day. In the second case the rabbit dies before Alice even wakes up on day 0.