Loading...
Loading...
The college library has one printer that every student shares. Jobs are printed in the order they were sent: the job that was sent first is printed first.
You get q commands, one per line: - add name pages: the student called name sends a job with that many pages. - print: the printer prints the oldest waiting job. Print the name on that job. If no job is waiting, print EMPTY. - status: print two numbers, the number of jobs waiting and the total pages of those jobs.
An add command prints nothing.
add name pages, print or status.print and status command, in the order the commands appear.print or status.Example
Input
7 add Asha 3 add Ravi 5 status print status print print
Output
2 8 Asha 1 5 Ravi EMPTY
Asha sent her job first, so it prints first. The second status shows only Ravi's 5 pages. The last print finds nothing waiting.
Try the problem first. Open one hint at a time.
The job that came first leaves first. Which structure keeps that order?
Use an ArrayDeque as a queue: add new jobs at the back and take printed jobs from the front.
Keep a running total of waiting pages. Add on every add and subtract on every print.
The total can reach 10^5 × 10^9, so it needs a long.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor