-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob_sequencing_problem.cpp
More file actions
75 lines (61 loc) · 1.46 KB
/
job_sequencing_problem.cpp
File metadata and controls
75 lines (61 loc) · 1.46 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
71
72
73
74
75
/*JOB Sequencing problem.
Suppose an array of jobs is given with their respective deadline and profits.
It is also given that every job takes single unit of time, so the minimum possible deadline for any job is 1.
Therefore to maximize the total profit if only one job can be scheduled at a time we use this algorithm.
*/
#include <bits/stdc++.h>
using namespace std;
struct job{
int id;
int p;
int d;
};
//function compares the profit of jobs.
bool comp(job a1,job a2)
{
return( a1.p>a2.p);
}
void job_seq(job j[],int n)
{
int t=j[0].d;
for(int i=1;i<n;i++)
{
if(j[i].d>t)
t=j[i].d;
}
int b[t];
//sorting the jobs descending order of profit.
sort(j,j+n, comp);
for(int i=0;i<t;i++)
b[i]=-1;
for(int i=0;i<n;i++)
{
for(int k=j[i].d-1;k>=0;k--)
{
if(b[k]==-1)
{
b[k]=j[i].id;
break;
}
}
}
cout<<"\n Sequence of the jobs :\n";
for(int i=0;i<t;i++)
cout<<b[i]<<" ";
}
int main()
{
cout<<"\n Enter the number of jobs:\n";
int n;
cin>>n;
job j[n];
cout<<"\n Enter job's deadline and profit: \n";
for(int i=0;i<n;i++)
{
cout<<i+1<<" job:";
j[i].id=i+1;
cin>>j[i].d>>j[i].p;
}
//function to implement JSP
job_seq(j,n);
return 0;