Paste: 111
Author: | 111 |
Mode: | c++ |
Date: | Sat, 15 Oct 2022 03:09:35 |
Plain Text |
#include<bits/stdc++.h>
using namespace std;
int main() {
int num_server, limit, cooldown;
vector<int> handle(num_server, 0), recover(num_server, 0);
vector<string> events;
int lastFind = 0, currTime = 0;
for (string env : events) {
if (env.find("REQUEST") != env.npos) {
// request
int j = lastFind;
for (j = lastFind; j < recover.size(); j++) {
if (recover[j] < currTime) {
handle[j]++;
if (handle[j] == limit) {
recover[j] += cooldown;
}
lastFind = j;
break;
}
}
if (j == recover.size()) {
// reach the end, should start from beginning
j = 0;
for (j = 0; j < recover.size(); j++) {
if (recover[j] < currTime) {
handle[j]++;
if (handle[j] == limit) {
recover[j] += cooldown;
}
lastFind = j;
break;
}
}
}
currTime++;
} else {
// up
int index = getIndex(env); // 从字符串中解析出index 是哪一台服务器
recover[index] = currTime;
}
}
int maxHandle = 0, maxHandleIndex = -1;
for (int i = 0; i < num_server; i++) {
if (handle[i] >= maxHandle) {
maxHandle = handle[i];
maxHandleIndex = i;
}
}
return maxHandleIndex;
}
New Annotation