Queue Logic - Python

Asked

Viewed 474 times

2

I have a question on a question, and I hope you can help me, the question itself is not how to develop the question, that is not the problem, the problem is in the logic of the question that I am not able to understand, the question is the following:

The legislation in force obliges banks to initiate the service to a client in no more than 20 minutes after the customer entry in the queue single bank branch. The queue is unique, thus a free cashier asks the first customer of the queue to come to your window to be served. (We will ignore here the problem of priority customers, elderly, pregnant women, people with special needs, etc.) We are assuming also that no cashier serves two customers at the same time.

Your program will receive the number of active boxes in the agency, the number of customers and, for each customer, two information, namely the moment customer’s entry in the queue, and the duration of that customer’s service client. Initially all boxes are empty, since the agency just opened. Your problem is to determine the number of customers who wait more than 20 minutes to have your service started.

Entree:

The first line of the entry contains two integers separated by one blank space. The first, C, is the number of active boxes in the bank branch. The second, N, the number of clients who will seek service at the agency that day. The next N lines will have each a customer information, consisting of two integers, T and D, separated by a blank space. The integer T provides the moment at the customer queues in minutes from the instant of agency opening. Integer D provides, in minutes, the time necessary to meet the customer. The lines are ordered by customers' queue entry.

Suppose:

1 <= C <= 10 1 <= N <= 1000 0 <= T <= 300 1 <= D <= 10

Exit:

The output should contain only one line, containing a single integer, the number of customers whose service will be started more than 20 minutes after your queue entry.

The entrance he gives is as follows:

1 5 - 0 10 - 0 10 - 1 10 - 2 10 - 30 10

Being the 1 the total of active boxes, 5 the total of customers, and the other the time of arrival of the customer and the time it will take to be served respectively.

And the exit in this case is 1.

So, come on, as I understood it in this case the first person in the queue would have no waiting time, would be met directly, the second would wait for the first 10 of the first, for having entered together, the third would wait for the first 10 minutes, but having arrived in minute 1 would expect only 9 of the second, totaling 19 correct? From here I start to curl up, the fourth, would wait the 30 minutes of the first 3, but because I arrived in the second minute would wait 28 minutes? In case he would be the client who would blow up? The last one would arrive in the 30th minute but how would the time be in 28min he would wait only 2min? And if by one case another client arrived in the 36th minute, for example, how would you look? And in this example, only 1 active box is being used, and it is possible to have up to 10.

I wonder if you can shed some light on the logic of this matter, on how this account should be made because I’m half lost.

1 answer

1

See the following timeline.

(12)-(123)-(1234)--------( 234)----------( 34)----------( 45)----------( 5)----------( )
 ^^     ^      ^          1               2              3 ^            4              5
  • Each stroke is a minute gone by
  • In parentheses is the customer queue (the leftmost is being served)
  • The bottom line indicates who entered (with ^) and who left (by downloading the number)

Analyzing the events:

  • In the zero minute, the first and second customer came in and the first started to be met
  • It’s been a minute
  • In minute 1, the third customer entered
  • It’s been a minute
  • At minute 2, the fourth customer entered
  • It’s been 8 minutes
  • In the 10th minute, the first customer left and the second began to be met
  • It’s been 10 minutes
  • In the 20th minute, the second customer left and the third started to be met
  • It’s been 10 minutes
  • In the 30th minute, the third customer left, the fourth began to be attended and the fifth entered
  • It’s been 10 minutes
  • In the 40th minute, the fourth customer left and the fifth started to be attended
  • It’s been 10 minutes
  • In the 50th minute, the fifth customer left

Calculating the waiting time:

  • Customer 1 waited 0 minutes (entered minute 0, was met at minute 0)
  • Client 2 waited 10 minutes (entered the minute 0, was met in the minute 10)
  • Customer 3 waited 19 minutes (entered minute 1, was serviced at minute 20)
  • Customer 4 waited 28 minutes (entered minute 2, was serviced in minute 30)
  • Customer 5 waited 10 minutes (entered the 30 minute, was met in the 40 minute)

That is, customer 4 was the only one who waited more than 20 minutes, so the output of the program is 1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.