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

📅  最后修改于: 2021-05-13 22:43:17             🧑  作者: Mango

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

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

例子:

方法:

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

  • 以降序对数组A []进行排序。
  • 为了使总和最大化,请从排序数组中添加前K个元素, B []中的索引对应于该元素包含1 。减去其余的此类元素。
  • A []中与包含0的B []中的索引相对应的所有值相加。

下面是上述方法的实现:

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)