Dados dos números enteros X e Y , y dos valores cost1 y cost2 , la tarea es convertir los dos números dados en cero a un costo mínimo realizando los siguientes dos tipos de operaciones:
- Aumentar o disminuir cualquiera de ellos en 1 al costo1 .
- Aumente o disminuya ambos en 1 al costo2 .
Ejemplos:
Entrada: X = 1, Y = 3, costo1 = 391, costo2 = 555
Salida: 1337
Explicación:
Reduzca Y a 1 usando la primera operación dos veces y convierta X e Y de 1 a 0 usando la segunda operación.
Por lo tanto, el costo total = 391 * 2 + 555 = 1337.Entrada: X = 12, Y = 7, costo1 = 12, costo2 = 7
Salida: 4
Explicación:
Reduzca X a 7 usando la primera operación y luego convierta X e Y a 0 usando la segunda operación.
Por lo tanto, el costo total = 12 * 5 + 7 * 7 = 109
Enfoque:
La forma más óptima de resolver el problema es:
- Reduzca el máximo de X e Y al mínimo usando la primera operación. Esto aumenta el costo en abs(X – Y) * cost1 .
- Luego, reduce tanto X como Y a 0 usando la segunda operación. Esto aumenta el costo por un mínimo de (X, Y) * costo2 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to find the minimum // cost to make the two integers equal // to zero using given operations #include <bits/stdc++.h> 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
<script> // Javascript 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 function makeZero(x, y, a, b) { // If x is greater than y then swap if (x > y) { let temp = x; x = y; y = temp; } // Cost of making y equal to x let tot_cost = (y - x) * a; // Cost if we choose 1st operation let cost1 = 2 * x * a; // Cost if we choose 2nd operation let cost2 = x * b; // Total cost tot_cost += Math.min(cost1, cost2); document.write(tot_cost); } // Driver code let X = 1, Y = 3; let cost1 = 391, cost2 = 555; makeZero(X, Y, cost1, cost2); // This code is contributed by rameshtravel07 </script>
1337
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por ghoshashis545 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA