#C272. Task Scheduler
Task Scheduler
Task Scheduler
You are given the task of simulating a Task Scheduler system. In this system, each task has a unique task id and an associated priority value. The scheduler supports three operations:
insert x p
: Insert a task with idx
and priorityp
into the scheduler. If a task with the same id already exists, the insertion is ignored.execute
: Remove from the scheduler and output the task id with the highest priority. If there are multiple tasks with the same highest priority, the task that was inserted first is executed. If the scheduler is empty, output-1
.next
: Output (without removing) the task id with the highest priority (breaking ties by insertion order as inexecute
). Output-1
if no tasks exist.
The priority comparison is defined mathematically as: choose the task with the maximum value of \(-p\) (i.e. maximum \(p\)) and if two tasks \(i\) and \(j\) have equal priority, then the one with the lower insertion index is considered higher. Your goal is to process a series of operations and output the results of the execute
and next
operations.
Note: All formulas are written in LaTeX. For example, the priority value is compared such that if tasks have priorities \(p_i\), the scheduler returns the task with the maximum \(p_i\) and for equal \(p_i\), the smallest insertion order is chosen.
inputFormat
The first line contains a single integer \(Q\) representing the number of operations. Each of the following \(Q\) lines is one of the following commands:
insert x p
- insert a task with id \(x\) and priority \(p\).execute
- execute and remove the task with the highest priority; output its id.next
- output the id of the task with the highest priority without removing it.
Input is read from standard input (stdin).
outputFormat
For each execute
or next
command, output the corresponding task id in a new line. If the scheduler is empty when these commands are given, output -1
. The output is printed to standard output (stdout).
5
insert 1 5
insert 2 10
next
execute
next
2
1
</p>