📜  最少要交付的物品数

📅  最后修改于: 2021-04-26 06:53:30             🧑  作者: Mango

给定N个存储桶,每个存储桶包含A [i]个项目。给定K个游览,其中所有物品都需要交付。一次旅行只能从一个水桶中取物品。任务是告诉每个巡回演出需要运送的物品的最小数量,以便可以在K个巡回演出中运送所有物品。
条件:K> = N
例子

Input : 
N = 5
A[] = { 1, 3, 5, 7, 9 }
K = 10
Output : 3
By delivering 3 items at a time, 
Number of tours required for bucket 1 = 1
Number of tours required for bucket 2 = 1
Number of tours required for bucket 3 = 2
Number of tours required for bucket 4 = 3
Number of tours required for bucket 5 = 3
Total number of tours = 10

Input :
N = 10
A = 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
k = 50
Output : 9

方法:需要查找每次交货的最小项目数。因此,其想法是从1迭代到存储桶中项目的最大值,并计算每个存储桶所需的行程数,并找到完整交付所需的行程总数。行程小于或等于K的第一个这样的值给出了所需的数字。
下面是上述想法的实现:

C++
//C++ program to find the minimum numbers of tours required
#include 
  
using namespace std;
int getMin(int N,int A[],int k)
{
    //Iterating through each possible 
   // value of minimum Items
   int maximum=0,tours=0;
     
   for(int i=0;i


Java
// Java program to find the minimum numbers of tours required
import java.util.*;
class solution
{
  
static int getMin(int N,int A[],int k)
{
    //Iterating through each possible 
// value of minimum Items
int maximum=0,tours=0;
      
for(int i=0;i


Python3
# Python3 program to find minimum numbers of 
# tours required
  
def getMin(N, A, k):
  
    # Iterating through each possible 
    # value of minimum Items
    for i in range(1, max(A)+1):
        tours = 0
        for j in range(0, len(A)):
            if(A[j]% i == 0):# Perfectly Divisible
                tours += A[j]/i
  
            else:
                # Not Perfectly Divisible means required
                # tours are Floor Division + 1
                tours += A[j]//i + 1 
  
        if(tours <= k):
            # Return First Feasible Value found,
            # since it is also the minimum
            return i 
    return "Not Possible"
  
# Driver Code
N = 10
A = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
k = 50
print(getMin(N, A, k))


C#
// C# program to find the minimum 
// numbers of tours required
using System;
  
class GFG
{
  
static int getMin(int N, int [] A, int k)
{
    // Iterating through each possible 
    // value of minimum Items
    int maximum = 0,tours = 0;
      
    for(int i = 0; i < N; i++)
        maximum = Math.Max(maximum, A[i]);
          
    for(int i = 1; i < maximum + 1; i++)
    {
        tours = 0;
        for(int j = 0; j < N; j++)
        {
            if(A[j] % i == 0)// perfecctly Divisible 
        {
            tours += A[j] /i;
        }
        else
        {
                // Not Perfectly Divisible means required
                // tours are Floor Division + 1
                tours += (int)Math.Floor(A[j] / (i * 1.0)) + 1;
        }
    }
        if(tours <= k)
        {
            // Return First Feasible Value found,
            // since it is also the minimum
            return i;
        }
    }
      
    return -1;
}
  
// Driver code
public static void Main() 
{
    int []a = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
  
    int n = 10;
  
    int k = 50;
  
    if(getMin(n, a, k) == -1)
        Console.WriteLine("Not Possible");
    else
        Console.WriteLine(getMin(n, a, k));
}
}
  
// This code is contributed by 
// Mohit kumar


PHP


输出:
9