给定两个包含 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
?>
Javascript
输出:
21
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。