48 lines
1.2 KiB
C++
48 lines
1.2 KiB
C++
#include <unordered_map>
|
|
#include <queue>
|
|
|
|
/*
|
|
Task scheduling with changeable priorities
|
|
*/
|
|
|
|
class PriorityManager {
|
|
private:
|
|
std::unordered_map<int, int> info;
|
|
std::priority_queue<std::pair<int, int>> heap;
|
|
public:
|
|
PriorityManager(std::vector<std::pair<int, int>>& tasks) {
|
|
for (auto& task : tasks) {
|
|
int taskId = task.first, priority = task.second;
|
|
info[taskId] = priority;
|
|
heap.push(std::make_pair(priority, taskId));
|
|
}
|
|
}
|
|
|
|
void add(int taskId, int priority) {
|
|
info[taskId] = priority;
|
|
heap.push(std::make_pair(priority, taskId));
|
|
}
|
|
|
|
void edit(int taskId, int newPriority) {
|
|
info[taskId] = newPriority;
|
|
heap.push(std::make_pair(newPriority, taskId));
|
|
}
|
|
|
|
void rmv(int taskId) {
|
|
info.erase(taskId);
|
|
}
|
|
|
|
int execTop() {
|
|
while (!heap.empty()) {
|
|
auto task = heap.top();
|
|
heap.pop();
|
|
int priority = task.first, taskId = task.second;
|
|
|
|
if (info.find(taskId) != info.end() && info[taskId] == priority) {
|
|
rmv(taskId);
|
|
return taskId;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
}; |