给定大小为N的数组arr [] ,它表示每个卡上的初始编号;给定大小为M的二维数组B [] [] ,其中M表示需要执行的操作数。在每次操作中,最多选择B [j] [0]个卡(可能为零),然后用B [j] [1]替换写在每个所选卡上的整数。任务是在M次操作后找到最大可能的总和。
例子:
Input: arr[] = {5, 1, 4}, B[][] = {{2, 3}, {1, 5}}
Output: 14
Replacing 1 with 5 and the sum becomes
5 + 5 + 4 = 14 which is the maximum possible.
Input: arr[] = {100, 100}, B[][] = {{2, 99}}
Output: 200
方法:贪婪的方法在这里适用。以要替换的数字升序对数组arr []进行排序,并以降序对数组B [] []进行排序。然后尝试用B [] []的未使用卡之一替换arr []的最后一张未替换卡。最后,打印最大化的总和。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum
// possible sum after M operations
int max_sum(int a[], int n, int b[][2], int m)
{
// Sort the array a in
// increasing order
sort(a, a + n);
// Place all replacable cards in B
vector > B;
for (int i = 0; i < m; i++)
B.push_back({ b[i][1], b[i][0] });
// Sort vector B in decreasing order
sort(B.rbegin(), B.rend());
// To store last unused card of a
int left = 0;
// Try to apply all m operations
for (int i = 0; i < m; i++) {
int x = B[i].first, y = B[i].second;
// Try for all applicable cards
for (int j = 0; j < y; j++) {
// If current number on card is
// less than applicable card
if (a[left] < x) {
a[left] = x;
left++;
if (left == n)
break;
}
else
break;
}
}
// To store the maximum
// possible sum
int ans = 0;
// Calculate the maximum
// possible sum
for (int i = 0; i < n; i++)
ans += a[i];
// Return the required answer
return ans;
}
// Driver code
int main()
{
int a[] = { 5, 1, 4 };
int n = sizeof(a) / sizeof(a[0]);
int b[][2] = { { 2, 3 }, { 1, 5 } };
int m = sizeof(b) / sizeof(b[0]);
cout << max_sum(a, n, b, m);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
// User defined Pair class
class Pair {
int x;
int y;
// Constructor
public Pair(int x, int y)
{
this.x = x;
this.y = y;
}
}
// class to define user defined conparator
class Sort {
static void sort(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator() {
@Override public int compare(Pair p1, Pair p2)
{
return p1.x - p2.x;
}
});
}
}
public class Main
{
// Function to return the maximum
// possible sum after M operations
static int max_sum(int[] a, int n,
int[][] b, int m)
{
// Sort the array a in
// increasing order
Arrays.sort(a);
// Place all replacable cards in B
Pair B[] = new Pair[m];
for(int i = 0; i < m; i++)
B[i] = new Pair(b[i][1], b[i][0]);
// Sort vector B in decreasing order
Sort obj = new Sort();
obj.sort(B, m);
// To store last unused card of a
int left = 0;
// Try to apply all m operations
for(int i = m-1; i >= 0; i--)
{
int x = B[i].x, y = B[i].y;
// Try for all applicable cards
for(int j = 0; j < y; j++)
{
// If current number on card is
// less than applicable card
if (a[left] < x)
{
a[left] = x;
left++;
if (left == n)
break;
}
else
break;
}
}
// To store the maximum
// possible sum
int ans = 0;
// Calculate the maximum
// possible sum
for(int i = 0; i < n; i++)
ans += a[i];
// Return the required answer
return ans;
}
// Driver code
public static void main(String[] args) {
int[] a = { 5, 1, 4 };
int n = a.length;
int[][] b = { { 2, 3 }, { 1, 5 } };
int m = 2;
System.out.println(max_sum(a, n, b, m));
}
}
// This code is contributed by divyesh072019.
Python3
# Python3 implementation of the approach
# Function to return the maximum
# possible sum after M operations
def max_sum(a, n, b, m) :
# Sort the array a in
# increasing order
a.sort();
# Place all replacable cards in B
B = [];
for i in range(m) :
B.append([b[i][1], b[i][0]]);
# Sort vector B in decreasing order
B.sort(reverse = True)
# To store last unused card of a
left = 0;
# Try to apply all m operations
for i in range(m) :
x = B[i][0];
y = B[i][1];
# Try for all applicable cards
for j in range(y) :
# If current number on card is
# less than applicable card
if (a[left] < x) :
a[left] = x;
left += 1;
if (left == n) :
break;
else :
break;
# To store the maximum
# possible sum
ans = 0;
# Calculate the maximum
# possible sum
for i in range(n) :
ans += a[i];
# Return the required answer
return ans;
# Driver code
if __name__ == "__main__" :
a = [5, 1, 4];
n = len(a);
b = [[2, 3], [1, 5]];
m = len(b);
print(max_sum(a, n, b, m));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG{
// Function to return the maximum
// possible sum after M operations
static int max_sum(int[] a, int n,
int[,] b, int m)
{
// Sort the array a in
// increasing order
Array.Sort(a);
// Place all replacable cards in B
List> B = new List>();
for(int i = 0; i < m; i++)
B.Add(new Tuple(b[i, 1], b[i, 0]));
// Sort vector B in decreasing order
B.Sort();
B.Reverse();
// To store last unused card of a
int left = 0;
// Try to apply all m operations
for(int i = 0; i < m; i++)
{
int x = B[i].Item1, y = B[i].Item2;
// Try for all applicable cards
for(int j = 0; j < y; j++)
{
// If current number on card is
// less than applicable card
if (a[left] < x)
{
a[left] = x;
left++;
if (left == n)
break;
}
else
break;
}
}
// To store the maximum
// possible sum
int ans = 0;
// Calculate the maximum
// possible sum
for(int i = 0; i < n; i++)
ans += a[i];
// Return the required answer
return ans;
}
// Driver code
static void Main()
{
int[] a = { 5, 1, 4 };
int n = a.Length;
int[,] b = { { 2, 3 }, { 1, 5 } };
int m = 2;
Console.WriteLine(max_sum(a, n, b, m));
}
}
// This code is contributed by divyeshrabadiya07
输出:
14