Scheduler
InterviewTime limit2sMemory limit256 MB
Simulate a priority scheduler for T seconds where the process with the largest p_i + t_i wins ties by smallest index, and report each process's total run time.
- Level
Medium7 of 10
- Topics
- Simulation, Heap, Greedy, Implementation
- Solved
- No attempts yet
Problem
In the multitasking operating system <> there are processes running. Each process has a priority , which affects how often the process runs. The system has only one core on one processor to run things, so it has to distribute CPU time among these processes while taking their priorities into account.
The algorithm that decides which process runs at each moment works as follows. Besides the priority , each process also has a counter . Initially all equal 0. Then every second:
- The processes with the maximum value of are chosen.
- Among those processes, the process with the smallest number is chosen.
- The chosen process runs for one second.
- For the chosen process , the value of is set to 0.
- For all other processes, the value of is increased by 1.
Model the work of the operating system for seconds and calculate how many seconds each process ran. All calculations and switches between processes are instant, so the running time of each process is an integer number of seconds.
Input
The first line contains two space-separated integers and : the number of processes in the operating system () and the number of seconds to be modeled ().
The second line contains space-separated integers : the process priorities ().
Output
In the only line of the output, print space-separated integers: how many seconds each of the processes ran.