📜  以这种方式选择数字以使金额最大化

📅  最后修改于: 2021-04-21 23:39:43             🧑  作者: Mango

给定两个N个数字的数组A1和A2。有两个人A和B从N中选择数字。如果A选择第i个数字,则将向他支付A1 [i]的金额;如果B选择第i个数字,则将向他支付A2 [i] ]金额,但A不能选择X个以上的数字,而B不能选择Y个以上的数字。任务是选择N个数字,以使总金额最终达到最大。

注意: X + Y> = N

Examples:
Input: N = 5, X = 3, Y = 3 
       A1[] = {1, 2, 3, 4, 5}, 
       A2= {5, 4, 3, 2, 1}
Output: 21 
B will take the first 3 orders and A 
will take the last two orders. 

Input: N = 2, X = 1, Y = 1 
       A1[] = {10, 10}, A2= {20, 20}
Output: 30 

方法:让我们创建一个新的数组C,使得C [i] = A2 [i] – A1 [i] 。现在,我们将按降序对数组C进行排序。请注意,条件X + Y> = N保证了我们能够将数字分配给任何一个人。假设对于某些i,A1 [i]> A2 [i],并且您已将订单分配给B,则由于此分配而导致的损失为C [i]。同样,对于某些i,A2 [i]> A1 [i],并且您给A分配了一个数字,由于此分配而导致的损失为C [i]。由于我们希望将遇到的损失减至最小,因此最好处理可能损失较高的数字,因为我们可以尝试减少起始部分的损失。在分配损失较小的数字之后,没有必要选择损失较大的数字。因此,我们首先将所有数字最初分配给A,然后贪婪地从中减去损失。一旦分配的订单号在X之下,则我们将存储它的最大值。

下面是上述方法的实现:

C++
// C++ program to maximize profit
  
#include 
using namespace std;
  
// Function that maximizes the sum
int maximize(int A1[], int A2[], int n,
             int x, int y)
{
    // Array to store the loss
    int c[n];
  
    // Initial sum
    int sum = 0;
  
    // Generate the array C
    for (int i = 0; i < n; i++) {
        c[i] = A2[i] - A1[i];
        sum += A1[i];
    }
  
    // Sort the array elements
    // in descending order
    sort(c, c + n, greater());
  
    // Variable to store the answer
    int maxi = -1;
  
    // Iterate in the array, C
    for (int i = 0; i < n; i++) {
  
        // Subtract the loss
        sum += c[i];
  
        // Check if X orders are going
        // to be used
        if (i + 1 >= (n - x))
            maxi = max(sum, maxi);
    }
  
    return maxi;
}
  
// Driver Code
int main()
{
    int A1[] = { 1, 2, 3, 4, 5 };
    int A2[] = { 5, 4, 3, 2, 1 };
  
    int n = 5;
    int x = 3, y = 3;
  
    cout << maximize(A1, A2, n, x, y);
  
    return 0;
}


Java
// Java program to maximize profit
import java.util.*;
  
class GFG
{
  
// Function that maximizes the sum
static int maximize(int A1[], int A2[], int n,
            int x, int y)
{
    // Array to store the loss
    int[] c = new int[n];
  
    // Initial sum
    int sum = 0;
  
    // Generate the array C
    for (int i = 0; i < n; i++) 
    {
        c[i] = A2[i] - A1[i];
        sum += A1[i];
    }
  
    // Sort the array elements
    // in descending order
int temp;
for(int i = 0; i < n - 1; i++)
{
    if(c[i] < c[i+1])
    {
        temp = c[i];
        c[i] = c[i + 1];
        c[i + 1] = temp;
    }
}
  
    // Variable to store the answer
    int maxi = -1;
  
    // Iterate in the array, C
    for (int i = 0; i < n; i++) 
    {
  
        // Subtract the loss
        sum += c[i];
  
        // Check if X orders are going
        // to be used
        if (i + 1 >= (n - x))
            maxi = Math.max(sum, maxi);
    }
  
    return maxi;
}
  
// Driver Code
public static void main(String args[])
{
    int A1[] = { 1, 2, 3, 4, 5 };
    int A2[] = { 5, 4, 3, 2, 1 };
  
    int n = 5;
    int x = 3, y = 3;
  
    System.out.println(maximize(A1, A2, n, x, y));
}
}
  
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 program to maximize profit
  
# Function that maximizes the Sum
def maximize(A1, A2, n, x, y):
  
    # Array to store the loss
    c = [0 for i in range(n)]
  
    # Initial Sum
    Sum = 0
  
    # Generate the array C
    for i in range(n):
        c[i] = A2[i] - A1[i]
        Sum += A1[i]
      
    # Sort the array elements
    # in descending order
    c.sort()
    c = c[::-1]
  
    # Variable to store the answer
    maxi = -1
  
    # Iterate in the array, C
    for i in range(n):
  
        # Subtract the loss
        Sum += c[i]
  
        # Check if X orders are going
        # to be used
        if (i + 1 >= (n - x)):
            maxi = max(Sum, maxi)
  
    return maxi
      
# Driver Code
A1 = [ 1, 2, 3, 4, 5 ]
A2 = [ 5, 4, 3, 2, 1 ]
  
n = 5
x, y = 3, 3
  
print(maximize(A1, A2, n, x, y))
  
# This code is contributed
# by Mohit Kumar


C#
// C# program to maximize profit
using System;
  
class GFG
{
      
    // Function that maximizes the sum
    static int maximize(int [] A1, int [] A2, int n,
                                    int x, int y)
    {
        // Array to store the loss
        int [] c = new int[n];
      
        // Initial sum
        int sum = 0;
      
        // Generate the array C
        for (int i = 0; i < n; i++) 
        {
            c[i] = A2[i] - A1[i];
            sum += A1[i];
        }
      
        // Sort the array elements
        // in descending order
            int temp;
        for(int i = 0; i < n - 1; i++)
        {
            if(c[i] < c[i+1])
            {
                temp = c[i];
                c[i] = c[i + 1];
                c[i + 1] = temp;
            }
        }
      
        // Variable to store the answer
        int maxi = -1;
      
        // Iterate in the array, C
        for (int i = 0; i < n; i++) 
        {
      
            // Subtract the loss
            sum += c[i];
      
            // Check if X orders are going
            // to be used
            if (i + 1 >= (n - x))
                maxi = Math.Max(sum, maxi);
        }
      
        return maxi;
    }
      
    // Driver Code
    public static void Main()
    {
        int [] A1 = { 1, 2, 3, 4, 5 };
        int [] A2 = { 5, 4, 3, 2, 1 };
      
        int n = 5;
        int x = 3, y = 3;
      
        Console.WriteLine(maximize(A1, A2, n, x, y));
    }
}
  
// This code is contributed by ihritik


PHP
= ($n - $x)) 
            $maxi = max($sum, $maxi); 
    } 
  
    return $maxi; 
} 
      
# Driver Code 
$A1 = array( 1, 2, 3, 4, 5 ); 
$A2 = array( 5, 4, 3, 2, 1 ); 
  
$n = 5; 
$x = 3;
$y = 3; 
  
echo maximize($A1, $A2, $n, $x, $y); 
  
// This code is contributed by Ryuga
?>


输出:
21