📌  相关文章
📜  使用给定的运算将给定的两个整数转换为零的成本降至最低

📅  最后修改于: 2021-05-06 08:48:36             🧑  作者: Mango

给定两个整数XY以及两个值cost1cost2 ,任务是通过执行以下两种类型的操作以最小的成本转换等于零的给定两个数字:

  • 成本1将其中任何一个增加或减少1。
  • cost2将它们两者都增加或减少1。

例子:

方法:
解决问题的最佳方法是:

  • 通过使用第一个操作,将X和Y的最大值减小到最小值。这使成本增加了abs(X-Y)* cost1
  • 然后,使用第二个操作将X和Y都减小为0。这使成本增加了(X,Y)* cost2的最小值

下面是上述方法的实现:

C++
// C++ implementation to find the minimum
// cost to make the two integers equal
// to zero using given operations
#include 
using namespace std;
 
// Function to find out the minimum cost to
// make two number X and Y equal to zero
int makeZero(int x, int y, int a, int b)
{
     
    // If x is greater than y then swap
    if(x > y)
       x = y,
       y = x;
     
    // Cost of making y equal to x
    int tot_cost = (y - x) * a;
 
    // Cost if we choose 1st operation
    int cost1 = 2 * x * a;
     
    // Cost if we choose 2nd operation
    int cost2 = x * b;
     
    // Total cost
    tot_cost += min(cost1, cost2);
     
    cout << tot_cost;
}
 
// Driver code
int main()
{
    int X = 1, Y = 3;
    int cost1 = 391, cost2 = 555;
 
    makeZero(X, Y, cost1, cost2);
}
 
// This code is contributed by coder001


Java
// Java implementation to find the minimum
// cost to make the two integers equal
// to zero using given operations
import java.util.*;
class GFG{
     
// Function to find out the minimum cost to
// make two number X and Y equal to zero
static void makeZero(int x, int y, int a, int b)
{
     
    // If x is greater than y then swap
    if(x > y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
     
    // Cost of making y equal to x
    int tot_cost = (y - x) * a;
 
    // Cost if we choose 1st operation
    int cost1 = 2 * x * a;
     
    // Cost if we choose 2nd operation
    int cost2 = x * b;
     
    // Total cost
    tot_cost += Math.min(cost1, cost2);
     
    System.out.print(tot_cost);
}
 
// Driver code
public static void main(String args[])
{
    int X = 1, Y = 3;
    int cost1 = 391, cost2 = 555;
 
    makeZero(X, Y, cost1, cost2);
}
}
 
// This code is contributed by Code_Mech


Python3
# Python3 implementation to find the
# minimum cost to make the two integers
# equal to zero using given operations
 
 
# Function to find out the minimum cost to make
# two number X and Y equal to zero
def makeZero(x, y, a, b):
     
    # If x is greater than y then swap
    if(x > y):
        x, y = y, x
     
    # Cost of making y equal to x
    tot_cost = (y - x) * a
 
    # Cost if we choose 1st operation
    cost1 = 2 * x * a
     
    # Cost if we choose 2nd operation
    cost2 = x * b
     
    # Total cost
    tot_cost+= min(cost1, cost2)
     
    print(tot_cost)
     
 
if __name__ =="__main__":
     
    X, Y = 1, 3
 
    cost1, cost2 = 391, 555
 
    makeZero(X, Y, cost1, cost2)


C#
// C# implementation to find the minimum
// cost to make the two integers equal
// to zero using given operations
using System;
 
class GFG{
     
// Function to find out the minimum cost to
// make two number X and Y equal to zero
static void makeZero(int x, int y, int a, int b)
{
     
    // If x is greater than y then swap
    if(x > y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
     
    // Cost of making y equal to x
    int tot_cost = (y - x) * a;
 
    // Cost if we choose 1st operation
    int cost1 = 2 * x * a;
     
    // Cost if we choose 2nd operation
    int cost2 = x * b;
     
    // Total cost
    tot_cost += Math.Min(cost1, cost2);
     
    Console.Write(tot_cost);
}
 
// Driver code
public static void Main()
{
    int X = 1, Y = 3;
    int cost1 = 391, cost2 = 555;
 
    makeZero(X, Y, cost1, cost2);
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
1337

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