-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1606.找到处理最多请求的服务器.cpp
48 lines (42 loc) · 1.27 KB
/
1606.找到处理最多请求的服务器.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
/*
* @lc app=leetcode.cn id=1606 lang=cpp
*
* [1606] 找到处理最多请求的服务器
*/
// @lc code=start
class Solution {
public:
struct node{
int finish;
int id;
bool operator > (const node& other) const {
if(finish == other.finish)return id > other.id;
return finish > other.finish;
}
};
vector<int> busiestServers(int k, vector<int>& arrival, vector<int>& load) {
vector<int> cnt(k);
priority_queue<node,vector<node>,greater<>> working;
set<int> waiting;
vector<int> ans;
int m = 0;
for(int i = 0;i < k;i++)waiting.insert(i);
for(int i = 0;i < arrival.size();i++){
while(working.size() && working.top().finish <= arrival[i]){
waiting.insert(working.top().id);
working.pop();
}
auto cur = waiting.lower_bound(i % k);
if(cur == waiting.end())cur = waiting.begin();
if(cur == waiting.end())continue;
working.push({arrival[i] + load[i],*cur});
m = max(m,++cnt[*cur]);
waiting.erase(cur);
}
for(int i = 0;i < k;i++){
if(cnt[i] == m)ans.push_back(i);
}
return ans;
}
};
// @lc code=end