On the Bus
Time limit1sMemory limit128 MB
Track passengers boarding and alighting over a series of stops, never exceeding the bus capacity, and report the final count.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
A bus leaves the depot with 4 passengers aboard. At the first stop, 3 more passengers board. At the next stop, 1 passenger gets off and 2 board. How many passengers are on the bus now?
Each scenario in this problem follows the same idea: track the passengers as the bus moves through its stops and report how many are aboard once the scenario ends.
Input
The input contains several scenarios.
Each scenario is given as follows:
- The first line contains a route number (up to 5 letters or digits, no spaces) and , the capacity of the bus (the maximum number of passengers, ), separated by a space.
- The second line contains , the number of passengers already on the bus ().
- The third line contains , the number of stops to process ().
- Each of the next lines contains two integers separated by a space: the number of passengers getting off at that stop, followed by the number of passengers waiting to board.
At each stop the alighting passengers get off first. If the number of passengers waiting to board is greater than the number of free seats, the passengers who cannot be seated are left behind (standing passengers are not allowed).
The input ends with a line containing exactly # 0. This line is not a scenario and must not be processed.
Output
For each scenario, print one line: the route number exactly as it appears in the input, a single space, and the number of passengers aboard the bus at the end of the scenario.