📌  相关文章
📜  通过最多 M 次减少来最小化可能的两个分数的乘积

📅  最后修改于: 2021-10-27 06:21:29             🧑  作者: Mango

给定两个整数R1、R2表示两个球员的得分, B1、B2 分别表示他们面对的球,任务是找到可以得到的R1 * R2最小值,使得R1R2可以减少M运行满足条件R1 ≥ B1R2 ≥ B2

例子:

方法:可以通过将数量完全减少到它们的极限来获得最小乘积。将R1减小到其极限B1 ,然后尽可能减小R2 (不超过M )。类似地,将R2减少到最多 B2,然后尽可能减少R2 (不超过M )。两种情况下得到的最小乘积就是要求的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Utility function to find the minimum
// product of R1 and R2 possible
int minProductUtil(int R1, int B1, int R2,
                   int B2, int M)
{
    int x = min(R1-B1, M);
     
    M -= x;
     
    // Reaching to its limit
    R1 -= x;
     
    // If M is remaining
    if (M > 0)
    {
        int y = min(R2 - B2, M);
        M -= y;
        R2 -= y;
    }
    return R1 * R2;
}
 
// Function to find the minimum
// product of R1 and R2
int minProduct(int R1, int B1, int R2, int B2, int M)
{
     
    // Case 1 - R1 reduces first
    int res1 = minProductUtil(R1, B1, R2, B2, M);
     
    // Case 2 - R2 reduces first
    int res2 = minProductUtil(R2, B2, R1, B1, M);
 
    return min(res1, res2);
}
 
// Driver Code
int main()
{
     
    // Given Input
    int R1 = 21, B1 = 10, R2 = 13, B2 = 11, M = 3;
     
    // Function Call
    cout << (minProduct(R1, B1, R2, B2, M));
    return 0;
}
 
// This code is contributed by maddler


Java
// Java program for the above approach
import java.io.*;
class GFG{
     
// Utility function to find the minimum
// product of R1 and R2 possible
static int minProductUtil(int R1, int B1, int R2,
                   int B2, int M)
{
    int x = Math.min(R1-B1, M);
     
    M -= x;
     
    // Reaching to its limit
    R1 -= x;
     
    // If M is remaining
    if (M > 0)
    {
        int y = Math.min(R2 - B2, M);
        M -= y;
        R2 -= y;
    }
    return R1 * R2;
}
 
// Function to find the minimum
// product of R1 and R2
static int minProduct(int R1, int B1, int R2, int B2, int M)
{
     
    // Case 1 - R1 reduces first
    int res1 = minProductUtil(R1, B1, R2, B2, M);
     
    // Case 2 - R2 reduces first
    int res2 = minProductUtil(R2, B2, R1, B1, M);
 
    return Math.min(res1, res2);
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given Input
    int R1 = 21, B1 = 10, R2 = 13, B2 = 11, M = 3;
     
    // Function Call
    System.out.print((minProduct(R1, B1, R2, B2, M)));
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python program for the above approach
 
# Utility function to find the minimum
# product of R1 and R2 possible
def minProductUtil(R1, B1, R2, B2, M):
 
    x = min(R1-B1, M)
 
    M -= x
     
    # Reaching to its limit
    R1 -= x
     
    # If M is remaining
    if M > 0:
        y = min(R2-B2, M)
        M -= y
        R2 -= y
 
    return R1 * R2
   
# Function to find the minimum
# product of R1 and R2
def minProduct(R1, B1, R2, B2, M):
     
    # Case 1 - R1 reduces first
    res1 = minProductUtil(R1, B1, R2, B2, M)
     
    # case 2 - R2 reduces first
    res2 = minProductUtil(R2, B2, R1, B1, M)
 
    return min(res1, res2)
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    R1 = 21; B1 = 10; R2 = 13; B2 = 11; M = 3
     
    # Function Call
    print(minProduct(R1, B1, R2, B2, M))


C#
// C# program for the above approach
using System;
class GFG{
     
// Utility function to find the minimum
// product of R1 and R2 possible
static int minProductUtil(int R1, int B1, int R2,
                   int B2, int M)
{
    int x = Math.Min(R1-B1, M);
     
    M -= x;
     
    // Reaching to its limit
    R1 -= x;
     
    // If M is remaining
    if (M > 0)
    {
        int y = Math.Min(R2 - B2, M);
        M -= y;
        R2 -= y;
    }
    return R1 * R2;
}
 
// Function to find the minimum
// product of R1 and R2
static int minProduct(int R1, int B1, int R2, int B2, int M)
{
     
    // Case 1 - R1 reduces first
    int res1 = minProductUtil(R1, B1, R2, B2, M);
     
    // Case 2 - R2 reduces first
    int res2 = minProductUtil(R2, B2, R1, B1, M);
 
    return Math.Min(res1, res2);
}
 
// Driver Code
public static void Main (String[] args)
{
     
    // Given Input
    int R1 = 21, B1 = 10, R2 = 13, B2 = 11, M = 3;
     
    // Function Call
    Console.Write((minProduct(R1, B1, R2, B2, M)));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
220

时间复杂度: O(1)
辅助空间: O(1)