-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjob scheduling2.cpp
56 lines (49 loc) · 973 Bytes
/
job scheduling2.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
#include<iostream>
#include<vector>
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
struct job{
public:
int jn;
int d;
int p;
bool comparat(const job& lhs, const job& rhs);
};
bool comparat(const job& lhs, const job& rhs) {
return lhs.p > rhs.p;
}
int main(){
int n;
cout<<"enter the number of jobs: ";
cin>>n;
int profit=0;
vector<job> j;
for(int i=0;i<n;i++){
job b;
cout<<"enter the deadline and profit (space-separated) of the "<<i+1<<"th job: ";
cin>>b.d>>b.p;
b.jn=i+1;
j.push_back(b);
}
sort(j.begin(), j.end(), &comparat);
int g[(j[0].d)+1]={0};
for(int i=0;i<n;i++)
{
for(int k=((j[i].d)-1);k>=0;k--){
if(g[k]==0)
{
g[k]=j[i].jn;
profit+=j[i].p;
i++;
exit;
}
}
}
cout<<"Jobs scheduled: \n";
for(int i=0;i<(j[0].d)+1;i++){
cout<<g[i]<<"th ";
}
cout<<"\nMaximum Profit with this Job Schedule: "<<profit;
return 0;
}