📌  相关文章
📜  对于 Q 查询,通过将最多 L 个元素替换为 R 来最大化数组总和

📅  最后修改于: 2022-05-13 01:56:05.972000             🧑  作者: Mango

对于 Q 查询,通过将最多 L 个元素替换为 R 来最大化数组总和

给定一个由N个整数组成的数组arr[]和一个由M对类型为{L, R}的数组Query[][] ,任务是通过执行查询Query[][找到数组的最大和]这样对于每个查询{L, R}最多将 L 数组元素替换为R

例子:

方法:可以在贪婪方法的帮助下解决给定的问题。最大化数组总和的主要思想是执行查询以将最小数量增加到最大值,因为操作的顺序无关紧要,因为它们彼此独立。请按照以下步骤解决给定的问题:

  • 维护一个最小堆优先级队列并存储所有数组元素。
  • 遍历给定的查询数组Q[][]并为每个查询{L, R}执行以下步骤:
    • 最多改变 L个元素的值小于 R 到R的值,从最小值开始。
    • 执行上述操作,弹出小于R的元素并将R推入优先级队列中的位置。
  • 完成上述步骤后,将存储在优先级队列中的值的总和打印为最大总和。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum array
// sum after performing M queries
void maximumArraySumWithMQuery(
    int arr[], vector >& Q,
    int N, int M)
{
 
    // Maintain a min-heap Priority-Queue
    priority_queue,
                   greater >
        pq;
 
    // Push all the elements in the
    // priority queue
    for (int i = 0; i < N; i++) {
        pq.push(arr[i]);
    }
 
    // Iterate through M Operations
    for (int i = 0; i < M; i++) {
 
        // Iterate through the total
        // possible changes allowed
        // and maximize the array sum
        int l = Q[i][0];
        int r = Q[i][1];
 
        for (int j = 0; j < l; j++) {
 
            // Change the value of elements
            // less than r to r, starting
            // from the smallest
            if (pq.top() < r) {
                pq.pop();
                pq.push(r);
            }
 
            // Break if current element >= R
            else {
                break;
            }
        }
    }
 
    // Find the resultant maximum sum
    int ans = 0;
 
    while (!pq.empty()) {
        ans += pq.top();
        pq.pop();
    }
 
    // Print the sum
    cout << ans;
}
 
// Driver Code
int main()
{
    int N = 3, M = 2;
    int arr[] = { 5, 1, 4 };
    vector > Query
        = { { 2, 3 }, { 1, 5 } };
 
    maximumArraySumWithMQuery(
        arr, Query, N, M);
 
    return 0;
}


Java
// Java program for the above approach
 
import java.util.PriorityQueue;
 
class GFG {
 
    // Function to find the maximum array
    // sum after performing M queries
    public static void maximumArraySumWithMQuery(int arr[], int[][] Q, int N, int M) {
 
        // Maintain a min-heap Priority-Queue
        PriorityQueue pq = new PriorityQueue();
 
        // Push all the elements in the
        // priority queue
        for (int i = 0; i < N; i++) {
            pq.add(arr[i]);
        }
 
        // Iterate through M Operations
        for (int i = 0; i < M; i++) {
 
            // Iterate through the total
            // possible changes allowed
            // and maximize the array sum
            int l = Q[i][0];
            int r = Q[i][1];
 
            for (int j = 0; j < l; j++) {
 
                // Change the value of elements
                // less than r to r, starting
                // from the smallest
                if (pq.peek() < r) {
                    pq.remove();
                    pq.add(r);
                }
 
                // Break if current element >= R
                else {
                    break;
                }
            }
        }
 
        // Find the resultant maximum sum
        int ans = 0;
 
        while (!pq.isEmpty()) {
            ans += pq.peek();
            pq.remove();
        }
 
        // Print the sum
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String args[]) {
        int N = 3, M = 2;
        int arr[] = { 5, 1, 4 };
        int[][] Query = { { 2, 3 }, { 1, 5 } };
 
        maximumArraySumWithMQuery(arr, Query, N, M);
 
    }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# Python program for the above approach
from queue import PriorityQueue
 
# Function to find the maximum array
# sum after performing M queries
def maximumArraySumWithMQuery(arr, Q, N, M):
 
    # Maintain a min-heap Priority-Queue
    pq = PriorityQueue()
 
    # Push all the elements in the
    # priority queue
    for i in range(N):
        pq.put(arr[i])
 
    # Iterate through M Operations
    for i in range(M):
 
        # Iterate through the total
        # possible changes allowed
        # and maximize the array sum
        l = Q[i][0];
        r = Q[i][1];
 
        for j in range(l):
 
            # Change the value of elements
            # less than r to r, starting
            # from the smallest
            if (pq.queue[0] < r):
                pq.get();
                pq.put(r);
 
            # Break if current element >= R
            else:
                break
         
    # Find the resultant maximum sum
    ans = 0;
     
    while ( not pq.empty() ):
        ans += pq.queue[0];
        pq.get();
 
    # Print the sum
    print(ans)
 
# Driver Code
N = 3
M = 2
arr = [5, 1, 4]
Query = [[2, 3], [1, 5]]
 
maximumArraySumWithMQuery(arr, Query, N, M)
 
# This code is contributed by gfgking.


输出:
14

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