14897 - Spider-Man’s Queue Service   

Description

this image is AI generated

 

To congratulate you on your recent achievements in developing a successful delivery service system (refer to question 14311 & 14598 for a fun backstory), you were promoted to be the manager of a new project, welcome to ‘Spider-Man’s Queue Service: Project Stacks & Queue’!

In the restaurants where our food delivery Spider-Men will pick up their order, people have to line up in a queue to order/get their food. However, due to the restaurant’s popularity there are so many people at the restaurant. When one queue is overloaded, the restaurant would open another queue to speed up the lines of impatient customers. But since opening a new queue and having a staff to attend to it consumes so much resource, the restaurant would try to close the additional queues as soon as the line becomes empty. Your task is to implement a system to keep track of the lines, this data is vital for Spider-Man’s Delivery Service data analytics team! 

 

Commands:

Enter <ct> <num> <p>

During this command, you will be given a character <ct>, and integers <num> and <p>.

Where <ct> represents the customer type, and <num> represents the total number of customers who entered the store. Lastly, <p> represents the patience of each of the customers. When the command is called, you must queue the customers into the first queue available (starting from the bottom of the stack). If the queue reaches its current capacity, move to the next queue, if all current queues in the stack are full you must push a new queue with the same capacity as the remaining customers. Enqueue all the remaining customers into the newly created queue. Each customer added will be assigned a unique ID, starting with 0.

Each customer type will be one of the following: Regular, Grumpy, and Spider-man type! Each has its own unique behavior (explained below).

 

Resize <q_id> <new_cap>

Given <q_id> an integer that represents the queue ID to resize, and <new_cap> an integer that represents the new capacity. You will resize the specified queue to a new capacity.

*<q_id> is a unique ID given to the queue when pushed into the stack.

 

Process
Process the customers that are waiting next in line. During this stage, the tasks will be handled in the following order starting from the first queue in the stack (bottom of the stack) and the first element of the queue. Complete each stage for all queues in the stack before moving to the next stage (pay attention to this):

  1. Dequeue: If the queue is not empty, handle customers at the front of each queue (dequeue, unless blocked by other conditions). If the queue is empty, do nothing.
     
  2. Patience: After dequeueing, starting from the second element of the queue (the customer who is in front of the queue is always immune to this effect). Deduct the patience score from the customer by ‘-1’ unless specified otherwise.
     
  3. Type Effects: If a customer’s patience reaches ‘0’ and is not already at the front of the queue, an effect will take place depending on their type:
    • R’ - Regular type, if patience reaches ‘0’. The customer immediately quits the queue. Customers behind it shift forward.
       
    • S’ - Spider-man type! If patience reaches ‘0’,Spider-man will immediately look to its right and left (queue above/below). If a queue contains fewer people such that when moved, Spider-man would be nearer to the front of the queue than it already is. Then Spider-man would move to that queue, prioritize the queue with fewer people, and if both adjacent queues are the same size, prioritize the queue above it. Moving to another queue will cause Spider-man’s patience to increase by 2.

      Example:
      2 –[ r | r | r | r | r ] ← s position doesn’t get nearer to the front, not moving here!
      1 –[ r | r | r | r | r | s ]
      0 –[ r | r | r | r ] ← move s to this queue instead!
       
    • G’ - Grumpy type, if patience reaches ‘0’, grumpy customers will rage at the staff causing the line to be blocked in the next turn, the patience will be increased back to 7. This blockage causes the patience for the rest of the queueing customers in the next turn to be reduced by ‘-3’ instead (except for the customer who raged). The grumpy customer who caused the blockage will get the default ‘-1’ penalty.
       
  4. Pop Stack: When a queue is empty, the restaurant would want to close the queues as soon as possible, hence it starts popping the empty queue out as soon as it can. Start popping all empty queues starting from the top of the stack until you reach a non-empty or queue or queue ID 0. Empty queues in the middle of the stack cannot be popped if the queue above it is not empty! Note, the first queue (id: 0) cannot be popped as it's the original queue the restaurant set up for customers.

Guarantees & Notes:

  • Note that each queue and customer added will be assigned a unique ID starting with 0, and incremented by ‘1’ each time.
  • Note that the minimum patience is ‘0’, and cannot be negative.
  • Note that if more than one grumpy customer raged at the same time in the same queue, the effect is applied only once and not stacked.
  • It is guaranteed that RESIZE’s new capacity will be larger than the current queue’s capacity.
  • It is guaranteed that the total queues added to the stack at any given moment won’t exceed 500.
  • Note that if any part is unclear/visualization needed. Please check the homework 1 slides first. If the answer is not present, don’t hesitate to reach out to TAs for help.
  • Testcase 1~8 can be used to test your code’s correctness as it focuses on different areas of the problem, happy debugging ;) !

Hints for Debugging

Spider-Man customer type only appear from testcase 4 onwards. Grumpy customer only appear from testcase 6 onwards.

You're recommended to use OOP to solve this problem.

More hints may be given during TA lab on Mondays if deemed necessary.

 

Input

The first line will contain an integer <qs> which specifies the capacity of the initial queue in the stack.

The following lines will be one of the commands:

  • Enter
  • Resize
  • Process 
  • Parameter size:

Parameter name

Details

Size

qs

The initial capacity of the queue

1 ≤ qs ≤ 1,000

ct

Customer type

‘R’, ‘S’, or ‘G’

num

Number of customers

1 ≤ num ≤ 2,000

p

Customer initial patience

2 ≤ p ≤ 5,000

q_id

Queue ID to be resized

Current IDs in the stack

new_cap

Queue’s new capacity

1 ≤ new_cap ≤ 1,000

Output

Output according to the specifications:

  • If a queue is blocked by rage effect, output: "queue #<q_id>: rage blocked
  • If a regular customer quits the queue, output: “queue #<q_id>: <customer_id> quit
  • If a grumpy customer raged, output: “queue #<q_id>: <customer_id> raged
  • If a Spider-man switch queue, output: “queue #<new_q_id>: <customer_id> switch in” Finally, after all commands are performed, Output: “=== End ===”.

It should then be followed by a summary of the remaining elements in each queue:

  • If queue is empty: “queue #<q_id>: empty
  • If queue is not empty: “queue #<q_id>: (<customer_id>,<remaining_patience>)

Repeat the same format for all the remaining customers in the queue, separated by: “, “ Enter a newline after every output.

*<q_id> represents the queue ID the event happens.

*<new_q_id> represents the target queue ID.

*<customer_id> represents the customer’s ID.

Sample Input  Download

Sample Output  Download




Discuss