📜  给定矩形的正好k个切割可以得到的最小可能面积中的最大值

📅  最后修改于: 2021-04-27 19:47:20             🧑  作者: Mango

给定n×m的大矩形区域(具有单位正方形)并允许k倍的切割,切割应该是笔直的(水平或垂直),并且应沿着单位正方形的边缘进行。准确地进行k次切割可以得到的最小工件的最大可能面积是多少。

例子 :

Input : 3 4 1
Output : 6

Input : 6 4 2
Output : 8

第二个输入的图像

第一个输入的图像

由于这是n×m的矩形区域,因此有(n-1)行和(m-1)列。因此,如果k>(n + m – 2),则不可能进行切割。那么,如果k小于那个。有两种情况

  1. 当k小于max(n,m)– 1时:在第一种情况下,如果k小于max(n,m)– 1,则m *(n /(k + 1))或n *( m /(k + 1))最大值,这里我们将其除以(k +1),因为水平或垂直方向即(m * n =总块数)被分为(k +1)个部分。
  2. 当k大于或等于max(n,m)– 1时:在第二种情况下,如果k> = max(n,m)– 1,则行和列都将被剪切,因此,最大可能的最小面积为m /(k – n + 2)或n /(k – m + 2) 。在这种情况下,假设如果n> m,则首先可以进行n-1(行或列)切割。之后(k – n)切割将在m – 1上进行。因此,在这里我们调整了这个(k – n)切口,使得最小的可能除法应该最大。

代码–以下是以下方法的实现

C++
// C++ code for Maximum of smallest
// possible area that can get with
// exactly k cut of given rectangular
#include 
using namespace std;
 
void max_area(int n, int m, int k)
{
    if (k > (n + m - 2))
 
        cout << "Not possible" << endl;
 
    else {
 
        int result;
 
        // for the 1st case
        if (k < max(m, n) - 1) {
 
            result = max(m * (n / (k + 1)), n * (m / (k + 1)));
        }
 
        // for the second case
        else {
 
            result = max(m / (k - n + 2), n / (k - m + 2));
        }
 
        // print final result
        cout << result << endl;
    }
}
 
// driver code
int main()
{
 
    int n = 3, m = 4, k = 1;
    max_area(n, m, k);
}


Java
// Java code for Maximum of smallest
// possible area that can get with
// exactly k cut of given rectangular
 
class GFG {
     
    //Utility Function
    static void max_area(int n, int m, int k)
    {
        if (k > (n + m - 2))
     
            System.out.println("Not possible");
     
        else {
     
            int result;
     
            // for the 1st case
            if (k < Math.max(m, n) - 1)
            {
                result = Math.max(m * (n / (k + 1)),
                         n * (m / (k + 1)));
            }
     
            // for the second case
            else {
     
                result = Math.max(m / (k - n + 2),
                         n / (k - m + 2));
            }
     
            // print final result
            System.out.println(result);
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 3, m = 4, k = 1;
        max_area(n, m, k);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 code for Maximum of smallest
# possible area that can get with
# exactly k cut of given rectangular
 
def max_area(n,m,k):
    if (k > (n + m - 2)):
        print("Not possible")
    else:
        # for the 1st case
        if (k < max(m,n) - 1):
            result = max(m * (n / (k + 1)), n * (m / (k + 1)));
 
        # for the second case
        else:
            result = max(m / (k - n + 2), n / (k - m + 2));
 
        # print final result
        print(result)
 
# driver code
n = 3
m = 4
k = 1
 
max_area(n, m, k)
 
# This code is contributed
# by Azkia Anam.


C#
// C# code for Maximum of smallest
// possible area that can get with
// exactly k cut of given rectangular
using System;
 
class GFG {
     
    //Utility Function
    static void max_area(int n, int m, int k)
    {
        if (k > (n + m - 2))
     
            Console.WriteLine("Not possible");
     
        else {
     
            int result;
     
            // for the 1st case
            if (k < Math.Max(m, n) - 1)
            {
                result = Math.Max(m * (n / (k + 1)),
                        n * (m / (k + 1)));
            }
     
            // for the second case
            else {
     
                result = Math.Max(m / (k - n + 2),
                        n / (k - m + 2));
            }
     
            // print final result
            Console.WriteLine(result);
        }
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 3, m = 4, k = 1;
         
        max_area(n, m, k);
    }
}
 
// This code is contributed by vt_m.


PHP
 ($n + $m - 2))
 
        echo "Not possible" ,"\n";
 
    else
    {
        $result;
 
        // for the 1st case
        if ($k < max($m, $n) - 1)
        {
 
            $result = max($m * ($n / ($k + 1)),
                          $n * ($m / ($k + 1)));
        }
 
        // for the second case
        else
        {
 
            $result = max($m / ($k - $n + 2),
                          $n / ($k - $m + 2));
        }
 
        // print final result
    echo $result ,"\n";
    }
}
 
// Driver Code
$n = 3; $m = 4; $k = 1;
max_area($n, $m, $k);
 
// This code is contributed by ajit
?>


Javascript


输出 :

6