Haircut Queue
InterviewTime limit5sMemory limit512 MB
Find which of B barbers with fixed cutting times serves the Nth customer when each free barber takes the next waiting customer.
- Level
Medium5 of 10
- Topics
- Binary search, Math
- Solved
- No attempts yet
Problem
A long line has formed outside a fashionable barber shop, and you are standing in it. The shop has barbers on duty, numbered 1 through . The th barber always takes exactly minutes to cut one customer's hair, and can only cut one customer's hair at a time. As soon as a barber finishes, that barber takes the next customer.
The customer at the head of the line goes to the available barber with the lowest number. If no barber is available, that customer waits until at least one becomes available.
You are the th person in line, and the shop has just opened. Find the number of the barber who cuts your hair.
Input
The first line contains the number of test cases . Each test case then follows on two lines. The first line of a test case contains two space-separated integers and , the number of barbers and your place in line. The customer at the head of the line is number 1, the next one is number 2, and so on. The second line contains , separated by spaces.
Limits
Output
For each test case, print one line in the format Case #x: y, where is the test case number starting from 1 and is the number of the barber who cuts your hair.
Note
Take the case , , , . The moment the shop opens, the first customer can choose between barbers 1 and 2, so she takes the lower number, barber 1. The second customer goes straight to barber 2. The third customer waits, because no barber is free. After 5 minutes barber 2 finishes the second customer and takes the third one. At minute 10 barbers 1 and 2 finish together, and you are next in line, so you choose between them and take barber 1.