forked from Google-Developer-Student-Club-CCOEW/Competitive-Programming-2023-GDSC-CUMMINS-X-GDSC-MMCOE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max number of tasks you can assign.cpp
79 lines (63 loc) Β· 2.22 KB
/
max number of tasks you can assign.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
class Solution {
public:
int maxTaskAssign(vector<int>& tasks, vector<int>& workers, int p, int strength) {
int n = tasks.size(), m = workers.size();
// Sorting the tasks and workers in increasing order
sort(tasks.begin(), tasks.end());
sort(workers.begin(), workers.end());
int lo = 0, hi = min(m, n);
int ans;
while(lo <= hi) {
int mid = lo + (hi - lo) / 2;
int count = 0;
bool flag = true;
// Inserting all workers in a multiset
multiset<int> st(workers.begin(), workers.end());
// Checking if the mid smallest tasks can be assigned
for(int i = mid - 1; i >= 0; i--) {
// Case 1: Trying to assing to a worker without the pill
auto it = prev(st.end());
if(tasks[i] <= *it) {
// Case 1 satisfied!
st.erase(it);
} else {
// Case 2: Trying to assign to a worker with the pill
auto it = st.lower_bound(tasks[i] - strength);
if(it != st.end()) {
// Case 2 satisfied!
count++;
st.erase(it);
} else {
// Case 3: Impossible to assign mid tasks
flag = false;
break;
}
}
// If at any moment, the number of pills require for mid tasks exceeds
// the allotted number of pills, we stop the loop
if(count > p) {
flag = false;
break;
}
}
if(flag) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
};
//Input
tasks =
[3,2,1]
workers =
[0,3,3]
pills =
1
strength =
1
//Output
3