-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_2071_maxTaskAssign.cc
96 lines (86 loc) · 1.86 KB
/
Problem_2071_maxTaskAssign.cc
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
#include <algorithm>
#include <cstdint>
#include <deque>
#include <iostream>
#include <map>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// TODO: figure it out
int maxTaskAssign(vector<int> &tasks, vector<int> &workers, int pills, int strength)
{
int n = tasks.size(), m = workers.size();
sort(tasks.begin(), tasks.end());
sort(workers.begin(), workers.end());
auto check = [&](int mid) -> bool {
int p = pills;
deque<int> ws;
int ptr = m - 1;
// 从大到小枚举每一个任务
for (int i = mid - 1; i >= 0; --i)
{
while (ptr >= m - mid && workers[ptr] + strength >= tasks[i])
{
ws.push_front(workers[ptr]);
--ptr;
}
if (ws.empty())
{
return false;
}
// 如果双端队列中最大的元素大于等于 tasks[i]
else if (ws.back() >= tasks[i])
{
ws.pop_back();
}
else
{
if (!p)
{
return false;
}
--p;
ws.pop_front();
}
}
return true;
};
int left = 1, right = min(m, n), ans = 0;
while (left <= right)
{
int mid = (left + right) / 2;
if (check(mid))
{
ans = mid;
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return ans;
}
};
void testMaxTaskAssign()
{
Solution s;
vector<int> t1 = {3, 2, 1};
vector<int> w1 = {0, 3, 3};
vector<int> t2 = {5, 4};
vector<int> w2 = {0, 0, 0};
vector<int> t3 = {10, 15, 30};
vector<int> w3 = {0, 10, 10, 10, 10};
EXPECT_EQ_INT(3, s.maxTaskAssign(t1, w1, 1, 1));
EXPECT_EQ_INT(1, s.maxTaskAssign(t2, w2, 1, 5));
EXPECT_EQ_INT(2, s.maxTaskAssign(t3, w3, 3, 10));
EXPECT_SUMMARY;
}
int main()
{
testMaxTaskAssign();
return 0;
}