📜  调度 K 个进程所需的最短时间

📅  最后修改于: 2021-10-28 02:04:25             🧑  作者: Mango

给定一个正整数K和一个由N 个正整数组成的数组arr[] ,这样arr[i]是第i处理器可以在1 秒内调度的进程数。任务是最小化调度K 个进程所需的总时间,以便在第i处理器调度后, arr[i]减少到floor(arr[i]/2)

例子:

朴素方法:解决给定问题的最简单方法是将给定的列表按升序排序,选择能力最高的处理器,然后将K的值减少该值,然后从列表中删除该处理器并将其一半添加到再次排序列表。重复上述过程,直到至少调度了K 个进程,并打印出调度至少 K 个进程后所需的时间。

时间复杂度: O(N*log N)
辅助空间: O(N)

高效的方法:上述方法也可以通过使用Hashing的概念进行优化。请按照以下步骤解决问题:

  • 初始化给定数组中存在的最大元素大小的辅助数组tmp[]
  • 初始化一个变量,比如count来存储分别调度所有进程的最短时间。
  • 从末尾遍历给定数组tmp[]并执行以下步骤:
    • 如果tmp[] 中的当前元素大于0并且i * tmp[i]小于K
      • K的值减少i * tmp[i] 值
      • tmp[i/2]增加tmp[i],因为处理器的能力将减少一半。
      • count的值增加值tmp[i]
      • 如果K的值已经小于或等于0 ,则打印count的值作为结果。
    • 如果数组tmp[] 中的当前元素至少为0i * tmp[i] 的至少为 K ,则执行以下步骤:
      • 如果K可被当前索引整除,则将count的值增加K / i
      • 否则,将count的值增加K/i +1
  • 完成上述步骤后,如果无法调度所有进程,则打印-1 。否则,将计数打印为所需的最短时间。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum required
// time to schedule all process
int minTime(int A[], int n, int K)
{
     
    // Stores max element from A[]
    int max_ability = A[0];
 
    // Find the maximum element
    for(int i = 1; i < n; i++)
    {
        max_ability = max(max_ability, A[i]);
    }
 
    // Stores frequency of each element
    int tmp[max_ability + 1] = {0};
 
    // Stores minimum time required
    // to schedule all process
    int count = 0;
 
    // Count frequencies of elements
    for(int i = 0; i < n; i++)
    {
        tmp[A[i]]++;
    }
 
    // Find the minimum time
    for(int i = max_ability; i >= 0; i--)
    {
        if (tmp[i] != 0)
        {
            if (tmp[i] * i < K)
            {
                 
                // Decrease the value
                // of K
                K -= (i * tmp[i]);
 
                // Increment tmp[i/2]
                tmp[i / 2] += tmp[i];
 
                // Increment the count
                count += tmp[i];
 
                // Return count, if all
                // process are scheduled
                if (K <= 0)
                {
                    return count;
                }
            }
 
            else
            {
                 
                // Increment count
                if (K % i != 0)
                {
                    count += (K / i) + 1;
                }
                else
                {
                    count += (K / i);
                }
 
                // Return the count
                return count;
            }
        }
    }
 
    // If it is not possible to
    // schedule all process
    return -1;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 1, 7, 2, 4 };
    int N = 5;
    int K = 15;
     
    cout << minTime(arr, N, K);
     
    return 0;
}
 
// This code is contributed by mohit kumar 29


Java
// Java program for the above approach
 
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Function to find minimum required
    // time to schedule all process
    static int minTime(int[] A, int n, int K)
    {
        // Stores max element from A[]
        int max_ability = A[0];
 
        // Find the maximum element
        for (int i = 1; i < n; i++) {
            max_ability = Math.max(
                max_ability, A[i]);
        }
 
        // Stores frequency of each element
        int tmp[] = new int[max_ability + 1];
 
        // Stores minimum time required
        // to schedule all process
        int count = 0;
 
        // Count frequencies of elements
        for (int i = 0; i < n; i++) {
            tmp[A[i]]++;
        }
 
        // Find the minimum time
        for (int i = max_ability;
             i >= 0; i--) {
 
            if (tmp[i] != 0) {
 
                if (tmp[i] * i < K) {
 
                    // Decrease the value
                    // of K
                    K -= (i * tmp[i]);
 
                    // Increment tmp[i/2]
                    tmp[i / 2] += tmp[i];
 
                    // Increment the count
                    count += tmp[i];
 
                    // Return count, if all
                    // process are scheduled
                    if (K <= 0) {
                        return count;
                    }
                }
 
                else {
 
                    // Increment count
                    if (K % i != 0) {
                        count += (K / i) + 1;
                    }
                    else {
                        count += (K / i);
                    }
 
                    // Return the count
                    return count;
                }
            }
        }
 
        // If it is not possible to
        // schedule all process
        return -1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 3, 1, 7, 2, 4 };
        int N = arr.length;
        int K = 15;
        System.out.println(
            minTime(arr, N, K));
    }
}


Python3
# Python3 program for the above approach
 
# Function to find minimum required
# time to schedule all process
def minTime(A, n, K):
     
    # Stores max element from A[]
    max_ability = A[0]
 
    # Find the maximum element
    for i in range(1, n):
        max_ability = max(max_ability, A[i])
 
    # Stores frequency of each element
    tmp = [0 for i in range(max_ability + 1)]
 
    # Stores minimum time required
    # to schedule all process
    count = 0
 
    # Count frequencies of elements
    for i in range(n):
        tmp[A[i]] += 1
 
    # Find the minimum time
    i = max_ability
     
    while(i >= 0):
        if (tmp[i] != 0):
            if (tmp[i] * i < K):
                 
                # Decrease the value
                # of K
                K -= (i * tmp[i])
 
                # Increment tmp[i/2]
                tmp[i // 2] += tmp[i]
 
                # Increment the count
                count += tmp[i]
 
                # Return count, if all
                # process are scheduled
                if (K <= 0):
                    return count
            else:
                 
                # Increment count
                if (K % i != 0):
                    count += (K // i) + 1
                else:
                    count += (K // i)
 
                # Return the count
                return count
        i -= 1
 
    # If it is not possible to
    # schedule all process
    return -1
 
# Driver code
if __name__ == '__main__':
     
    arr = [ 3, 1, 7, 2, 4 ]
    N = 5
    K = 15
     
    print(minTime(arr, N, K))
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find minimum required
// time to schedule all process
static int minTime(int[] A, int n, int K)
{
     
    // Stores max element from A[]
    int max_ability = A[0];
 
    // Find the maximum element
    for(int i = 1; i < n; i++)
    {
        max_ability = Math.Max(
            max_ability, A[i]);
    }
 
    // Stores frequency of each element
    int []tmp = new int[max_ability + 1];
 
    // Stores minimum time required
    // to schedule all process
    int count = 0;
 
    // Count frequencies of elements
    for(int i = 0; i < n; i++)
    {
        tmp[A[i]]++;
    }
     
    // Find the minimum time
    for(int i = max_ability; i >= 0; i--)
    {
        if (tmp[i] != 0)
        {
            if (tmp[i] * i < K)
            {
                 
                // Decrease the value
                // of K
                K -= (i * tmp[i]);
 
                // Increment tmp[i/2]
                tmp[i / 2] += tmp[i];
 
                // Increment the count
                count += tmp[i];
 
                // Return count, if all
                // process are scheduled
                if (K <= 0)
                {
                    return count;
                }
            }
 
            else
            {
                 
                // Increment count
                if (K % i != 0)
                {
                    count += (K / i) + 1;
                }
                else
                {
                    count += (K / i);
                }
 
                // Return the count
                return count;
            }
        }
    }
 
    // If it is not possible to
    // schedule all process
    return -1;
}
 
// Driver Code
public static void Main(string[] args)
{
    int []arr = { 3, 1, 7, 2, 4 };
    int N = arr.Length;
    int K = 15;
     
    Console.WriteLine(minTime(arr, N, K));
}
}
 
// This code is contributed by ukasp


Javascript


C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to execute k processes that can be gained in
// minimum amount of time
void executeProcesses(int A[], int N, int K)
{
    // Stores all the array elements
    priority_queue pq;
 
    // Push all the elements to the
    // priority queue
    for (int i = 0; i < N; i++) {
        pq.push(A[i]);
    }
 
    // Stores the required result
    int ans = 0;
 
    // Loop while the queue is not
    // empty and K is positive
    while (!pq.empty() && K > 0) {
 
        // Store the top element
        // from the pq
        int top = pq.top();
 
        // Pop it from the pq
        pq.pop();
 
        // Add it to the answer
        ans ++;
 
        // Divide it by 2 and push it
        // back to the pq
        K = K - top;
        top = top / 2;
        pq.push(top);
    }
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    int A[] = { 3, 1, 7, 4, 2 };
    int K = 15;
    int N = sizeof(A) / sizeof(A[0]);
    executeProcesses(A, N, K);
 
    return 0;
}


Python3
# Python3 program for the above approach
  
# Function to execute k processes that
# can be gained in minimum amount of time
def executeProcesses(A, N, K):
     
    # Stores all the array elements
    pq = []
     
    # Push all the elements to the
    # priority queue
    for i in range(N):
        pq.append(A[i])
     
    # Stores the required result
    ans = 0
    pq.sort()
     
    # Loop while the queue is not
    # empty and K is positive
    while (len(pq) > 0 and K > 0):
         
        # Store the top element
        # from the pq
        top = pq.pop()
         
        # Add it to the answer
        ans += 1
         
        # Divide it by 2 and push it
        # back to the pq
        K -= top
        top //=2
        pq.append(top)
         
        pq.sort()
     
    # Print the answer
    print(ans)
 
# Driver Code
A = [ 3, 1, 7, 4, 2 ]
K = 15
N=len(A)
executeProcesses(A, N, K)
 
#  This code is contributed by patel2127


Javascript


输出
4

时间复杂度: O(M),其中 M 是数组中的最大元素
辅助空间: O(M)

替代方法(使用 STL):在最大堆的帮助下,可以使用贪心方法解决给定的问题。请按照以下步骤解决问题:

  • 初始化一个优先级队列,比如 PQ,并将给定数组的所有元素插入 PQ。
  • 初始化一个变量,比如 ans 为 0 来存储最终获得的最大钻石。
  • 迭代一个循环,直到优先级队列 PQ 不为空且 K 的值 > 0:
    • 弹出优先级队列的顶部元素,并将弹出的元素添加到变量 ans 中。
    • 将弹出的元素除以2,插入优先队列PQ。
    • 将 K 的值减 1。
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++14

// C++ program for the above approach
#include 
using namespace std;
 
// Function to execute k processes that can be gained in
// minimum amount of time
void executeProcesses(int A[], int N, int K)
{
    // Stores all the array elements
    priority_queue pq;
 
    // Push all the elements to the
    // priority queue
    for (int i = 0; i < N; i++) {
        pq.push(A[i]);
    }
 
    // Stores the required result
    int ans = 0;
 
    // Loop while the queue is not
    // empty and K is positive
    while (!pq.empty() && K > 0) {
 
        // Store the top element
        // from the pq
        int top = pq.top();
 
        // Pop it from the pq
        pq.pop();
 
        // Add it to the answer
        ans ++;
 
        // Divide it by 2 and push it
        // back to the pq
        K = K - top;
        top = top / 2;
        pq.push(top);
    }
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    int A[] = { 3, 1, 7, 4, 2 };
    int K = 15;
    int N = sizeof(A) / sizeof(A[0]);
    executeProcesses(A, N, K);
 
    return 0;
}

蟒蛇3

# Python3 program for the above approach
  
# Function to execute k processes that
# can be gained in minimum amount of time
def executeProcesses(A, N, K):
     
    # Stores all the array elements
    pq = []
     
    # Push all the elements to the
    # priority queue
    for i in range(N):
        pq.append(A[i])
     
    # Stores the required result
    ans = 0
    pq.sort()
     
    # Loop while the queue is not
    # empty and K is positive
    while (len(pq) > 0 and K > 0):
         
        # Store the top element
        # from the pq
        top = pq.pop()
         
        # Add it to the answer
        ans += 1
         
        # Divide it by 2 and push it
        # back to the pq
        K -= top
        top //=2
        pq.append(top)
         
        pq.sort()
     
    # Print the answer
    print(ans)
 
# Driver Code
A = [ 3, 1, 7, 4, 2 ]
K = 15
N=len(A)
executeProcesses(A, N, K)
 
#  This code is contributed by patel2127

Javascript


输出
4

时间复杂度O((N + K)*log N)

辅助空间O(N)

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