📌  相关文章
📜  对 N 张卡进行 M 次操作后的最大可能和

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

给定一个大小为N的数组arr[]代表每张卡片上的初始数字,并给定一个大小为M 的二维数组B[][] ,其中M代表需要执行的操作数。在每个操作中,最多选择B[j][0]张卡片(可能为零)并用B[j][1]替换写在每张所选卡片上的整数。任务是找到M 次操作后的最大可能和。

例子:

方法:此处适用贪婪方法。将数组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


Javascript


输出:
14

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程