Bathroom Stalls (Small1)
Time limit5sMemory limit512 MB
Simulate K people choosing stalls by a fixed farthest-from-others rule and report the distances around the stall the last person takes.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Heap, Greedy
- Solved
- No attempts yet
Problem
A bathroom has N + 2 stalls in a single row. The two stalls at the ends are always in use because the bathroom guards keep them, and the other N stalls are for visitors.
Whenever someone enters the bathroom, they try to pick a stall as far from other people as possible. To keep the choice unambiguous, it follows fixed rules. For every empty stall S, compute two values and . is the number of empty stalls between S and the nearest occupied stall to its left, and is the same count taken to the right. First keep only the stalls where is largest. If exactly one stall is left, that stall is the choice. If several are left, keep only those among them where is largest, and if several still remain, take the leftmost one.
K people enter the bathroom. Each person picks a stall before the next one arrives, and nobody ever leaves.
Let S be the stall picked by the last person. Find and .
Input
The first line contains the number of test cases T. Each of the next T lines describes one test case with two integers N and K.
Limits
Output
For each test case, print one line containing Case #x: y z, where x is the test case number (starting from 1), y is , and z is for the stall S picked by the last person to enter.
Hint
In the first test case of the example, the first person takes the left one of the two middle stalls. Writing an occupied stall as O and an empty one as ., the row becomes O.O..O. The second and last person takes the stall immediately to the right, which leaves 1 empty stall on one side and 0 on the other.
In the second test case, the first person takes the middle stall and the row becomes O..O..O. The second and last person takes the leftmost empty stall.
In the third test case, the first person takes the left one of the two middle stalls and the row becomes O..O...O. The second person takes the center of the three consecutive empty stalls.
In the fourth test case, every stall ends up occupied no matter which stalls the people pick.
In the fifth test case, the first and only person takes the left one of the two middle stalls.