forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjob_sheduling_with_deadlines.cpp
81 lines (76 loc) · 1.97 KB
/
job_sheduling_with_deadlines.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
78
79
80
81
/*
Given:
1. 'n' jobs.
2. an array containing respective deadlines.
3. an array containing respective profits.
4. all jobs takes unit time to complete.
5. Initially all the jobs are at t=0;
Assumption:
profit array is in descending order.
Problem statement:
select the subset of given n-jobs, such that the jobs which are selected must be completed
within their deadlines and get maximum profit.
Objective:
to get max profit and sheduled jobs.
*/
#include<iostream>
#include<algorithm>
using namespace std;
void JSD_algo(int[],int[],int[],int);
int main()
{
int n,k;
cout<<"enter the no. of jobs:"<<endl;
cin>>n;
int jobs[n],profit[n],deadlines[n];
cout<<"enter the profit in decreasing order:"<<endl;
for(k=0;k<n;k++)
cin>>profit[k];
cout<<"enter the respective deadline:"<<endl;
for(k=0;k<n;k++)
cin>>deadlines[k];
cout<<"enter the respective job serial no.:"<<endl;
for(k=0;k<n;k++)
cin>>jobs[k];
JSD_algo(jobs,profit,deadlines,n);
}
void JSD_algo(int jobs[],int p[],int d[],int n)
{
int profit=0,i,max_dl,j;
max_dl=*max_element(d,d+n); /* finding max deadline*/
int sub_job[max_dl]={0}; /* no. of maximum jobs can be sheduled will be equal to max deadline */
for(i=0;i<n;i++)
{
for(j=d[i]-1;j>=0;j--)
{
if(sub_job[j]==0)
{
profit=profit+p[i];
sub_job[j]=jobs[i];
break;
}
}
}
cout<<"sheduled jobs:\n";
for(int k=0;k<max_dl;k++)
cout<<sub_job[k]<<" ";
cout<<"\nmax profit:"<<profit;
}
/*
Time complexity:O(n^2) becase we are using loop inside a loop .(nxn)
space complexity:O(1)
--------------------------------------------------------------------------------------------------------
input:
enter the no. of jobs:
6
enter the profit in decreasing order:
100 85 74 65 60 43
enter the respective deadline:
4 1 3 1 2 2
enter the respective job serial no.:
6 5 2 3 1 4
output:
sheduled jobs:
5 1 2 6
max profit:319
*/