📌  相关文章
📜  使用给定操作最大化给定数组的总和

📅  最后修改于: 2021-10-25 06:24:19             🧑  作者: Mango

给定两个数组A[]B[]N 个整数和一个整数K 组成,任务是通过以下操作最大化从数组A[]计算的总和:

  • 对于B[] 中包含0 的每个索引,将A[]中的相应索引添加到总和中
  • 对于B[] 中包含1 的每个索引,将A[]中相应索引处的值添加到最多K 个此类索引的总和中。对于剩余的索引,从总和中减去。

例子:

方法:

请按照以下步骤解决问题:

  • 按降序对数组 A[] 进行排序。
  • 要使总和最大化,请添加与B[]中的索引包含1对应的已排序数组中的前K 个元素。减去剩余的此类元素。
  • A[] 中B[] 中包含0的索引对应的所有值加到总和中。

下面是上述方法的实现:

C++
// C++ Program to maximise the
// sum of the given array
#include 
using namespace std;
 
// Comparator to sort the array
// in ascending order
bool compare(pair p1,
             pair p2)
{
    return p1.first > p2.first;
}
 
// Function to maximise the sum of
// the given array
int maximiseScore(int A[], int B[],
                  int K, int N)
{
 
    // Stores {A[i], B[i]} pairs
    vector > pairs(N);
    for (int i = 0; i < N; i++) {
        pairs[i] = make_pair(A[i], B[i]);
    }
 
    // Sort in descending order of the
    // values in the array A[]
    sort(pairs.begin(), pairs.end(), compare);
 
    // Stores the maximum sum
    int sum = 0;
    for (int i = 0; i < N; i++) {
 
        // If B[i] is equal to 0
        if (pairs[i].second == 0) {
 
            // Simply add A[i] to the sum
            sum += pairs[i].first;
        }
 
        else if (pairs[i].second == 1) {
 
            // Add the highest K numbers
            if (K > 0) {
                sum += pairs[i].first;
                K--;
            }
 
            // Subtract from the sum
            else {
                sum -= pairs[i].first;
            }
        }
    }
 
    // Return the sum
    return sum;
}
 
// Driver Code
int main()
{
 
    int A[] = { 5, 4, 6, 2, 8 };
    int B[] = { 1, 0, 1, 1, 0 };
    int K = 2;
    int N = sizeof(A) / sizeof(int);
    cout << maximiseScore(A, B, K, N);
    return 0;
}


Java
// Java program to maximise the
// sum of the given array
import java.util.*;
 
class Pair implements Comparable
{
    int first, second;
    Pair(int x, int y)
    {
        first = x;
        second = y;
    }
    public int compareTo(Pair p)
    {
        return p.first - first;
    }
}
 
class GFG{
     
// Function to maximise the sum of
// the given array
static int maximiseScore(int A[], int B[],
                         int K, int N)
{
 
    // Stores {A[i], B[i]} pairs
    ArrayList pairs = new ArrayList<>();
    for(int i = 0; i < N; i++)
    {
        pairs.add(new Pair(A[i], B[i]));
    }
 
    // Sort in descending order of the
    // values in the array A[]
    Collections.sort(pairs);
 
    // Stores the maximum sum
    int sum = 0;
    for(int i = 0; i < N; i++)
    {
         
        // If B[i] is equal to 0
        if (pairs.get(i).second == 0)
        {
             
            // Simply add A[i] to the sum
            sum += pairs.get(i).first;
        }
 
        else if (pairs.get(i).second == 1)
        {
             
            // Add the highest K numbers
            if (K > 0)
            {
                sum += pairs.get(i).first;
                K--;
            }
 
            // Subtract from the sum
            else
            {
                sum -= pairs.get(i).first;
            }
        }
    }
 
    // Return the sum
    return sum;
}
 
// Driver Code
public static void main(String[] args)
{
 
    int A[] = { 5, 4, 6, 2, 8 };
    int B[] = { 1, 0, 1, 1, 0 };
    int K = 2;
    int N = A.length;
     
    System.out.print(maximiseScore(A, B, K, N));
}
}
 
// This code is contributed by jrishabh99


Python3
# Python Program to maximise the
# sum of the given array
 
# Comparator to sort the array
# in ascending order
def compare(p1, p2):
    return p1[0] > p2[0]
 
# Function to maximise the sum of
# the given array
def maximiseScore(A, B, K, N):
     
    # Stores {A[i], B[i]} pairs
    pairs = []
    for i in range(N):
        pairs.append([A[i], B[i]])
     
    # Sort in descending order of the
    # values in the array A[]
    pairs.sort(key = lambda x:x[0], reverse = True)
 
    # Stores the maximum sum
    Sum = 0
 
    for i in range(N):
       
        # If B[i] is equal to 0
        if(pairs[i][1] == 0):
           
            # Simply add A[i] to the sum
            Sum += pairs[i][0]
        elif(pairs[i][1] == 1):
             
            # Add the highest K numbers
            if(K > 0):
                Sum += pairs[i][0]
                K -= 1
                 
            # Subtract from the sum
            else:
                Sum -= pairs[i][0]
     
    # Return the sum
    return Sum
 
# Driver Code
A = [5, 4, 6, 2, 8]
B = [1, 0, 1, 1, 0]
K = 2
N = len(A)
print(maximiseScore(A, B, K, N))
 
# This code is contributed by avanitrachhadiya2155


输出:
21

时间复杂度: O(N*log(N))
辅助空间: O(N)

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