给定处理具有无限计算能力的多个请求的M 个服务器和大小为N 的数组到达时间[]和进程时间 []以下列方式表示N 个请求的到达时间和加载时间:
- 每个服务器从0到(M – 1)编号,并且请求按照严格的时间递增顺序给出。
- 每个请求i都通过以下方式分配给其中一台服务器:
- 选择第(i % m)个服务器。如果所选服务器空闲,则将请求分配给该服务器。
- 否则,选择下一个可用的服务器。如果没有可用的服务器,则该请求将被丢弃。
考虑到每个服务器一次只能处理一个请求,任务是在处理所有传入请求后找到每个服务器上的负载,因为每个服务器上的负载是它处理的请求数。
例子:
Input: N = 4, M = 3, arrivalTime[] = {1, 3, 6, 8}, processTime[] = {1, 2, 2, 1}
Output:
1st Server -> 2
2nd Server -> 1
3rd Server -> 1
Explanation:
The first and fourth requests are assigned to the first server.
The second request is assigned to the second server and the third request is assigned to the third server.
Below is the transition table:
Request Number | Arrival Time | Load Time | End Time | Available Servers | Demanded Server | Assigned Server |
0 | 1 | 1 | 2 | 0, 1, 2 | 0 | 0 |
1 | 3 | 2 | 5 | 0, 1, 2 | 1 | 1 |
2 | 6 | 2 | 8 | 0, 1, 2 | 2 | 2 |
3 | 8 | 1 | 9 | 0, 1, 2 | 1 | 1 |
Input: N = 4, M = 2, arrivalTime = {1, 2, 4, 6}, processTime = {7, 1, 4, 4}
Output:
1st Server -> 1
2nd Server -> 2
Explanation:
The first request is assigned to the first server and second request to the second server.
The third request is assigned to the second server. The demanded server for the third request is the first server but since, it is busy at the arrival time of the request,
So, the second server is assigned to it.
The fourth request is dropped as both servers are busy at the time of its arrival.
Below is the transition table:
Request Number | Arrival Time | Load Time | End Time | Available Servers | Demanded Server | Assigned Server |
0 | 1 | 7 | 8 | 0, 1 | 0 | 0 |
1 | 2 | 1 | 3 | 1 | 1 | 1 |
2 | 4 | 4 | 8 | 1 | 0 | 1 |
3 | 6 | 4 | 10 | – | 1 | – |
方法:这个想法是使用一个最小优先级队列和一个集合。优先队列保持繁忙服务器的数量,并在它们空闲时帮助释放它们。 Set 用于维护可用服务器的数据,以便将它们分配给传入的请求。以下是步骤:
- 初始化一个辅助数组loadOnServer[] ,它将在每个服务器上存储负载。
- 迭代传入的请求并通过在每个请求中添加到达时间和处理时间来找到每个请求的结束时间。
- 从结束时间超过当前结束时间的优先级队列中弹出繁忙的服务器。
- 如果可用服务器集为空,则删除当前请求。
- 现在,使用下界函数搜索集合中的第(i % m)个服务器,如果下界迭代器指向集合的末尾,则选择集合中的第一个服务器。
- 完成上述步骤后,增加所选服务器上的负载计数器。
- 完成上述步骤后,在loadOnServer[] 中打印所有负载的存储。
下面是上述方法的实现:
C++
// C++ Program for the above approach
#include
using namespace std;
// Function to print load on each server
void printLoadOnEachServer(
int m, int loadOnServer[])
{
// Traverse the loadOnServer and
// print each loads
for (int i = 0; i < m; i++) {
cout << i + 1 << "st Server -> "
<< loadOnServer[i] << ".\n";
}
}
// Function for finding the load
// on each server
void loadBalancing(int n, int m,
int arrivalTime[],
int processTime[])
{
// Stores the load on each Server
int loadOnServer[m];
for (int i = 0; i < m; i++) {
// Initialize load on each
// server as zero
loadOnServer[i] = 0;
}
// Minimum priority queue for
// storing busy servers according
// to their release time
priority_queue,
vector >,
greater > >
busyServers;
// Set to store available Servers
set availableServers;
for (int i = 0; i < m; i++) {
// Initially, all servers are free
availableServers.insert(i);
}
// Iterating through the requests.
for (int i = 0; i < n; i++) {
// End time of current request
// is the sum of arrival time
// and process time
int endTime = arrivalTime[i]
+ processTime[i];
// Releasing all the servers which
// have become free by this time
while (!busyServers.empty()
&& busyServers.top().first
<= arrivalTime[i]) {
// Pop the server
pair releasedServer
= busyServers.top();
busyServers.pop();
// Insert available server
availableServers.insert(
releasedServer.second);
}
// If there is no free server,
// the request is dropped
if ((int)availableServers.empty()) {
continue;
}
int demandedServer = i % m;
// Searching for demanded server
auto itr
= availableServers.lower_bound(
demandedServer);
if (itr == availableServers.end()) {
// If demanded Server is not free
// and no server is free after it,
// then choose first free server
itr = availableServers.begin();
}
int assignedServer = *itr;
// Increasing load on assigned Server
loadOnServer[assignedServer]++;
// Removing assigned server from list
// of assigned servers
availableServers.erase(assignedServer);
// Add assigned server in the list of
// busy servers with its release time
busyServers.push({ endTime,
assignedServer });
}
// Function to print load on each server
printLoadOnEachServer(m, loadOnServer);
}
// Driver Code
int main()
{
// Given arrivalTime and processTime
int arrivalTime[] = { 1, 2, 4, 6 };
int processTime[] = { 7, 1, 4, 4 };
int N = sizeof(arrivalTime)
/ sizeof(int);
int M = 2;
// Function Call
loadBalancing(N, M, arrivalTime,
processTime);
return 0;
}
1st Server -> 1.
2st Server -> 2.
时间复杂度: O(N*log M)
辅助空间: O(M)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。