📌  相关文章
📜  包含给定尺寸的 N 个非重叠矩形的最小正方形的大小

📅  最后修改于: 2021-10-23 09:10:03             🧑  作者: Mango

给定两个正整数WH以及N个尺寸为W*H 的矩形,任务是找到所需的最小正方形尺寸,以便所有N 个矩形都可以打包而不重叠。

例子:

方法:给定的问题基于以下观察:

  • 可以证明,正方形内矩形的最佳间距之一由下式给出:

  • 可以在边为X的正方形中拟合的尺寸为W*H的矩形的最大数量由⌊X/W⌋⋅⌊X/H⌋ 给出
  • 上述函数是单调递增的。因此,想法是使用二分搜索来找到满足给定条件的正方形的最小边。

请按照以下步骤解决问题:

  • 初始化两个变量,1W*H*N
  • 迭代直到i小于j并执行以下步骤:
    • mid的值为(i + j)/2
    • 现在,如果值(mid/W)*(mid/H)最多为 N ,则将high的值更新为mid
    • 否则,将low的值更新为(mid + 1)
  • 完成以上步骤后,打印high的值作为结果值。

下面是上述方法的实现:

C++
// CPP program for the above approach
#include
using namespace std;
 
// Function to check if side of square X
// can pack all the N rectangles or not
bool bound(int w, int h, int N, int x)
{
     
    // Find the number of rectangle
    // it can pack
    int val = (x / w) * (x / h);
 
    // If val is atleast N,
    // then return true
    if (val >= N)
        return true;
 
    // Otherwise, return false
    else
        return false;
}
 
// Function to find the size of the
// smallest square that can contain
// N rectangles of dimensions W * H
int FindSquare(int N, int W, int H)
{
     
    // Stores the lower bound
    int i = 1;
 
    // Stores the upper bound
    int j = W * H * N;
 
    // Iterate until i is less than j
    while (i < j)
    {
         
        // Calculate the mid value
        int mid = i + (j - i) / 2;
 
        // If the current size of square
        // cam contain N rectangles
        if (bound(W, H, N, mid))
            j = mid;
 
        // Otherwise, update i
        else
            i = mid + 1;
    }
 
    // Return the minimum size of the
    // square required
    return j;
}
 
// Driver code
int main()
{
    int W = 2;
    int H = 3;
    int N = 10;
 
    // Function Call
    cout << FindSquare(N, W, H);
}
 
// This code is contributed by ipg2016107.


Java
// Java program for the above approach
class GFG{
     
// Function to check if side of square X
// can pack all the N rectangles or not
static boolean bound(int w, int h, int N, int x)
{
     
    // Find the number of rectangle
    // it can pack
    int val = (x / w) * (x / h);
 
    // If val is atleast N,
    // then return true
    if (val >= N)
        return true;
 
    // Otherwise, return false
    else
        return false;
}
 
// Function to find the size of the
// smallest square that can contain
// N rectangles of dimensions W * H
static int FindSquare(int N, int W, int H)
{
     
    // Stores the lower bound
    int i = 1;
 
    // Stores the upper bound
    int j = W * H * N;
 
    // Iterate until i is less than j
    while (i < j)
    {
         
        // Calculate the mid value
        int mid = i + (j - i) / 2;
 
        // If the current size of square
        // cam contain N rectangles
        if (bound(W, H, N, mid))
            j = mid;
 
        // Otherwise, update i
        else
            i = mid + 1;
    }
 
    // Return the minimum size of the
    // square required
    return j;
}
 
// Driver code
public static void main(String[] args)
{
    int W = 2;
    int H = 3;
    int N = 10;
 
    // Function Call
    System.out.print(FindSquare(N, W, H));
}
}
 
// This code is contributed by sk944795


Python3
# Python program for the above approach
 
# Function to check if side of square X
# can pack all the N rectangles or not
def bound(w, h, N, x):
 
    # Find the number of rectangle
    # it can pack
    val = (x//w)*(x//h)
     
    # If val is atleast N,
    # then return true
    if(val >= N):
        return True
       
    # Otherwise, return false
    else:
        return False
 
# Function to find the size of the
# smallest square that can contain
# N rectangles of dimensions W * H
def FindSquare(N, W, H):
 
    # Stores the lower bound
    i = 1
     
    # Stores the upper bound
    j = W * H*N
     
    # Iterate until i is less than j
    while(i < j):
       
        # Calculate the mid value
        mid = i + (j - i)//2
 
        # If the current size of square
        # cam contain N rectangles
        if(bound(W, H, N, mid)):
            j = mid
         
        # Otherwise, update i
        else:
            i = mid + 1
 
    # Return the minimum size of the
    # square required
    return j
 
# Driver Code
 
W = 2
H = 3
N = 10
 
# Function Call
print(FindSquare(N, W, H))


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if side of square X
// can pack all the N rectangles or not
static bool bound(int w, int h, int N, int x)
{
     
    // Find the number of rectangle
    // it can pack
    int val = (x / w) * (x / h);
 
    // If val is atleast N,
    // then return true
    if (val >= N)
        return true;
 
    // Otherwise, return false
    else
        return false;
}
 
// Function to find the size of the
// smallest square that can contain
// N rectangles of dimensions W * H
static int FindSquare(int N, int W, int H)
{
 
    // Stores the lower bound
    int i = 1;
 
    // Stores the upper bound
    int j = W * H * N;
 
    // Iterate until i is less than j
    while (i < j)
    {
         
        // Calculate the mid value
        int mid = i + (j - i) / 2;
 
        // If the current size of square
        // cam contain N rectangles
        if (bound(W, H, N, mid))
            j = mid;
 
        // Otherwise, update i
        else
            i = mid + 1;
    }
     
    // Return the minimum size of the
    // square required
    return j;
}
 
// Driver Code
public static void Main()
{
    int W = 2;
    int H = 3;
    int N = 10;
 
    // Function Call
    Console.WriteLine(FindSquare(N, W, H));
}
}
 
// This code is contributed by ukasp


Javascript


输出:
9

时间复杂度: O(log(W*H))
辅助空间: O(1)

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