-
Notifications
You must be signed in to change notification settings - Fork 0
/
q1.cpp
61 lines (52 loc) · 1.15 KB
/
q1.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
//2 Greedy Algorithms
//1. Implement the job sequencing with deadlines problem using the fixed tuple size formulation.
#include<iostream>
#include<algorithm>
using namespace std;
struct Job
{
char id[3];
int deadline;
int profit;
};
bool comparison(Job a, Job b)
{
return (a.profit > b.profit);
}
void printJobScheduling(Job arr[], int n)
{
int sumofProfits = 0;
sort(arr, arr+n, comparison);
int result[n];
bool slot[n];
for (int i=0; i<n; i++)
slot[i] = false;
for (int i=0; i<n; i++)
{
for (int j=min(n, arr[i].deadline)-1; j>=0; j--)
{
if (slot[j]==false)
{
result[j] = i;
slot[j] = true;
break;
}
}
}
for (int i=0; i<n; i++) {
if (slot[i]) {
cout<<arr[result[i]].id << " ";
sumofProfits = sumofProfits + arr[result[i]].profit;
}
}
cout<<"\nTotal Profit: "<<sumofProfits<<"\n";
}
int main()
{
Job arr[] = { {"J1", 7, 15}, {"J2", 2, 20}, {"J3", 5, 30}, {"J4", 3, 18}, {"J5", 4, 18}, {"J6", 5, 10}, {"J7", 2, 23}, {"J8", 7, 16}, {"J9", 3, 25} };
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Profit sequence of jobs: ";
printJobScheduling(arr, n);
cout<<endl;
return 0;
}