Chefs' orders

3.3

3 votes
Queue, Data Structures, Queues, Basics of Queues
Problem

There are N people entering a restaurant at different times (let us say they are numbered from 1 to N). There are K chefs working in the restaurant (let us say they are numbered from 1 to K). A person orders immediately after he enters the restaurant. The arrival time of a person is given in an array A and the time it takes to prepare his orders are given in an array B. You are also given an array C where Ci denotes that if the ith person waits for 1 unit of time after ordering the food (which is the same as entering the restaurant), his anger will increase by this amount. It is known that the restaurant will only be open till time 109 units and all peoples' orders must be prepared by then (read verdict and scoring section for more information). You need to minimize the sum of total anger of all the people.

Due to a new policy, each chef has a contract from the restaurant on a timely basis. This means that each chef will stop accepting orders after a certain amount of time. For instance, if the chef 1 has his contract with 10 units of time, he will not accept orders after he has worked for at least 10 units of time. Note that the chef will still complete any ongoing orders that he has. Note that the chef's contract is the time that he should be preparing orders. If a chef is idle (not preparing any order for a customer), that does not count as his working time.

Note: Only one chef will work on preparing the order for one person. A chef can only prepare one order at a time and a person's order can only be prepared by one chef. Also if a chef completes preparation of an order at time t, he will only be able to pick up and start preparing a new one at time t+1.

Input format

  • The first line contains two space-separated integers N and K.
  • The second line contains an array A of N integers where Ai denotes arrival time of the ith person.
  • The third line contains an array B of N integers where Bi denotes the time it takes for preparing order for a person i.
  • The fourth line contains an array C of N integers where Ci denotes the anger of the ith person for each unit of time he has to wait. 
  • The fifth line contains an array D of K integers where Di denotes the time unit mentioned in the ith chef's contract.

Output format

Print N lines. Each line contains two space-separated integers (say timei and chefi) denoting that ith person's order will be started to prepare at time timei by the chef chefi. Note that 1-based indexing must be used for printing the chef. Please refer to the verdict and scoring section for caveats and the sample explanation for better understanding.

Constraints

1KN1041Ai1051Bi,Ci1Di109

It is given that ki=1Di=ni=1Bi. And under this contraint, it can be proved that an answer always exists.

Verdict and scoring

Do note that if your output states that a person's order will be started processing at time tand all the K chefs are busy at time t, it will result in a wrong answer verdict. Since the restaurant closes at time t=109, all the orders must be ready by then. If there is any person whose order is still in preparation or not yet given for preparation, it will result in a Wrong Answer verdict. If a chef is asked to prepare food, but his contract time is expired, it will still result in a Wrong Answer verdict.

The scoring for this problem is relative. The contestant with the best answer(in this case minimum anger) will receive the highest score and your score will be relative to that. 

Sample Input
5 2
5 10 15 20 25
11 11 5 2 4
124 10 14 135 42
1 32
Sample Output
5 1
10 2
21 2
26 2
28 2
Time Limit: 10
Memory Limit: 256
Source Limit:
Explanation

Here there are 2 chefs available(lets call them chef1 and chef2) at the restaurant and 5 people enter. One of the possible ways to address these orders is:

  • chef1 will prepare the order of 1st person as soon as he arrived. Since the contract of chef1 only states that he should pick orders upto 1 unit of time, this will be his first and last order. Now, only one chef(chef2) will be able to address the orders.
  • When the 2nd person arrives, chef2 will be free and starts preparing their order. So, now chef2 will only be available to prepare another order at time = 21 units.
  • When the 3rd person arrives, chef2 is busy. chef1 gets free at time = 16 units, but since his contract time is expired, he will no longer accept orders. Thus, chef2 will start preparing this order as soon as he gets free(at time = 21 units). He will be available at time = 26 units.
  • The 4th person arrives at time 20 units. chef1 has finished his order, but he no longer will be accepting any orders. chef2 will be available at time 26 units only. Hence, he will have to wait until then and then his order will start being prepared by chef2.
  • The last person arrives at time 25 units. Now, the 4th person will also be waiting at that time. chef2 becomes free at time 26 and can choose any order of serving the customers. In the sample output, chef2 serves the 4th person first and after completion of his order, starts preparing the last person's order.

An additional sample test is given here for your better understanding:

Sample Input

5 3
5 5 5 5 5
10 12 20 1 3
123 213 35 209 198
10 15 21

 

Sample Output

5 1
5 2
5 3
17 2
18 2

 

Explanation

Now the chef count is increased to 3 and all the customers arrive at the same time - t=5 units. One of the possible ways to address the customers is:

  • chef1 starts preparing the order of the first customer as soon as he enters. The time taken for the preparation of the first customer's order is 10 units. chef1 will be free at time 15 units. But the contract of chef1 also is of 10 units of time. Thus, he won't be working on any other orders.
  • chef2 starts preparing the order of the second customer as soon as he enters. chef2 will become free at time 17 units and he will work for 3 more units of time after the completion of this order because his contract length is 15 units of time.
  • chef3 starts preparing the order of the third customer as soon as he enters.  He will be available at time 25 units. After he is done preparing this order, he will still be available to work for 1 unit of time.
  • At time 15 units, chef1 becomes free. But since he has already worked 10 units of time(his contract time), he will no longer be serving orders. Hence, even when there are two customers in the shop whose orders are not addressed yet, chef1 will not do anything.
  • chef2 becomes free at 17 units of time. Since he still has 3 units of time left, he starts preparing order of fourth person. He becomes free at 18 units of time. He still has 2 units of time left! He immediately picks up the order of fifth person. Here only 2 units of chef's contract time is left but he still prepares an order of 3 units of time. This is because a chef will complete any pending orders that he has received during his contract time. He will not leave it in between even if it means that he has to work more than contract time. 
Editor Image

?