Queues of a supermarket (3) P79292


Statement
 

pdf   zip

thehtml

Simulate the behavior of the queues of the supermarket of a military headquater: Initially, there are n queues (1, 2, …, n), each one with some customers. Afterwards, two events can happen:

  • A costumer arrives to a queue: If the queue is between 1 and n, the customer goes to the end of that queue. Otherwise, the event is ignored.
  • A customer leaves a queue: If the queue is between 1 and n, and that queue is not empty, the customer with the highest graduation leaves the queue (in case of a tie, the one that entered first leaves the queue). Otherwise, the event is ignored.

Input

Input starts with the number of queues n (a strictly positive natural number). Follow n lines, one per queue, each one with its customers (a word) and their graduations (“soldier”, “sergeant”, “captain”, or “colonel”), in the order in which they arrived to que queue. Follow an empty line and the description of several events, one per line: the word “ENTERS” followed by the customer, the customer’s graduation, and the queue; or the word “LEAVES” followed by the queue.

Output

First, print the name of the customers that leave the queues, in the order that they departed. Afterwards, print the final content of the n queues, using the order in which the customers would leave. Follow the format of the example.

Public test cases
  • Input

    4
    Cristina soldier Tomas captain
    Francesc sergeant Damia soldier Domenec colonel
    
    Teresa captain Toni sergeant Carles captain
    
    LEAVES 1
    LEAVES 1
    ENTERS Amalia sergeant 4
    ENTERS Pep soldier 4
    LEAVES 2
    LEAVES 1
    ENTERS Leo captain 1
    ENTERS Maria sergeant 4
    LEAVES 4
    LEAVES 4
    LEAVES 4
    ENTERS Carme soldier 4
    LEAVES 2
    LEAVES -1
    LEAVES 2
    

    Output

    DEPARTS
    -------
    Tomas
    Cristina
    Domenec
    Teresa
    Carles
    Toni
    Francesc
    Damia
    
    FINAL CONTENTS
    --------------
    queue 1: Leo
    queue 2:
    queue 3:
    queue 4: Amalia Maria Pep Carme
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan Spanish
    Official solutions
    C++ Python
    User solutions
    C++ Python