-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathJob Sequencing.cpp
77 lines (64 loc) · 1.44 KB
/
Job Sequencing.cpp
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
76
77
#include<bits/stdc++.h>
using namespace std;
struct JOB{
char job;
int dline;
int profit;
};
bool comparision(JOB a,JOB b){
return (a.profit>b.profit);
}
void job_Sequencing(struct JOB a[],int n){
sort(a,a+n,comparision); //max to min Profit
//Finding max deadline => MAX Size of output Array
int max_deadline=0;
for(int i=0;i<n;i++){
if(a[i].dline>max_deadline){
max_deadline=a[i].dline;
}
}
//cout<<max_deadline;
char output[max_deadline+1];
for(int i=1;i<=max_deadline;i++){
output[i]='?';
}
for(int i=0;i<n;i++){
if(output[a[i].dline]=='?'){
output[a[i].dline]=a[i].job;
}
else{
int x=a[i].dline-1; //finding vacant space
while(output[x]!='?'){
--x;
}
if(x>=1){
output[x]=a[i].job;
}
}
}
for(int i=1;i<=max_deadline;i++){
if(output[i]!='?'){
cout<<output[i]<<' ';
}
}
}
int main(){
int n;
cout<<"Enter Number of Jobs: ";
cin>>n;
struct JOB a[n];
cout<<"Enter Job names: ";
for(int i=0;i<n;i++){
cin>>a[i].job;
}
cout<<"Enter Deadline For Each Job: ";
for(int i=0;i<n;i++){
cin>>a[i].dline;
}
cout<<"Enter Profit For Each Job: ";
for(int i=0;i<n;i++){
cin>>a[i].profit;
}
job_Sequencing(a,n);
return 0;
}