Button Bashing
InterviewTime limit1sMemory limit256 MB
Press add and subtract buttons from 0, clamped between 0 and 3600, to reach a target time in the fewest presses or the nearest reachable longer time.
- Level
Medium4 of 10
- Topics
- BFS, Shortest path, Graph
- Solved
- No attempts yet
Problem
You bought a new microwave, and it has a lot of buttons for entering the cooking time quickly. Some buttons add time and some subtract it. You want to enter the cooking time you have in mind with as few button presses as possible.
The cooking time is at least 0 seconds and at most 1 hour. If a button press would make the cooking time less than 0 seconds, the microwave sets the cooking time to 0 seconds. If a button press would make the cooking time more than 1 hour, the microwave sets the cooking time to 1 hour. The cooking time starts at 0 seconds. At least one button always adds 1 second or more.
Given the buttons of the microwave and a desired cooking time, find the smallest number of button presses that reaches that time. If the desired time cannot be entered exactly, find the shortest reachable cooking time that is not below the target, and the smallest number of button presses that reaches it. The cooking time cannot be changed once the microwave starts cooking.
Input
The first line has the number of test cases, a positive integer of at most 100.
Each test case has two lines.
- One line with two space-separated integers and (, ): the number of buttons available and the desired cooking time in seconds.
- One line with space-separated integers (): the number of seconds added to the cooking time when button is pressed.
Output
For each test case, print one line with two space-separated integers: the minimum number of button presses needed, and the minimum number of extra seconds that the microwave must run beyond the target.