给定一个作业数组,其中每个作业都有一个截止期限,如果作业在截止期限之前完成,则可获得相关利润。此外,每个作业都占用一个单位时间,因此任何作业的最小可能截止期限都是 1。如果一次只能安排一项作业,则最大化总利润。
例子:
输入:四个工作,截止日期和利润如下
JobID 截止期限 利润
一 4 20
二 1 10
三 1 40
四 1 30
输出:以下是工作利润最大的序列:c、a
输入: 五项工作,截止日期和利润如下
JobID 截止期限 利润
a 2 100
b 1 19
c 2 27
d 1 25
e 3 15
输出:以下是工作利润最大的序列:c,a,e
朴素方法:要解决问题,请遵循以下想法:
生成给定作业集的所有子集,并检查各个子集是否可行。跟踪所有可行子集中的最大利润。
作业排序问题的贪婪方法:
贪婪地首先选择利润最高的工作,方法是按利润降序对工作进行排序。这将有助于最大化总利润,因为为每个时间段选择利润最高的工作最终将最大化总利润
按照给定的步骤解决问题:
按利润的降序对所有工作进行排序。
按利润递减的顺序对工作进行迭代。对于每项工作,执行以下操作:
找到一个时间段 i,使得时间段为空、i < 截止时间且 i 最大。将作业放入
此时间段并将此时间段标记为已填充。
如果不存在这样的 i,则忽略该工作。
下面是上述方法的实现:
// C++ code for the above approach
#include <algorithm>
#include <iostream>
using namespace std;
// A structure to represent a job
struct Job {
char id; // Job Id
int dead; // Deadline of job
int profit; // Profit if job is over before or on
// deadline
};
// Comparator function for sorting jobs
bool comparison(Job a, Job b)
{
return (a.profit > b.profit);
}
// Returns maximum profit from jobs
void printJobScheduling(Job arr[], int n)
{
// Sort all jobs according to decreasing order of profit
sort(arr, arr + n, comparison);
int result[n]; // To store result (Sequence of jobs)
bool slot[n]; // To keep track of free time slots
// Initialize all slots to be free
for (int i = 0; i < n; i++)
slot[i] = false;
// Iterate through all given jobs
for (int i = 0; i < n; i++) {
// Find a free slot for this job (Note that we start
// from the last possible slot)
for (int j = min(n, arr[i].dead) - 1; j >= 0; j--) {
// Free slot found
if (slot[j] == false) {
result[j] = i; // Add this job to result
slot[j] = true; // Make this slot occupied
break;
}
}
}
// Print the result
for (int i = 0; i < n; i++)
if (slot[i])
cout << arr[result[i]].id << " ";
}
// Driver's code
int main()
{
Job arr[] = { { 'a', 2, 100 },
{ 'b', 1, 19 },
{ 'c', 2, 27 },
{ 'd', 1, 25 },
{ 'e', 3, 15 } };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Following is maximum profit sequence of jobs "
"\n";
// Function call
printJobScheduling(arr, n);
return 0;
}
// This code is contributed by Aditya Kumar (adityakumar129)
输出
以下是工作的最大利润序列
c a e
计算机辅助设计
时间复杂度: O(N 2 )
辅助空间: O(N)
使用优先级队列(最大堆)的作业排序问题:
按截止日期的升序对作业进行排序,然后从末尾开始迭代,计算每两个连续截止日期之间的可用时隙。当空时隙可用且堆不为空时,将作业的利润包含在最大堆的根部,因为这有助于为每组可用时隙选择利润最大的作业。
下面是上述方法的实现:
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// A structure to represent a job
struct Job {
char id; // Job Id
int dead; // Deadline of job
int profit; // Profit earned if job is completed before
// deadline
};
// Custom sorting helper struct which is used for sorting
// all jobs according to profit
struct jobProfit {
bool operator()(Job const& a, Job const& b)
{
return (a.profit < b.profit);
}
};
// Returns maximum profit from jobs
void printJobScheduling(Job arr[], int n)
{
vector<Job> result;
sort(arr, arr + n,
[](Job a, Job b) { return a.dead < b.dead; });
// set a custom priority queue
priority_queue<Job, vector<Job>, jobProfit> pq;
for (int i = n - 1; i >= 0; i--) {
int slot_available;
// we count the slots available between two jobs
if (i == 0) {
slot_available = arr[i].dead;
}
else {
slot_available = arr[i].dead - arr[i - 1].dead;
}
// include the profit of job(as priority),
// deadline and job_id in maxHeap
pq.push(arr[i]);
while (slot_available > 0 && pq.size() > 0) {
// get the job with the most profit
Job job = pq.top();
pq.pop();
// reduce the slots
slot_available--;
// add it to the answer
result.push_back(job);
}
}
// sort the result based on the deadline
sort(result.begin(), result.end(),
[&](Job a, Job b) { return a.dead < b.dead; });
// print the result
for (int i = 0; i < result.size(); i++)
cout << result[i].id << ' ';
cout << endl;
}
// Driver's code
int main()
{
Job arr[] = { { 'a', 2, 100 },
{ 'b', 1, 19 },
{ 'c', 2, 27 },
{ 'd', 1, 25 },
{ 'e', 3, 15 } };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Following is maximum profit sequence of jobs "
"\n";
// Function call
printJobScheduling(arr, n);
return 0;
}
// This code is contributed By Reetu Raj Dubey
输出
以下是作业的最大利润序列
a c e
时间复杂度: O(N log N)
辅助空间: O(N)