A small harbor maintains three classes of docking berths reserved for different ship sizes: Class 1 (large ships), Class 2 (medium ships), and Class 3 (small ships). Each class has a fixed number of available berths, set once at the start of the day and never replenished. As ships arrive one at a time requesting a berth of a specific class, the harbor master assigns an available berth of that exact class if one remains; otherwise the ship is turned away and must seek harbor elsewhere. Simulate the day's arrivals in order and report the harbor master's decision for each one.
Print Q lines. For the j-th arrival (in input order), print 1 if a berth of the requested class was available and was assigned to that ship (permanently reducing that class's remaining count by one), or 0 if no berth of that class remained and the ship was turned away.
Example 1
Input
1 2 3 5 1 2 2 3 1
Expected
1 1 1 1 0
Explanation
Remaining berths start at [1,2,3] for classes [1,2,3]. Request 1 (class 1): 1 remaining, assign, now [0,2,3] -> output 1. Request 2 (class 2): 2 remaining, assign, now [0,1,3] -> 1. Request 3 (class 2): 1 remaining, assign, now [0,0,3] -> 1. Request 4 (class 3): 3 remaining, assign, now [0,0,2] -> 1. Request 5 (class 1): 0 remaining, refuse -> 0. Output: 1,1,1,1,0.
Example 2
Input
0 1 0 4 1 2 3 2
Expected
0 1 0 0
Explanation
Remaining berths start at [0,1,0]. Request 1 (class 1): 0 remaining, refuse -> 0. Request 2 (class 2): 1 remaining, assign, now [0,0,0] -> 1. Request 3 (class 3): 0 remaining, refuse -> 0. Request 4 (class 2): 0 remaining now, refuse -> 0. Output: 0,1,0,0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →