📜  最多可购买芒果数量

📅  最后修改于: 2021-09-16 11:16:40             🧑  作者: Mango

鉴于两个整数WC,代表西瓜和硬币的数量,任务是找到可以给每个芒果加1枚西瓜和X硬币和Y币可以再赚卖西瓜买芒果的最大数量。

例子:

方法:这个问题可以使用二分查找来解决。这个想法是在搜索空间中找到最大数量的芒果。请按照以下步骤解决问题:

  • 将变量ans初始化为0以存储所需的结果。
  • 将两个变量l初始化为0r初始化为W来存储二分搜索的搜索空间的边界区域。
  • 循环而l≤r并执行以下步骤:
    • 将中间值存储在变量mid 中(l+r)/2
    • 使用WCxy的给定值检查是否可以购买中等数量的芒果。
    • 如果属实,那么更新中期,并通过更新l中旬+ 1月中的右侧部分搜索。否则,将r的值更新为mid-1
  • 打印ans的值作为结果。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if mid number
// of mangoes can be bought
bool check(int n, int m, int x, int y, int vl)
{
    // Store the coins
    int temp = m;
 
    // If watermelons needed are greater
    // than given watermelons
    if (vl > n)
        return false;
 
    // Store remaining watermelons if vl
    // watermelons are used to buy mangoes
    int ex = n - vl;
 
    // Store the value of coins if these
    // watermelon get sold
    ex *= y;
 
    // Increment coins by ex
    temp += ex;
 
    // Number of mangoes that can be buyed
    // if only x coins needed for one mango
    int cr = temp / x;
 
    // If the condition is satisfied,
    // return true
    if (cr >= vl)
        return true;
 
    // Otherwise return false
    return false;
}
 
// Function to find the maximum number of mangoes
// that can be bought by selling watermelons
int maximizeMangoes(int n, int m, int x, int y)
{
    // Initialize the boundary values
    int l = 0, r = n;
 
    // Store the required result
    int ans = 0;
 
    // Binary Search
    while (l <= r) {
 
        // Store the mid value
        int mid = l + (r - l) / 2;
 
        // Check if it is possible to
        // buy mid number of mangoes
        if (check(n, m, x, y, mid)) {
            ans = mid;
            l = mid + 1;
        }
 
        // Otherwise, update r to mid -1
        else
            r = mid - 1;
    }
 
    // Return the result
    return ans;
}
 
// Driver Code
int main()
{
    // Given Input
    int W = 4, C = 8, x = 4, y = 4;
 
    // Function Call
    cout << maximizeMangoes(W, C, x, y);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if mid number
// of mangoes can be bought
static boolean check(int n, int m, int x,
                     int y, int vl)
{
     
    // Store the coins
    int temp = m;
 
    // If watermelons needed are greater
    // than given watermelons
    if (vl > n)
        return false;
 
    // Store remaining watermelons if vl
    // watermelons are used to buy mangoes
    int ex = n - vl;
 
    // Store the value of coins if these
    // watermelon get sold
    ex *= y;
 
    // Increment coins by ex
    temp += ex;
 
    // Number of mangoes that can be buyed
    // if only x coins needed for one mango
    int cr = temp / x;
 
    // If the condition is satisfied,
    // return true
    if (cr >= vl)
        return true;
 
    // Otherwise return false
    return false;
}
 
// Function to find the maximum number
// of mangoes that can be bought by
// selling watermelons
static int maximizeMangoes(int n, int m,
                           int x, int y)
{
     
    // Initialize the boundary values
    int l = 0, r = n;
 
    // Store the required result
    int ans = 0;
 
    // Binary Search
    while (l <= r)
    {
         
        // Store the mid value
        int mid = l + (r - l) / 2;
 
        // Check if it is possible to
        // buy mid number of mangoes
        if (check(n, m, x, y, mid))
        {
            ans = mid;
            l = mid + 1;
        }
 
        // Otherwise, update r to mid -1
        else
            r = mid - 1;
    }
 
    // Return the result
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Input
    int W = 4, C = 8, x = 4, y = 4;
 
    // Function Call
    System.out.println(maximizeMangoes(W, C, x, y));
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to check if mid number
# of mangoes can be bought
def check(n, m, x, y, vl):
     
    # Store the coins
    temp = m
 
    # If watermelons needed are greater
    # than given watermelons
    if (vl > n):
        return False
 
    # Store remaining watermelons if vl
    # watermelons are used to buy mangoes
    ex = n - vl
 
    # Store the value of coins if these
    # watermelon get sold
    ex *= y
 
    # Increment coins by ex
    temp += ex
 
    # Number of mangoes that can be buyed
    # if only x coins needed for one mango
    cr = temp // x
 
    # If the condition is satisfied,
    # return true
    if (cr >= vl):
        return True
 
    # Otherwise return false
    return False
 
# Function to find the maximum number of mangoes
# that can be bought by selling watermelons
def maximizeMangoes(n, m, x, y):
     
    # Initialize the boundary values
    l = 0
    r = n
 
    # Store the required result
    ans = 0
 
    # Binary Search
    while (l <= r):
 
        # Store the mid value
        mid = l + (r - l) // 2
 
        # Check if it is possible to
        # buy mid number of mangoes
        if (check(n, m, x, y, mid)):
            ans = mid
            l = mid + 1
 
        # Otherwise, update r to mid -1
        else:
            r = mid - 1
 
    # Return the result
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    W = 4
    C = 8
    x = 4
    y = 4
 
    # Function Call
    print(maximizeMangoes(W, C, x, y))
     
# This code is contributed by bgangwar59


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if mid number
// of mangoes can be bought
static bool check(int n, int m, int x,
                  int y, int vl)
{
     
    // Store the coins
    int temp = m;
 
    // If watermelons needed are greater
    // than given watermelons
    if (vl > n)
        return false;
 
    // Store remaining watermelons if vl
    // watermelons are used to buy mangoes
    int ex = n - vl;
 
    // Store the value of coins if these
    // watermelon get sold
    ex *= y;
 
    // Increment coins by ex
    temp += ex;
 
    // Number of mangoes that can be buyed
    // if only x coins needed for one mango
    int cr = temp / x;
 
    // If the condition is satisfied,
    // return true
    if (cr >= vl)
        return true;
 
    // Otherwise return false
    return false;
}
 
// Function to find the maximum number of mangoes
// that can be bought by selling watermelons
static int maximizeMangoes(int n, int m, int x, int y)
{
     
    // Initialize the boundary values
    int l = 0, r = n;
 
    // Store the required result
    int ans = 0;
 
    // Binary Search
    while (l <= r)
    {
         
        // Store the mid value
        int mid = l + (r - l) / 2;
 
        // Check if it is possible to
        // buy mid number of mangoes
        if (check(n, m, x, y, mid))
        {
            ans = mid;
            l = mid + 1;
        }
 
        // Otherwise, update r to mid -1
        else
            r = mid - 1;
    }
 
    // Return the result
    return ans;
}
 
// Driver Code
public static void Main()
{
     
    // Given Input
    int W = 4, C = 8, x = 4, y = 4;
 
    // Function Call
    Console.Write(maximizeMangoes(W, C, x, y));
}
}
                
// This code is contributed by ukasp


Javascript


输出:
3

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