-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobSequencing.cpp
More file actions
109 lines (90 loc) · 1.54 KB
/
JobSequencing.cpp
File metadata and controls
109 lines (90 loc) · 1.54 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <bits/stdc++.h>
using namespace std;
struct job
{
char id;
int deadline;
int profit;
job(char i, int d, int p)
{
id = i;
deadline = d;
profit = p;
}
};
class JobSequencing
{
private:
vector<job> jobList;
int jobSlot;
int maxProfit = 0;
public:
int getProfit() const { return maxProfit; };
void takeInput();
vector<char> finJobSeq();
void printArray();
};
void JobSequencing::takeInput()
{
int _size;
cout << "Enter number of jobs:";
cin >> _size;
char a;
int d, p;
while (_size--)
{
cin >> a >> d >> p;
jobList.emplace_back(job(a, d, p));
}
cout << "Please input available jobSlot:";
cin >> jobSlot;
}
bool comProfit(job p, job q)
{
return p.profit > q.profit;
}
vector<char> JobSequencing::finJobSeq()
{
sort(jobList.begin(), jobList.end(), comProfit);
vector<char> finJobList;
unordered_set<int> slot;
for (int i = 0; i < jobList.size(); i++)
{
int limit = jobList[i].deadline;
while (limit != 0)
{
if (slot.count(limit) <= 0)
{
finJobList.emplace_back(jobList[i].id);
slot.insert(limit);
maxProfit += jobList[i].profit;
break;
}
else
limit--;
}
}
return finJobList;
}
void JobSequencing::printArray()
{
for (auto v : jobList)
{
cout << v.id << "->" << v.deadline << "->" << v.profit << endl;
}
}
int main()
{
JobSequencing *demo1 = new JobSequencing();
demo1->takeInput();
vector<char> rslt = demo1->finJobSeq();
for (auto i : rslt)
cout << i << "->";
cout << " profit:" << demo1->getProfit();
return 0;
}
// a 2 20
// b 2 15
// c 1 10
// d 3 5
// e 3 1