Barber Shop Queue
InterviewTime limit5sMemory limit512 MB
Find which of B barbers with different cutting times serves the Nth customer when ties go to the smallest barber number.
- Level
Medium5 of 10
- Topics
- Binary search, Math
- Solved
- No attempts yet
Problem
You are waiting in a long line for a haircut at a popular barber shop. The shop has barbers on duty, numbered 1 through . Barber always takes exactly minutes to cut one customer's hair, and a barber cuts only one customer's hair at a time. As soon as a barber finishes, he takes the next customer.
The customer at the head of the line goes to the free barber with the smallest number. If no barber is free, that customer waits until one becomes free.
You are the th person in line, and the shop has just opened. Find the number of the barber who will cut your hair.
Input
The first line contains the number of test cases . test cases follow, and each one consists of two lines.
The first line contains the number of barbers and your place in line , separated by a space. 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 form Case #x: y, where is the test case number starting from 1 and is the number of the barber who will cut your hair.
Explanation
Consider the first example case. You are the fourth customer, barber 1 takes 10 minutes and barber 2 takes 5 minutes. The moment the shop opens, the first customer can choose between barbers 1 and 2, so she goes to barber 1, the smaller number. 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. After 10 minutes barbers 1 and 2 both become free, and you are next, so you choose barber 1 out of the two.