📜  生产m件物品所需的最短时间

📅  最后修改于: 2021-05-06 22:16:10             🧑  作者: Mango

考虑n个机器,它们生产相同类型的项目,但速率不同,即,机器1花费1秒钟来生产一件物品,机器2花费2秒钟来生产一件物品。给定其中包含由我需要机以产生项时的阵列。考虑到所有机器同时工作,任务是找到生产m件物品所需的最短时间。
例子:

Input : arr[] = {1, 2, 3}, m = 11
Output : 6
In 6 sec, machine 1 produces 6 items, machine 2 produces 3 items,
and machine 3 produces 2 items. So to produce 11 items minimum
6 sec are required.

Input : arr[] = {5, 6}, m = 11
Output : 30

方法1 :(强力)
初始化时间= 0并将其递增1。计算每次生产的物料数量,直到生产的物料数量不等于m为止。
下面是上述想法的实现:

C++
// C++ program to find minimum time
// required to produce m items.
#include
using namespace std;
 
// Return the minimum time required to
// produce m items with given machines.
int minTime(int arr[], int n, int m)
{
    // Intialise time, items equal to 0.
    int t = 0;
 
    while (1)
    {
        int items = 0;
 
        // Calculating items at each second
        for (int i = 0; i < n; i++)
            items += (t / arr[i]);
 
        // If items equal to m return time.
        if (items >= m)
            return t;
 
        t++; // Increment time
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3 };
    int n = sizeof(arr)/sizeof(arr[0]);
    int m = 11;
 
    cout << minTime(arr, n, m) << endl;
 
    return 0;
}


Java
// Java program to find minimum time
// required to produce m items.
import java.io.*;
 
public class GFG{
 
// Return the minimum time required to
// produce m items with given machines.
static int minTime(int []arr, int n,
                   int m)
{
     
    // Intialise time, items equal to 0.
    int t = 0;
 
    while (true)
    {
        int items = 0;
 
        // Calculating items at each second
        for (int i = 0; i < n; i++)
            items += (t / arr[i]);
 
        // If items equal to m return time.
        if (items >= m)
            return t;
 
        t++; // Increment time
    }
}
 
    // Driver Code
    static public void main (String[] args)
    {
            int []arr = { 1, 2, 3 };
            int n = arr.length;
            int m = 11;
 
    System.out.println(minTime(arr, n, m));
    }
}
 
// This code is contributed by vt_m.


Python
# Python3 program to find minimum time
# required to produce m items.
import math as mt
 
# Return the minimum time required to
# produce m items with given machines.
def minTime(arr, n, m):
 
    # Intialise time, items equal to 0.
    t = 0
 
    while (1):
     
        items = 0
 
        # Calculating items at each second
        for i in range(n):
            items += (t // arr[i])
 
        # If items equal to m return time.
        if (items >= m):
            return t
 
        t += 1 # Increment time
     
# Driver Code
arr = [1, 2, 3]
n = len(arr)
m = 11
 
print(minTime(arr, n, m) )
 
# This code is contributed by
# Mohit kumar 29


C#
// C# program to find minimum time
// required to produce m items.
using System;
  
public class GFG{
  
// Return the minimum time
// required to produce m
// items with given machines.
static int minTime(int []arr, int n,
                   int m)
{
     
    // Intialise time, items equal to 0.
    int t = 0;
  
    while (true)
    {
        int items = 0;
  
        // Calculating items at each second
        for (int i = 0; i < n; i++)
            items += (t / arr[i]);
  
        // If items equal to m return time.
        if (items >= m)
            return t;
  
        t++; // Increment time
    }
}
  
// Driven Code
static public void Main (String []args)
{
    int []arr = {1, 2, 3};
    int n = arr.Length;
    int m = 11;
  
    // Calling function
    Console.WriteLine(minTime(arr, n, m));
     
}
}


PHP
= $m)
            return $t;
 
        $t++; // Increment time
    }
}
 
    // Driver Code
    $arr = array(1, 2, 3);
    $n =count($arr);
    $m = 11;
    echo minTime($arr, $n, $m);
 
// This code is contributed by anuj_67.
?>


Javascript


C++
// Efficient C++ program to find minimum time
// required to produce m items.
#include
using namespace std;
 
// Return the number of items can be
// produced in temp sec.
int findItems(int arr[], int n, int temp)
{
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans += (temp/arr[i]);
    return ans;
}
 
// Binary search to find minimum time required
// to produce M items.
int bs(int arr[], int n, int m, int high)
{
    int low = 1;
 
    // Doing binary search to find minimum
    // time.
    while (low < high)
    {
        // Finding the middle value.
        int mid = (low+high)>>1;
 
        // Calculate number of items to
        // be produce in mid sec.
        int itm = findItems(arr, n, mid);
 
        // If items produce is less than
        // required, set low = mid + 1.
        if (itm < m)
            low = mid+1;
 
        //  Else set high = mid.
        else
            high = mid;
    }
 
    return high;
}
 
// Return the minimum time required to
// produce m items with given machine.
int minTime(int arr[], int n, int m)
{
    int maxval = INT_MIN;
 
    // Finding the maximum time in the array.
    for (int i = 0; i < n; i++)
        maxval = max(maxval, arr[i]);
 
    return bs(arr, n, m, maxval*m);
}
 
// Driven Program
int main()
{
    int arr[] = { 1, 2, 3 };
    int n = sizeof(arr)/sizeof(arr[0]);
    int m = 11;
 
    cout << minTime(arr, n, m) << endl;
 
    return 0;
}


Java
// Efficient Java program to find
// minimum time required to
// produce m items.
import java.io.*;
 
public class GFG{
     
// Return the number of items can
// be produced in temp sec.
static int findItems(int []arr, int n,
                     int temp)
{
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans += (temp / arr[i]);
    return ans;
}
 
// Binary search to find minimum time
// required to produce M items.
static int bs(int []arr, int n,
              int m, int high)
              
{
    int low = 1;
 
    // Doing binary search to
    // find minimum time.
    while (low < high)
    {
        // Finding the middle value.
        int mid = (low + high) >> 1;
 
        // Calculate number of items to
        // be produce in mid sec.
        int itm = findItems(arr, n, mid);
 
        // If items produce is less than
        // required, set low = mid + 1.
        if (itm < m)
            low = mid + 1;
 
        // Else set high = mid.
        else
            high = mid;
    }
 
    return high;
}
 
// Return the minimum time required to
// produce m items with given machine.
static int minTime(int []arr, int n,
                   int m)
{
    int maxval = Integer.MIN_VALUE;
 
    // Finding the maximum time in the array.
    for (int i = 0; i < n; i++)
        maxval = Math.max(maxval, arr[i]);
 
    return bs(arr, n, m, maxval * m);
}
 
// Driven Program
static public void main (String[] args)
{
    int []arr = {1, 2, 3};
        int n = arr.length;
    int m = 11;
 
    System.out.println(minTime(arr, n, m));
}
}
 
// This code is contributed by vt_m.


Python
# Efficient Python3 program to find
# minimum time required to produce m items.
import sys
 
def findItems(arr, n, temp):
    ans = 0
    for i in range(n):
        ans += temp // arr[i]
    return ans
 
# Binary search to find minimum time
# required to produce M items.
def bs(arr, n, m, high):
    low = 1
 
    # Doing binary search to find minimum
    # time.
    while low < high:
 
        # Finding the middle value.
        mid = (low + high) >> 1
 
        # Calculate number of items to
        # be produce in mid sec.
        itm = findItems(arr, n, mid)
 
        # If items produce is less than
        # required, set low = mid + 1.
        if itm < m:
            low = mid + 1
 
        # Else set high = mid.
        else:
            high = mid
    return high
 
# Return the minimum time required to
# produce m items with given machine.
def minTime(arr, n, m):
    maxval = -sys.maxsize
 
    # Finding the maximum time in the array.
    for i in range(n):
        maxval = max(maxval, arr[i])
 
    return bs(arr, n, m, maxval * m)
 
# Driver Code
if __name__ == "__main__":
    arr = [1, 2, 3]
    n = len(arr)
    m = 11
    print(minTime(arr, n, m))
 
# This code is conributed by
# sanjeev2552


C#
// Efficient C# program to find
// minimum time required to
// produce m items.
using System;
 
public class GFG{
     
// Return the number of items can
// be produced in temp sec.
static int findItems(int []arr, int n,
                     int temp)
{
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans += (temp / arr[i]);
    return ans;
}
 
// Binary search to find minimum time
// required to produce M items..
static int bs(int []arr, int n,
              int m, int high)
{
    int low = 1;
 
    // Doing binary search to
    // find minimum time.
    while (low < high)
    {
        // Finding the middle value.
        int mid = (low + high) >> 1;
 
        // Calculate number of items to
        // be produce in mid sec.
        int itm = findItems(arr, n, mid);
 
        // If items produce is less than
        // required, set low = mid + 1.
        if (itm < m)
            low = mid + 1;
 
        // Else set high = mid.
        else
            high = mid;
    }
 
    return high;
}
 
// Return the minimum time required to
// produce m items with given machine.
static int minTime(int []arr, int n,
                   int m)
{
    int maxval = int.MinValue;
 
    // Finding the maximum time in the array.
    for (int i = 0; i < n; i++)
        maxval = Math.Max(maxval, arr[i]);
 
    return bs(arr, n, m, maxval * m);
}
 
// Driver Code
static public void Main ()
{
    int []arr = {1, 2, 3};
    int n = arr.Length;
    int m = 11;
 
    Console.WriteLine(minTime(arr, n, m));
}
}
 
// This code is contributed by vt_m.


PHP
> 1;
 
        // Calculate number of items to
        // be produce in mid sec.
        $itm = findItems($arr, $n, $mid);
 
        // If items produce is less than
        // required, set low = mid + 1.
        if ($itm < $m)
            $low = $mid + 1;
 
        // Else set high = mid.
        else
            $high = $mid;
    }
 
    return $high;
}
 
// Return the minimum time required to
// produce m items with given machine.
function minTime($arr, $n, $m)
{
    $maxval = PHP_INT_MIN;
 
    // Finding the maximum
    // time in the array.
    for($i = 0; $i < $n; $i++)
        $maxval = max($maxval, $arr[$i]);
 
    return bs($arr, $n, $m, $maxval*$m);
}
 
    // Driver Code
    $arr = array(1, 2, 3);
    $n = count($arr);
    $m = 11;
 
    echo minTime($arr, $n, $m);
 
// This code is contributed by anuj_67.
?>


输出:

6

方法2(有效):
这个想法是使用二进制搜索。要求产生m个最大的时间内将最大时间的阵列,最大值乘以米最大*米英寸因此,请使用介于1到最大* m之间的二进制搜索,并找出产生m个项目的最短时间。
下面是上述想法的实现:

C++

// Efficient C++ program to find minimum time
// required to produce m items.
#include
using namespace std;
 
// Return the number of items can be
// produced in temp sec.
int findItems(int arr[], int n, int temp)
{
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans += (temp/arr[i]);
    return ans;
}
 
// Binary search to find minimum time required
// to produce M items.
int bs(int arr[], int n, int m, int high)
{
    int low = 1;
 
    // Doing binary search to find minimum
    // time.
    while (low < high)
    {
        // Finding the middle value.
        int mid = (low+high)>>1;
 
        // Calculate number of items to
        // be produce in mid sec.
        int itm = findItems(arr, n, mid);
 
        // If items produce is less than
        // required, set low = mid + 1.
        if (itm < m)
            low = mid+1;
 
        //  Else set high = mid.
        else
            high = mid;
    }
 
    return high;
}
 
// Return the minimum time required to
// produce m items with given machine.
int minTime(int arr[], int n, int m)
{
    int maxval = INT_MIN;
 
    // Finding the maximum time in the array.
    for (int i = 0; i < n; i++)
        maxval = max(maxval, arr[i]);
 
    return bs(arr, n, m, maxval*m);
}
 
// Driven Program
int main()
{
    int arr[] = { 1, 2, 3 };
    int n = sizeof(arr)/sizeof(arr[0]);
    int m = 11;
 
    cout << minTime(arr, n, m) << endl;
 
    return 0;
}

Java

// Efficient Java program to find
// minimum time required to
// produce m items.
import java.io.*;
 
public class GFG{
     
// Return the number of items can
// be produced in temp sec.
static int findItems(int []arr, int n,
                     int temp)
{
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans += (temp / arr[i]);
    return ans;
}
 
// Binary search to find minimum time
// required to produce M items.
static int bs(int []arr, int n,
              int m, int high)
              
{
    int low = 1;
 
    // Doing binary search to
    // find minimum time.
    while (low < high)
    {
        // Finding the middle value.
        int mid = (low + high) >> 1;
 
        // Calculate number of items to
        // be produce in mid sec.
        int itm = findItems(arr, n, mid);
 
        // If items produce is less than
        // required, set low = mid + 1.
        if (itm < m)
            low = mid + 1;
 
        // Else set high = mid.
        else
            high = mid;
    }
 
    return high;
}
 
// Return the minimum time required to
// produce m items with given machine.
static int minTime(int []arr, int n,
                   int m)
{
    int maxval = Integer.MIN_VALUE;
 
    // Finding the maximum time in the array.
    for (int i = 0; i < n; i++)
        maxval = Math.max(maxval, arr[i]);
 
    return bs(arr, n, m, maxval * m);
}
 
// Driven Program
static public void main (String[] args)
{
    int []arr = {1, 2, 3};
        int n = arr.length;
    int m = 11;
 
    System.out.println(minTime(arr, n, m));
}
}
 
// This code is contributed by vt_m.

Python

# Efficient Python3 program to find
# minimum time required to produce m items.
import sys
 
def findItems(arr, n, temp):
    ans = 0
    for i in range(n):
        ans += temp // arr[i]
    return ans
 
# Binary search to find minimum time
# required to produce M items.
def bs(arr, n, m, high):
    low = 1
 
    # Doing binary search to find minimum
    # time.
    while low < high:
 
        # Finding the middle value.
        mid = (low + high) >> 1
 
        # Calculate number of items to
        # be produce in mid sec.
        itm = findItems(arr, n, mid)
 
        # If items produce is less than
        # required, set low = mid + 1.
        if itm < m:
            low = mid + 1
 
        # Else set high = mid.
        else:
            high = mid
    return high
 
# Return the minimum time required to
# produce m items with given machine.
def minTime(arr, n, m):
    maxval = -sys.maxsize
 
    # Finding the maximum time in the array.
    for i in range(n):
        maxval = max(maxval, arr[i])
 
    return bs(arr, n, m, maxval * m)
 
# Driver Code
if __name__ == "__main__":
    arr = [1, 2, 3]
    n = len(arr)
    m = 11
    print(minTime(arr, n, m))
 
# This code is conributed by
# sanjeev2552

C#

// Efficient C# program to find
// minimum time required to
// produce m items.
using System;
 
public class GFG{
     
// Return the number of items can
// be produced in temp sec.
static int findItems(int []arr, int n,
                     int temp)
{
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans += (temp / arr[i]);
    return ans;
}
 
// Binary search to find minimum time
// required to produce M items..
static int bs(int []arr, int n,
              int m, int high)
{
    int low = 1;
 
    // Doing binary search to
    // find minimum time.
    while (low < high)
    {
        // Finding the middle value.
        int mid = (low + high) >> 1;
 
        // Calculate number of items to
        // be produce in mid sec.
        int itm = findItems(arr, n, mid);
 
        // If items produce is less than
        // required, set low = mid + 1.
        if (itm < m)
            low = mid + 1;
 
        // Else set high = mid.
        else
            high = mid;
    }
 
    return high;
}
 
// Return the minimum time required to
// produce m items with given machine.
static int minTime(int []arr, int n,
                   int m)
{
    int maxval = int.MinValue;
 
    // Finding the maximum time in the array.
    for (int i = 0; i < n; i++)
        maxval = Math.Max(maxval, arr[i]);
 
    return bs(arr, n, m, maxval * m);
}
 
// Driver Code
static public void Main ()
{
    int []arr = {1, 2, 3};
    int n = arr.Length;
    int m = 11;
 
    Console.WriteLine(minTime(arr, n, m));
}
}
 
// This code is contributed by vt_m.

的PHP

> 1;
 
        // Calculate number of items to
        // be produce in mid sec.
        $itm = findItems($arr, $n, $mid);
 
        // If items produce is less than
        // required, set low = mid + 1.
        if ($itm < $m)
            $low = $mid + 1;
 
        // Else set high = mid.
        else
            $high = $mid;
    }
 
    return $high;
}
 
// Return the minimum time required to
// produce m items with given machine.
function minTime($arr, $n, $m)
{
    $maxval = PHP_INT_MIN;
 
    // Finding the maximum
    // time in the array.
    for($i = 0; $i < $n; $i++)
        $maxval = max($maxval, $arr[$i]);
 
    return bs($arr, $n, $m, $maxval*$m);
}
 
    // Driver Code
    $arr = array(1, 2, 3);
    $n = count($arr);
    $m = 11;
 
    echo minTime($arr, $n, $m);
 
// This code is contributed by anuj_67.
?>

输出:

6