给定一个正整数K和一个由N 个正整数组成的数组arr[] ,这样arr[i]是第i个处理器可以在1 秒内调度的进程数。任务是最小化调度K 个进程所需的总时间,以便在第i个处理器调度后, arr[i]减少到floor(arr[i]/2) 。
例子:
Input: N = 5, arr[] = {3, 1, 7, 2, 4}, K = 15
Output: 4
Explanation:
The order of scheduled process are as follows:
- The 3rd process is scheduled first. The array arr[] modifies to {3, 1, 3, 2, 4}, as arr[2] = floor(arr[2] / 2) = floor(7 / 2) = 3.
- The 5th process is scheduled next. The array arr[] modifies to {3, 1, 3, 2, 2}.
- The 1st process is scheduled next. The array arr[] modifies to {1, 1, 3, 2, 2}.
- The 2nd process is scheduled next. The array arr[] modifies to {3, 0, 3, 2, 4}.
The total processes scheduled by all the process = 7 + 4 + 3 + 1 = 15(= K) and the total time required is 4 seconds.
Input: N = 4, arr[] = {1, 5, 8, 6}, K = 10
Output: 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[] 中的当前元素至少为0且i * tmp[i] 的值至少为 K ,则执行以下步骤:
- 如果K可被当前索引整除,则将count的值增加K / i 。
- 否则,将count的值增加K/i +1 。
- 如果tmp[] 中的当前元素大于0并且i * tmp[i]小于K 。
- 完成上述步骤后,如果无法调度所有进程,则打印-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 frquencies 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 frquencies 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 frquencies 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 frquencies 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
4
时间复杂度: O(M),其中 M 是数组中的最大元素。
辅助空间: O(M)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live