这个题是比较经典的服务器,任务题。做这种题千万不能着急做,一定题目读明白,像此题就有一个并行的问题,如果有多台空闲服务器和多个任务,要考虑同时做。这种题目的数据结构也要好好的谨慎设计。
class Solution {
typedef pair<int, int> P;
public:
vector<int> assignTasks(vector<int>& servers, vector<int>& tasks) {
map<int, vector<int>> server_release;
priority_queue<P> active_servers;
for(int i=0;i<servers.size();i++)
active_servers.push(P(-servers[i], -i));
int ct_task = 0, time = 0;
vector<int> ans;
while(ct_task < tasks.size())
{
if(active_servers.size() == 0 || server_release.count(time))
{
time = server_release.begin()->first;
for(auto v: server_release[time])
{
active_servers.push(P(-servers[v], -v));
}
server_release.erase(time);
}
while(active_servers.size() > 0 && time >= ct_task)
{
auto s = active_servers.top();
active_servers.pop();
ans.push_back(-s.second);
server_release[time+tasks[ct_task]].push_back(-s.second);
ct_task++;
if(ct_task >= tasks.size())break;
}
time++;
}
return ans;
}
};
|