Skip to content

Commit b4c025d

Browse files
authored
Create 1882.Process-Tasks-Using-Servers.cpp
1 parent b664309 commit b4c025d

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
typedef array<int,2> AI2;
2+
typedef array<int,3> AI3;
3+
4+
class Solution {
5+
public:
6+
vector<int> assignTasks(vector<int>& servers, vector<int>& tasks)
7+
{
8+
priority_queue<AI2, vector<AI2>, greater<>>pq1;
9+
priority_queue<AI3, vector<AI3>, greater<>>pq2;
10+
for (int i=0; i<servers.size(); i++)
11+
pq1.push({servers[i], i});
12+
13+
14+
queue<int>jobs;
15+
vector<int>rets(tasks.size(),-1);
16+
17+
for (int j=0; j<tasks.size(); j++)
18+
{
19+
jobs.push(j);
20+
while (!pq2.empty() && pq2.top()[0] <= j)
21+
{
22+
auto [t,w,idx] = pq2.top();
23+
pq2.pop();
24+
pq1.push({w,idx});
25+
}
26+
27+
while (!jobs.empty() && !pq1.empty())
28+
{
29+
int job = jobs.front();
30+
jobs.pop();
31+
auto [w, idx] = pq1.top();
32+
pq1.pop();
33+
pq2.push({j+tasks[job], w, idx});
34+
rets[job] = idx;
35+
}
36+
}
37+
38+
while (!jobs.empty())
39+
{
40+
int job = jobs.front();
41+
jobs.pop();
42+
auto [t, w, idx] = pq2.top();
43+
pq2.pop();
44+
pq2.push({t+tasks[job], w, idx});
45+
rets[job] = idx;
46+
}
47+
48+
return rets;
49+
}
50+
};

0 commit comments

Comments
 (0)