-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob_sequencing.cpp
More file actions
70 lines (60 loc) · 1.4 KB
/
job_sequencing.cpp
File metadata and controls
70 lines (60 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
struct Job
{
string id;
int deadline;
int profit;
};
bool compare(Job a, Job b)
{
return a.profit > b.profit;
}
void job_sequencing(vector<Job> &jobs)
{
// sort the jobs based on the profit
sort(jobs.begin(), jobs.end(), compare);
int maxDeadline = 0;
for (auto job : jobs)
{
if (maxDeadline < job.deadline)
{
maxDeadline = job.deadline;
}
}
// array to keep track of jobs
vector<int> result(maxDeadline, -1);
// array to keep track of slot
vector<bool> slot(maxDeadline, false);
int profit = 0;
// iterate through all the jobs
for (int i = 0; i < jobs.size(); i++)
{
// finding the slot for the job
for (int j = min(maxDeadline, jobs[i].deadline) - 1; j >= 0; j--)
{
if (!slot[j])
{
result[j] = i;
slot[j] = true;
profit += jobs[i].profit;
break;
}
}
}
cout << "Selected Jobs: ";
for (int i = 0; i < maxDeadline; i++)
{
if (slot[i])
{
cout << jobs[result[i]].id << " ";
}
}
cout << "\nTotal Profit: " << profit << endl;
}
int main()
{
vector<Job> jobs = {{"J1", 2, 100}, {"J2", 1, 19}, {"J3", 2, 27}, {"J4", 1, 25}, {"J5", 3, 15}};
job_sequencing(jobs);
return 0;
}