EDF
InterviewTime limit1sMemory limit1024 MB
Given N existing jobs and M jobs arriving later, simulate earliest-deadline-first scheduling with preemption and report whether all jobs finish, and the finish time if they do.
- Level
Medium5 of 10
- Topics
- Greedy, Heap, Simulation
- Solved
- No attempts yet
Problem
Minku has so much work this year that jobs have piled up, and up to more jobs are added over time.
Each existing job is given its required time and deadline . Each added job is given its arrival time , required time , and deadline .
Minku starts with the job whose deadline is closest. If a newly added job has an earlier deadline than the job he is working on, he switches to the new job. The interrupted job keeps its progress: its required time is reduced by the time already spent on it, so he can resume it later.
For example, if a job that requires 20 minutes has been worked on for 15 minutes before he switches, he needs only 5 more minutes to finish it.
If two jobs have the same deadline, he does the one with less remaining required time first.
Determine whether Minku can finish all jobs by following this process. Minku focuses on one thing at a time, so he can work on only one job at once. He keeps working until all jobs are finished.
The current time is seconds.
Input
The first line contains the number of jobs . ()
Each of the next lines contains the required time and deadline of a job. (, , )
On the -th line, the number of jobs that are added during the process is given. ()
Each of the next lines contains the arrival time , required time , and deadline of an added job. (, , )
Output
On the first line, print NO if Minku cannot finish all jobs. Print YES if he can.
If he can finish all jobs, print the earliest time at which all jobs are completed on the second line.