📜  服务器负载的最小绝对差异

📅  最后修改于: 2021-09-17 07:31:59             🧑  作者: Mango

有一些进程需要执行。该进程导致运行它的服务器的负载量由单个整数表示。服务器上造成的总负载是该服务器上运行的所有进程的负载总和。您可以使用两台服务器,上面提到的进程可以在这些服务器上运行。您的目标是以最小化负载的绝对差异的方式在这两个服务器之间分配给定的进程。
给定一个由N 个整数组成的A[]数组,它表示由连续进程引起的负载,任务是打印服务器负载的最小绝对差异。
例子:

朴素的方法:解决问题的最简单的方法是生成负载分布的所有可能性,并在两个服务器的所有可能的负载组合之间找到可能的最小差异。

时间复杂度: O(2 N )
辅助空间: O(1)

高效的方法:该问题可以看作是 0/1 背包问题的变体,其中给定了 2 个服务器,我们必须尽可能平均地分配负载。因此,可以使用动态规划来解决。以下是步骤:

  • 计算required_load ,它等于(sum of all load / 2) ,因为负载需要尽可能均匀地分布。
  • 创建记忆表DP[][]以考虑[1, required_load]范围内所有可能的服务器负载。
  • 状态DP[i][j]存储j的最大值考虑到第 i元素的负载。因此,考虑到l i (在i 行加载),它可以填充在所有具有负载值 > l i的列中。
  • 现在出现两种可能性,要么在给定的列中填充l i
  • 现在,取上述两种可能性中的最大值,即
  • 最后, DP[n][required_load]将包含尽可能平衡的 server1 上的负载。

下面是上述方法的实现:

C++14
// C++14 program to implement
// the above approach
#include 
using namespace std;
 
// Function which returns the minimum
// difference of loads
int minServerLoads(int n, vector& servers)
{
     
    // Compute the overall server load
    int totalLoad = 0;
    for(int i : servers) totalLoad += i;
 
    int requiredLoad = totalLoad / 2;
 
    // Stores the results of subproblems
    vector> dp(n + 1,
    vector(requiredLoad + 1, 0));
 
    // Fill the partition table
    // in bottom up manner
    for(int i = 1; i < n + 1; i++)
    {
        for(int j = 1; j < requiredLoad + 1; j++)
        {
             
            // If i-th server is included
            if (servers[i - 1] > j)
                dp[i][j] = dp[i - 1][j];
 
            // If i-th server is excluded
            else
                dp[i][j] = max(dp[i - 1][j],
                          servers[i - 1] +
                        dp[i - 1][j - servers[i - 1]]);
        }
    }
 
    // Server A load: total_sum-ans
    // Server B load: ans
    // Diff: abs(total_sum-2 * ans)
    return totalLoad - 2 * dp[n][requiredLoad];
}
 
// Driver Code
int main()
{
    int N = 5;
     
    vector servers = { 1, 2, 3, 4, 5 };
     
    // Function call
    cout << (minServerLoads(N, servers));
}
 
// This code is contributed by mohit kumar 29


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG {
 
    // Function which returns the minimum
    // difference of loads
    static int minServerLoads(int n, int[] servers)
    {
        // Compute the overall server load
        int totalLoad = 0;
        for (int i = 0; i < servers.length; i++)
            totalLoad += servers[i];
        int requiredLoad = totalLoad / 2;
 
        // Stores the results of subproblems
        int dp[][] = new int[n + 1][requiredLoad + 1];
 
        // Fill the partition table
        // in bottom up manner
        for (int i = 1; i < n + 1; i++)
        {
            for (int j = 1; j < requiredLoad + 1; j++)
            {
                // If i-th server is included
                if (servers[i - 1] > j)
                    dp[i][j] = dp[i - 1][j];
 
                // If i-th server is excluded
                else
                    dp[i][j] = Math.max(dp[i - 1][j],
                                        servers[i - 1] +
                                        dp[i - 1][j - servers[i - 1]]);
            }
        }
 
        // Server A load: total_sum-ans
        // Server B load: ans
        // Diff: abs(total_sum-2 * ans)
        return totalLoad - 2 * dp[n][requiredLoad];
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 5;
        int servers[] = {1, 2, 3, 4, 5};
 
        // Function call
        System.out.print(minServerLoads(N, servers));
    }
}
 
// This code is contributed by Chitranayal


Python3
# Python3 program for the above approach
 
# Function which returns the minimum
# difference of loads
def minServerLoads(n, servers):
 
    # Compute the overall server load
    totalLoad = sum(servers)
 
    requiredLoad = totalLoad // 2
 
    # Stores the results of subproblems
    dp = [[0 for col in range(requiredLoad + 1)]
          for row in range(n + 1)]
 
    # Fill the partition table
    # in bottom up manner
    for i in range(1, n + 1):
        for j in range(1, requiredLoad + 1):
 
            # If i-th server is included
            if servers[i-1] > j:
                dp[i][j] = dp[i-1][j]
 
            # If i-th server is excluded
            else:
                dp[i][j] = max(dp[i-1][j],
                           servers[i-1] +
                           dp[i-1][j-servers[i-1]])
 
    # Server A load: total_sum-ans
    # Server B load: ans
    # Diff: abs(total_sum-2 * ans)
    return totalLoad - 2 * dp[n][requiredLoad]
 
# Driver Code
 
N = 5;
 
servers = [1, 2, 3, 4, 5]
 
# Function Call
print(minServerLoads(N, servers))


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function which returns the minimum
// difference of loads
static int minServerLoads(int n, int[] servers)
{
     
    // Compute the overall server load
    int totalLoad = 0;
    for(int i = 0; i < servers.Length; i++)
        totalLoad += servers[i];
         
    int requiredLoad = totalLoad / 2;
 
    // Stores the results of subproblems
    int [,]dp = new int[n + 1, requiredLoad + 1];
 
    // Fill the partition table
    // in bottom up manner
    for(int i = 1; i < n + 1; i++)
    {
        for(int j = 1; j < requiredLoad + 1; j++)
        {
             
            // If i-th server is included
            if (servers[i - 1] > j)
                dp[i, j] = dp[i - 1, j];
 
            // If i-th server is excluded
            else
                dp[i, j] = Math.Max(dp[i - 1, j],
                               servers[i - 1] +
                                    dp[i - 1, j -
                               servers[i - 1]]);
        }
    }
 
    // Server A load: total_sum-ans
    // Server B load: ans
    // Diff: abs(total_sum-2 * ans)
    return totalLoad - 2 * dp[n, requiredLoad];
}
 
// Driver Code
public static void Main(string[] args)
{
    int N = 5;
    int []servers = { 1, 2, 3, 4, 5 };
 
    // Function call
    Console.Write(minServerLoads(N, servers));
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
1

时间复杂度: O(N*S),其中 N 是服务器的数量,S 是所有服务器负载总和
辅助空间: O(N*S)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程