通过将最大元素减半来计算 K 次操作后的数组最大值之和
给定一个包含N个整数的数组arr[]和一个整数K ,任务是找到可能的数组的最大值之和,其中每个操作都将数组的当前最大值替换为其一半。
例子:
Input: arr[] = {2, 4, 6, 8, 10}, K = 5
Output: 33
Explanation: In 1st operation, the maximum of the given array is 10. Hence, the value becomes 10 and the array after 1st operation becomes arr[] = {2, 4, 6, 8, 5}.
The value after 2nd operation = 18 and arr[] = {2, 4, 6, 4, 5}.
Similarly, proceeding forward, value after 5th operation will be 33.
Input: arr[] = {6, 5}, K = 3
Output: 14
方法:给定的问题可以在贪心方法的帮助下解决。这个想法是使用最大堆数据结构。因此,遍历给定数组并将数组arr[]中的所有元素插入到最大优先级队列中。在每次操作中,使用 pop 操作从堆中删除最大值,将其添加到值中,然后将其除以 2 后重新插入堆中。重复上述操作K次后的值就是要求的答案。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to find maximum possible
// value after K given operations
int maxValue(vector arr, int K)
{
// Stores required value
int val = 0;
// Initializing priority queue
// with all elements of array
priority_queue pq(arr.begin(),
arr.end());
// Loop to iterate through
// each operation
while (K--) {
// Current Maximum
int max = pq.top();
pq.pop();
// Update value
val += max;
// Reinsert maximum
pq.push(max / 2);
}
// Return Answer
return val;
}
// Driver Call
int main()
{
vector arr = { 2, 4, 6, 8, 10 };
int K = 5;
cout << maxValue(arr, K);
}
Java
// Java code for the above approach
import java.util.Comparator;
import java.util.PriorityQueue;
class CustomComparator implements Comparator {
@Override
public int compare(Integer number1, Integer number2)
{
int value = number1.compareTo(number2);
// elements are sorted in reverse order
if (value > 0) {
return -1;
}
else if (value < 0) {
return 1;
}
else {
return 0;
}
}
}
class GFG {
// Function to find maximum possible
// value after K given operations
static int maxValue(int[] arr, int K)
{
// Stores required value
int val = 0;
// Initializing priority queue
// with all elements of array
PriorityQueue pq
= new PriorityQueue(new CustomComparator());
for (int i = 0; i < arr.length; i++) {
pq.add(arr[i]);
}
// Loop to iterate through
// each operation
while (K != 0) {
// Current Maximum
int max = pq.poll();
// Update value
val += max;
// Reinsert maximum
pq.add((int)Math.floor(max / 2));
K = K - 1;
}
// Return Answer
return val;
}
// Driver Call
public static void main(String[] args)
{
int[] arr = { 2, 4, 6, 8, 10 };
int K = 5;
System.out.println(maxValue(arr, K));
}
}
// This code is conbtributed by Potta Lokesh
Python3
# python3 program of the above approach
from queue import PriorityQueue
# Function to find maximum possible
# value after K given operations
def maxValue(arr, K):
# Stores required value
val = 0
# Initializing priority queue
# with all elements of array
pq = PriorityQueue()
for dt in arr:
pq.put(-1*dt)
# Loop to iterate through
# each operation
while (True):
# Current Maximum
max = -1 * pq.get()
# Update value
val += max
# Reinsert maximum
pq.put(-1 * (max // 2))
K -= 1
if K == 0:
break
# Return Answer
return val
# Driver Call
if __name__ == "__main__":
arr = [2, 4, 6, 8, 10]
K = 5
print(maxValue(arr, K))
# This code is contributed by rakeshsahni
33
时间复杂度: O(K*log N)
辅助空间: O(N)