📜  给定 N 个立方体的边时每个人的最大立方体体积

📅  最后修改于: 2022-05-13 01:57:49.725000             🧑  作者: Mango

给定 N 个立方体的边时每个人的最大立方体体积

给定一个由N个整数组成的数组,分别表示 N 个立方体结构的边。还给出了表示人口数量的M个整数。任务是找到可以给每个人的立方体的最大体积。
注意:立方体可以从任何 N 个立方体中切出任何形状。
例子:

朴素方法:一种朴素的方法是首先计算所有立方体的体积,然后线性检查每个体积是否可以分布在所有 M 个人中,并找到所有这些体积中的最大体积。
时间复杂度: O(N 2 )

有效的方法:一种有效的方法是使用二分搜索来找到答案。由于数组中给出了边长,因此将它们转换为相应立方体的体积。在所有立方体的体积中找到最大的体积。说,最大音量是maxVolume 。现在,对范围 [0, maxVolume] 执行二进制搜索

  • 计算范围的中间值,比如中间值。
  • 现在,计算所有体积为mid的立方体中可以切割的立方体总数。
  • 如果可以切割的立方体总数超过人数,则可以为每个人切割该体积的立方体,因此我们在[mid+1, maxVolume]范围内检查更大的值。
  • 如果总立方体不超过人数,那么我们检查[low, mid-1]范围内的答案。

下面是上述方法的实现:

C++
// C++ program to implement the above approach
 
#include 
using namespace std;
 
// Function to get the maximum volume that
// every person can get
int getMaximumVloume(int a[], int n, int m)
{
    int maxVolume = 0;
 
    // Convert the length to respective volumes
    // and find the maximum volumes
    for (int i = 0; i < n; i++) {
        a[i] = a[i] * a[i] * a[i];
 
        maxVolume = max(a[i], maxVolume);
    }
 
    // Apply binary search with initial
    // low as 0 and high as the maximum most
    int low = 0, high = maxVolume;
 
    // Initial answer is 0 slices
    int maxVol = 0;
 
    // Apply binary search
    while (low <= high) {
 
        // Get the mid element
        int mid = (low + high) >> 1;
 
        // Count the slices of volume mid
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            cnt += a[i] / mid;
        }
 
        // If the slices of volume
        // exceeds the number of persons
        // then every person can get volume mid
        if (cnt >= m) {
 
            // Then check for larger in the right half
            low = mid + 1;
 
            // Replace tbe answer with
            // current maximum i.e., mid
            maxVol = max(maxVol, mid);
        }
 
        // else traverse in the left half
        else
            high = mid - 1;
    }
 
    return maxVol;
}
 
// Driver code
int main()
{
    int a[] = { 1, 1, 1, 2, 2 };
    int n = sizeof(a) / sizeof(a[0]);
    int m = 3;
 
    cout << getMaximumVloume(a, n, m);
 
    return 0;
}


Java
// Java program to implement the above approach
class GFG
{
 
    // Function to get the maximum volume that
    // every person can get
    static int getMaximumVloume(int a[], int n, int m)
    {
        int maxVolume = 0;
 
        // Convert the length to respective volumes
        // and find the maximum volumes
        for (int i = 0; i < n; i++)
        {
            a[i] = a[i] * a[i] * a[i];
 
            maxVolume = Math.max(a[i], maxVolume);
        }
 
        // Apply binary search with initial
        // low as 0 and high as the maximum most
        int low = 0, high = maxVolume;
 
        // Initial answer is 0 slices
        int maxVol = 0;
 
        // Apply binary search
        while (low <= high)
        {
 
            // Get the mid element
            int mid = (low + high) >> 1;
 
            // Count the slices of volume mid
            int cnt = 0;
            for (int i = 0; i < n; i++)
            {
                cnt += a[i] / mid;
            }
 
            // If the slices of volume
            // exceeds the number of persons
            // then every person can get volume mid
            if (cnt >= m)
            {
 
                // Then check for larger in the right half
                low = mid + 1;
 
                // Replace tbe answer with
                // current maximum i.e., mid
                maxVol = Math.max(maxVol, mid);
            }
             
            // else traverse in the left half
            else
            {
                high = mid - 1;
            }
        }
 
        return maxVol;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int a[] = {1, 1, 1, 2, 2};
        int n = a.length;
        int m = 3;
 
        System.out.println(getMaximumVloume(a, n, m));
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program to implement
# the above approach
 
# Function to get the maximum volume
# that every person can get
def getMaximumVloume(a, n, m):
    maxVolume = 0
 
    # Convert the length to respective
    # volumes and find the maximum volumes
    for i in range(n):
        a[i] = a[i] * a[i] * a[i]
 
        maxVolume = max(a[i], maxVolume)
 
    # Apply binary search with initial
    # low as 0 and high as the maximum most
    low = 0
    high = maxVolume
 
    # Initial answer is 0 slices
    maxVol = 0
 
    # Apply binary search
    while (low <= high):
         
        # Get the mid element
        mid = (low + high) >> 1
 
        # Count the slices of volume mid
        cnt = 0
        for i in range(n):
            cnt += int(a[i] / mid)
 
        # If the slices of volume
        # exceeds the number of persons
        # then every person can get volume mid
        if (cnt >= m):
             
            # Then check for larger in the right half
            low = mid + 1
 
            # Replace tbe answer with
            # current maximum i.e., mid
            maxVol = max(maxVol, mid)
 
        # else traverse in the left half
        else:
            high = mid - 1
 
    return maxVol
 
# Driver code
if __name__ == '__main__':
    a = [1, 1, 1, 2, 2]
    n = len(a)
    m = 3
 
    print(getMaximumVloume(a, n, m))
 
# This code is contributed
# by Surendra_Gangwar


C#
// C# program to implement the above approach
using System;
 
class GFG
{
 
    // Function to get the maximum volume that
    // every person can get
    static int getMaximumVloume(int []a, int n, int m)
    {
        int maxVolume = 0;
 
        // Convert the length to respective volumes
        // and find the maximum volumes
        for (int i = 0; i < n; i++)
        {
            a[i] = a[i] * a[i] * a[i];
 
            maxVolume = Math.Max(a[i], maxVolume);
        }
 
        // Apply binary search with initial
        // low as 0 and high as the maximum most
        int low = 0, high = maxVolume;
 
        // Initial answer is 0 slices
        int maxVol = 0;
 
        // Apply binary search
        while (low <= high)
        {
 
            // Get the mid element
            int mid = (low + high) >> 1;
 
            // Count the slices of volume mid
            int cnt = 0;
            for (int i = 0; i < n; i++)
            {
                cnt += a[i] / mid;
            }
 
            // If the slices of volume
            // exceeds the number of persons
            // then every person can get volume mid
            if (cnt >= m)
            {
 
                // Then check for larger in the right half
                low = mid + 1;
 
                // Replace tbe answer with
                // current maximum i.e., mid
                maxVol = Math.Max(maxVol, mid);
            }
             
            // else traverse in the left half
            else
            {
                high = mid - 1;
            }
        }
 
        return maxVol;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []a = {1, 1, 1, 2, 2};
        int n = a.Length;
        int m = 3;
 
        Console.WriteLine(getMaximumVloume(a, n, m));
    }
}
 
/* This code contributed by PrinciRaj1992 */


PHP
> 1;
 
        // Count the slices of volume mid
        $cnt = 0;
        for ($i = 0; $i < $n; $i++)
        {
            $cnt += (int)($a[$i] / $mid);
        }
 
        // If the slices of volume
        // exceeds the number of persons
        // then every person can get volume mid
        if ($cnt >= $m)
        {
 
            // Then check for larger in the right half
            $low = $mid + 1;
 
            // Replace tbe answer with
            // current maximum i.e., mid
            $maxVol = max($maxVol, $mid);
        }
 
        // else traverse in the left half
        else
            $high = $mid - 1;
    }
 
    return $maxVol;
}
 
// Driver code
$a = array(1, 1, 1, 2, 2);
$n = sizeof($a);
$m = 3;
 
echo getMaximumVloume($a, $n, $m);
 
// This code is contributed by Akanksha Rai
?>


Javascript


输出:
4

时间复杂度: O(N * log (maxVolume))