给定正整数数组arr[] 、整数Q以及大小为Q 的数组X[]和Y[] 。对于数组X[]和Y[] 中的每个元素,我们可以执行以下操作:
- 对于来自数组 X[] 和 Y[] 的每个查询,从数组arr[]中最多选择 X[i] 个元素,并用整数Y[i]替换所有选定的元素。
- 执行 Q 操作后,任务是从数组arr[] 中获取最大和。
例子:
Input: arr[] = {5, 2, 6, 3, 8, 5, 4, 7, 9, 10}, Q = 3, X[] = {2, 4, 1}, Y[] = {4, 3, 10}
Output: 68
Explanation:
For i = 1,
We can replace atmost 2 elements from array arr[] with integer 4. Here 2 element of array arr[] are smaller than 4 so we will replace elements 2 and 3 from array arr[] with 4 and arr[] becomes {5, 4, 6, 4, 8, 5, 4, 7, 9, 10}.
For i = 2,
We can replace at most 4 elements from array ar[] with integer 3, but no element of array arr[] is smaller than 3. So we will not replace anything.
For i = 3,
We can replace at most 1 element from array arr[] with integer 10, 9 elements of array arr[] are smaller than 10. To get the maximum sum, we will replace the smallest element from array arr[] with 10. Array arr[] after 3rd operation = {5, 10, 6, 4, 8, 5, 10, 7, 9, 10 }. The maximum possible sum is 68.
Input: ar[] = {200, 100, 200, 300}, Q = 2, X[] = {2, 3}, Y[] = {100, 90}
Output: 800
Explanation:
For i = 1,
We can replace atmost 2 elements from array arr[] with integer 100, no element of array arr[] is smaller than 100. So we will replace 0 elements.
For i = 2,
We can replace at most 3 elements from array arr[] with integer 90, no element of array arr[] is smaller than 90. So we will replace 0 elements. So the maximum sum we can obtain after q operation is 800.
天真的方法:天真的想法是从数组arr[] 中挑选X[i] 个数字元素。如果数组中的元素小于Y[i],则更新这些元素的X[i] 。
时间复杂度: (N 2 )
辅助空间: O(1)
高效的方法:这个想法是使用优先级队列在具有较低值的元素之前获取具有较高值的元素,精确地成对的优先级队列以其频率存储值。以下是步骤:
- 将数组arr[] 的每个元素及其出现在优先级队列中。
- 对于数组X[]中的每个元素(比如X[i] )执行以下操作:
- 从优先级队列中最多选择X[i]个最小元素。
- 如果选择元素小于Y[i],则将其替换为 Y[i]。
- 将被替换的元素以其对应的频率重新插入到优先级队列中。
- 在上述操作之后,数组arr[]将包含所有元素之和最大的元素。打印总和。
下面是上述方法的实现:
C++
// C++ implementation to find the
// maximum possible sum of array
// after performing given operations
#include
using namespace std;
// Function to get maximum
// sum after q operations
void max_sum(int ar[], int n,
int q, int x[], int y[])
{
int ans = 0, i;
// priority queue to
// get maximum sum
priority_queue > pq;
// Push pair, value and 1
// in the priority queue
for (i = 0; i < n; i++)
pq.push({ ar[i], 1 });
// Push pair, value (to be replaced)
// and number of elements (to be replaced)
for (i = 0; i < q; i++)
pq.push({ y[i], x[i] });
// Add top n elements from
// the priority queue
// to get max sum
while (n > 0) {
// pr is the pair
// pr.first is the value and
// pr.second is the occurrence
auto pr = pq.top();
// pop from the priority queue
pq.pop();
// Add value to answer
ans += pr.first * min(n, pr.second);
// Update n
n -= pr.second;
}
cout << ans << "\n";
}
// Driver code
int main()
{
int ar[] = { 200, 100, 200, 300 };
int n = (sizeof ar) / (sizeof ar[0]);
int q = 2;
int x[] = { 2, 3 };
int y[] = { 100, 90 };
max_sum(ar, n, q, x, y);
return 0;
}
Java
// Java implementation to find the
// maximum possible sum of array
// after performing given operations
import java.util.*;
import java.lang.*;
class GFG{
static class pair
{
int first, second;
pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to get maximum
// sum after q operations
static void max_sum(int ar[], int n, int q,
int x[], int y[])
{
int ans = 0, i;
// priority queue to
// get maximum sum
PriorityQueue pq = new PriorityQueue<>(
(a, b) -> Integer.compare(a.second, b.second));
// Push pair, value and 1
// in the priority queue
for(i = 0; i < n; i++)
pq.add(new pair(ar[i], 1 ));
// Push pair, value (to be replaced)
// and number of elements (to be replaced)
for(i = 0; i < q; i++)
pq.add(new pair(y[i], x[i]));
// Add top n elements from
// the priority queue
// to get max sum
while (n > 0)
{
// pr is the pair
// pr.first is the value and
// pr.second is the occurrence
pair pr = pq.peek();
// pop from the priority queue
pq.poll();
// Add value to answer
ans += pr.first * Math.min(n, pr.second);
// Update n
n -= pr.second;
}
System.out.println(ans);
}
// Driver Code
public static void main (String[] args)
{
int ar[] = { 200, 100, 200, 300 };
int n = ar.length;
int q = 2;
int x[] = { 2, 3 };
int y[] = { 100, 90 };
max_sum(ar, n, q, x, y);
}
}
// This code is contributed by offbeat
800
时间复杂度: O(N*log 2 N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。