Paste: 111

Author: 111
Mode: c++
Date: Sat, 15 Oct 2022 03:15:46
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), totalHandle(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]++; 
                    totalHandle[j]++; 
                    if (handle[j] == limit) {
                        recover[j] += cooldown; 
                        handle[j] = 0; 
                    }
                    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]++; 
                        totalHandle[j]++; 
                        if (handle[j] == limit) {
                            recover[j] += cooldown;
                            handle[j] = 0;  
                        }
                        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 (totalHandle[i] >= maxHandle) {
            maxHandle = totalHandle[i]; 
            maxHandleIndex = i; 
        }
    }
    return maxHandleIndex;
}

New Annotation

Summary:
Author:
Mode:
Body: