forked from dishanp/Algorithms-For-Software-Developers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGreedy_SOLN_jobSequencing.cpp
74 lines (69 loc) · 1.67 KB
/
Greedy_SOLN_jobSequencing.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
#include <algorithm>
#include <bits/stdc++.h>
#define ll long long
#define pb(x) push_back(x);
#define pp() pop_back()
#define vi vector<int>
#define v(x) vector<x>
#define pii pair<int, int>
#define pqq priority_queue
#define g(x) greater<x>
#define all(arr) arr.begin(), arr.end()
#define loop(i, n) for (int i = 0; i < n; i++)
#define MOD ll(1e9 + 7)
#define tests(t) \
int t; \
cin >> t; \
while (t--)
using namespace std;
void Solve(vector<pii> &jobs, int n, int &noOfJobs, int &maxProfit)
{
sort(all(jobs), greater<pii>()); //sorted based on max profit -> min profit.
vector<int> slots(n, -1);
int res = 0;
loop(i, n)
{
pii temp = jobs[i];
int x = 0, deadline = temp.second;
while (x < deadline)
{
if (slots[x] == -1)
{
res += temp.first;
slots[x] = 1; //job scheduled.
break;
}
x++;
}
maxProfit = (maxProfit < res) ? res : maxProfit;
}
int x = -1;
while (++x < n)
{
if (slots[x] != -1)
noOfJobs++;
}
return;
}
int main()
{
// IOS();
tests(t)
{
cout << "Number of jobs (n) | <profit,deadline>\n";
int n;
cin >> n;
vector<pii> jobs;
loop(i, n)
{
int deadline, profit;
cin >> profit >> deadline;
jobs.push_back(make_pair(profit, deadline));
}
int noOfJobs = 0, maxProfit = 0;
Solve(jobs, n, noOfJobs, maxProfit);
cout << "No of Jobs Performed : " << noOfJobs << endl
<< "Max Profit : " << maxProfit << endl;
}
return 0;
}