📌  相关文章
📜  将X和Y之间的绝对差最大增加N个减量

📅  最后修改于: 2021-06-26 12:07:13             🧑  作者: Mango

给定五个整数XYABN ,任务是通过精确地执行N次以下操作来找到XY之间的最大可能绝对差:

  • X的值减1,直到A。
  • Y的值减1,直到B。

注意: (X – A + Y – B)的值必须大于或等于N

例子:

方法:可以使用贪婪技术解决问题。请按照以下步骤解决问题:

  • 初始化一个变量,例如n1,以存储在X上执行的最大操作数。
  • 更新n1 = min(N,X – A)
  • 初始化一个变量,例如n2,以存储在Y上执行的最大操作数。
  • 更新n2 = min(N,Y – B)
  • 初始化变量diff_X_Y_1来存储XY的绝对差,方法是先将X的值精确地减少1 (最小为N(n,n1)) ,然后再将Y的值减少余下的操作次数。
  • 初始化变量diff_X_Y_2来存储XY的绝对差,方法是先将Y的值精确地减少1 (最小N(n,n2)) ,然后再将X的值减少余下的操作次数。
  • 最后,输出max(diff_X_Y_1,diff_X_Y_2)的值

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the absolute difference
// between X and Y with given operations
int AbsDiff(int X, int Y, int A, int B, int N)
{
    // Stores maximum absolute difference
    // between X and Y with given operations
    int maxDiff = 0;
 
    // Stores maximum number of operations
    // performed on X
    int n1 = X - A;
 
    // Update X
    X = X - min(N, n1);
 
    // Decrementing N at most N times
    N = N - min(N, n1);
 
    // Stores maximum number of operations
    // performed on Y
    int n2 = Y - B;
 
    // Update Y
    Y = Y - min(N, n2);
 
    // Update maxDiff
    maxDiff = abs(X - Y);
 
    return maxDiff;
}
 
// Function to find the max absolute difference
// between X and Y with given operations
int maxAbsDiff(int X, int Y, int A, int B, int N)
{
    // Stores absolute difference between
    // X and Y by first decrementing X and then Y
    int diffX_Y_1;
 
    // Stores absolute difference between X
    // and Y first decrementing Y and then X
    int diffX_Y_2;
 
    // Update diffX_Y_1
    diffX_Y_1 = AbsDiff(X, Y, A, B, N);
 
    // Swap X, Y and A, B
    swap(X, Y);
    swap(A, B);
 
    // Update diffX_Y_2
    diffX_Y_2 = AbsDiff(X, Y, A, B, N);
 
    return max(diffX_Y_1, diffX_Y_2);
}
 
// Driver Code
int main()
{
    int X = 10;
    int Y = 10;
    int A = 8;
    int B = 5;
    int N = 3;
    cout << maxAbsDiff(X, Y, A, B, N);
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
     
// Function to find the absolute difference
// between X and Y with given operations
public static int AbsDiff(int X, int Y, int A,
                          int B, int N)
{
     
    // Stores maximum absolute difference
    // between X and Y with given operations
    int maxDiff = 0;
   
    // Stores maximum number of operations
    // performed on X
    int n1 = X - A;
   
    // Update X
    X = X - Math.min(N, n1);
   
    // Decrementing N at most N times
    N = N - Math.min(N, n1);
   
    // Stores maximum number of operations
    // performed on Y
    int n2 = Y - B;
   
    // Update Y
    Y = Y - Math.min(N, n2);
   
    // Update maxDiff
    maxDiff = Math.abs(X - Y);
   
    return maxDiff;
}
   
// Function to find the max absolute difference
// between X and Y with given operations
public static int maxAbsDiff(int X, int Y, int A,
                             int B, int N)
{
     
    // Stores absolute difference between
    // X and Y by first decrementing X and then Y
    int diffX_Y_1;
   
    // Stores absolute difference between X
    // and Y first decrementing Y and then X
    int diffX_Y_2;
   
    // Update diffX_Y_1
    diffX_Y_1 = AbsDiff(X, Y, A, B, N);
   
    // Swap X, Y and A, B
    int temp1 = X;
    X = Y;
    Y = temp1;
     
    int temp2 = A;
    A = B;
    B = temp2;
   
    // Update diffX_Y_2
    diffX_Y_2 = AbsDiff(X, Y, A, B, N);
   
    return Math.max(diffX_Y_1, diffX_Y_2);
}
 
// Driver code
public static void main(String[] args)
{
    int X = 10;
    int Y = 10;
    int A = 8;
    int B = 5;
    int N = 3;
     
    System.out.println(maxAbsDiff(X, Y, A, B, N));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to implement
# the above approach
  
# Function to find the absolute difference
# between X and Y with given operations
def AbsDiff(X, Y, A, B, N):
     
    # Stores maximum absolute difference
    # between X and Y with given operations
    maxDiff = 0
  
    # Stores maximum number of operations
    # performed on X
    n1 = X - A
  
    # Update X
    X = X - min(N, n1)
  
    # Decrementing N at most N times
    N = N - min(N, n1)
  
    # Stores maximum number of operations
    # performed on Y
    n2 = Y - B
  
    # Update Y
    Y = Y - min(N, n2)
  
    # Update maxDiff
    maxDiff = abs(X - Y)
  
    return maxDiff
 
# Function to find the max absolute difference
# between X and Y with given operations
def maxAbsDiff(X, Y, A, B, N):
  
    # Stores absolute difference between
    # X and Y by first decrementing X and then Y
    diffX_Y_1 = AbsDiff(X, Y, A, B, N)
  
    # Swap X, Y and A, B
    temp1 = X
    X = Y
    Y = temp1
  
    temp2 = A
    A = B
    B = temp2
 
    # Stores absolute difference between X
    # and Y first decrementing Y and then X
    diffX_Y_2 = AbsDiff(X, Y, A, B, N)
  
    return max(diffX_Y_1, diffX_Y_2)
 
# Driver Code
X = 10
Y = 10
A = 8
B = 5
N = 3
 
print(maxAbsDiff(X, Y, A, B, N))
 
# This code is contributed by sanjoy_62


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
     
// Function to find the absolute difference
// between X and Y with given operations
public static int AbsDiff(int X, int Y, int A,
                          int B, int N)
{
     
    // Stores maximum absolute difference
    // between X and Y with given operations
    int maxDiff = 0;
   
    // Stores maximum number of operations
    // performed on X
    int n1 = X - A;
   
    // Update X
    X = X - Math.Min(N, n1);
   
    // Decrementing N at most N times
    N = N - Math.Min(N, n1);
     
    // Stores maximum number of operations
    // performed on Y
    int n2 = Y - B;
   
    // Update Y
    Y = Y - Math.Min(N, n2);
   
    // Update maxDiff
    maxDiff = Math.Abs(X - Y);
   
    return maxDiff;
}
   
// Function to find the max absolute difference
// between X and Y with given operations
public static int maxAbsDiff(int X, int Y, int A,
                             int B, int N)
{
     
    // Stores absolute difference between
    // X and Y by first decrementing X and then Y
    int diffX_Y_1;
   
    // Stores absolute difference between X
    // and Y first decrementing Y and then X
    int diffX_Y_2;
   
    // Update diffX_Y_1
    diffX_Y_1 = AbsDiff(X, Y, A, B, N);
     
    // Swap X, Y and A, B
    int temp1 = X;
    X = Y;
    Y = temp1;
     
    int temp2 = A;
    A = B;
    B = temp2;
   
    // Update diffX_Y_2
    diffX_Y_2 = AbsDiff(X, Y, A, B, N);
   
    return Math.Max(diffX_Y_1, diffX_Y_2);
}
 
// Driver code
public static void Main(String[] args)
{
    int X = 10;
    int Y = 10;
    int A = 8;
    int B = 5;
    int N = 3;
     
    Console.WriteLine(maxAbsDiff(X, Y, A, B, N));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
3

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