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

📅  最后修改于: 2021-05-17 21:21:30             🧑  作者: Mango

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

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

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

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

  • 计算required_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


输出:
1

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