📌  相关文章
📜  在有限的时间内安排优先任务并最大程度地减少损失

📅  最后修改于: 2021-04-23 20:33:02             🧑  作者: Mango

给定n个任务的到达时间,优先级和所需的时间单位数。我们需要在单个资源上安排这些任务。目的是安排任务,以便采取最大优先级的任务。目的是使优先级乘积与由于时间限制而未计划的任务剩余时间的总和最小化。该标准仅表示调度应将可能的损失降到最低。

例子:

Input : Total time = 3
        Task1: arrival = 1, units = 2, priority = 300
        Task2: arrival = 2, units = 2, priority = 100
Output : 100
Explanation : Two tasks are given and time to finish 
them is 3 units. First task arrives at time 1 and it
needs 2 units. Since no other task is running at that
time, we take it. 1 unit of task 1 is over. Now task 2 
arrives. The priority of task 1 is more than task 2 
(300 > 100) thus 1 more unit  of task 1 is employed. 
Now 1 unit of time is left and we have 2 units of task
2 to be done. So simply 1 unit of task 2 is done and 
the answer is ( units of task 2 left X priority of 
task 2 ) = 1 X 100 = 100

Input : Total Time = 3
        Task1: arrival = 1, units = 1, priority = 100
        Task2: arrival = 2, units = 2, priority = 300
Output : 0

我们使用优先级队列并一次安排一个任务单元。

  1. 将总损失初始化为每个优先级和单位的总和。这个想法是将结果初始化为最大值,然后一个接一个地减去计划任务的优先级。
  2. 根据到达时间对所有任务进行排序。
  3. 处理从1到总时间的每个时间单位。对于当前时间,选择可用的最高优先级任务。安排此任务的1个单位,并从总损失中减去其优先级。

以下是上述步骤的C++实现。

// C++ code for task scheduling on basis
// of priority and arrival time
#include "bits/stdc++.h"
using namespace std;
  
// t is total time. n is number of tasks.
// arriv[] stores arrival times. units[]
// stores units of time required. prior[]
// stores priorities.
int minLoss(int n, int t, int arriv[],
            int units[],  int prior[])
{
    // Calculating maximum possible answer
    // that could be calculated. Later we
    // will subtract the tasks from it
    // accordingly.
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans += prior[i] * units[i];
  
    // Pushing tasks in a vector so that they
    // could be sorted according with their
    // arrival time
    vector > v;
    for (int i = 0; i < n; i++)
        v.push_back({ arriv[i], i });
  
    // Sorting tasks in vector identified by
    // index and arrival time with respect
    // to their arrival time
    sort(v.begin(), v.end());
  
    // Priority queue to hold tasks to be
    // scheduled.
    priority_queue > pq;
  
    // Consider one unit of time at a time.
    int ptr = 0; // index in sorted vector
    for (int i = 1; i <= t; i++) {
  
        // Push all tasks that have arrived at
        // this time. Note that the tasks that
        // arrived earlier and could not be scheduled
        // are already in pq.
        while (ptr < n and v[ptr].x == i) {
            pq.push({ prior[v[ptr].y], units[v[ptr].y] });
            ptr++;
        }
  
        // Remove top priority task to be scheduled
        // at time i.
        if (!pq.empty()) {
            auto tmp = pq.top();
            pq.pop();
  
            // Removing 1 unit of task
            // from answer as we could
            // schedule it.
            ans -= tmp.x;
            tmp.y--;
            if (tmp.y)
                pq.push(tmp);
        }
    }
  
    return ans;
}
  
// driver code
int main()
{
    int n = 2, t = 3;
    int arriv[] = { 1, 2 };
    int units[] = { 2, 2 };
    int prior[] = { 100, 300 };
  
    printf("%d\n",  minLoss(n, t, arriv, units, prior));
    return 0;
}

输出:

100