给定一个由N 个整数组成的整数数组arr[] ,任务是通过执行最多K 个操作来最小化给定数组的总和,其中每个操作涉及将数组元素arr[i]减少到floor(arr[i] /2) 。
例子 :
Input: N = 4, a[] = {20, 7, 5, 4}, K = 3
Output: 17
Explanation:
Operation 1: {20, 7, 5, 4} -> {10, 7, 5, 4}
Operation 2: {10, 7, 5, 4} -> {5, 7, 5, 4}
Operation 3: {5, 7, 5, 4} -> {5, 3, 5, 4}
No further operation can be performed. Therefore, sum of the array = 17.
Input: N = 4, a[] = {10, 4, 6, 16}, K = 2
Output: 23
方法:为了获得最小可能的总和,每次操作的主要思想是在每次操作之前减少数组中的最大元素。这可以使用MaxHeap来实现。请按照以下步骤解决问题:
- 将所有数组元素插入MaxHeap 。
- 弹出MaxHeap的根并将(popped element) / 2插入MaxHeap
- 重复上述步骤K次后,将MaxHeap中的元素一一弹出,并不断添加它们的值。最后,打印总和。
下面是上述方法的实现:
C++
// C++ program to implement the
// above approach
#include
using namespace std;
// Function to obtain the minimum possible
// sum from the array by K reductions
int minSum(int a[], int n, int k)
{
priority_queue q;
// Insert elements into the MaxHeap
for(int i = 0; i < n; i++)
{
q.push(a[i]);
}
while(!q.empty() && k > 0)
{
int top = q.top() / 2;
// Remove the maximum
q.pop();
// Insert maximum / 2
q.push(top);
k -= 1;
}
// Stores the sum of remaining elements
int sum = 0;
while(!q.empty())
{
sum += q.top();
q.pop();
}
return sum;
}
// Driver code
int main()
{
int n = 4;
int k = 3;
int a[] = { 20, 7, 5, 4 };
cout << (minSum(a, n, k));
return 0;
}
// This code is contributed by jojo9911
Java
// Java Program to implement the
// above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to obtain the minimum possible
// sum from the array by K reductions
public static int minSum(int a[], int n, int k)
{
// Implements the MaxHeap
PriorityQueue maxheap
= new PriorityQueue<>((one, two) -> two - one);
// Insert elements into the MaxHeap
for (int i = 0; i < n; i++)
maxheap.add(a[i]);
while (maxheap.size() > 0 && k > 0) {
// Remove the maximum
int max_ele = maxheap.poll();
// Insert maximum / 2
maxheap.add(max_ele / 2);
k -= 1;
}
// Stores the sum of remaining elements
int sum = 0;
while (maxheap.size() > 0)
sum += maxheap.poll();
return sum;
}
// Driver Code
public static void main(String[] args)
{
int n = 4;
int k = 3;
int a[] = { 20, 7, 5, 4 };
System.out.println(minSum(a, n, k));
}
}
Python3
# Python3 program to implement the
# above approach
# Function to obtain the minimum possible
# sum from the array by K reductions
def minSum(a, n, k):
q = []
# Insert elements into the MaxHeap
for i in range(n):
q.append(a[i])
q = sorted(q)
while (len(q) > 0 and k > 0):
top = q[-1] // 2
# Remove the maximum
del q[-1]
# Insert maximum / 2
q.append(top)
k -= 1
q = sorted(q)
# Stores the sum of remaining elements
sum = 0
while(len(q) > 0):
sum += q[-1]
del q[-1]
return sum
# Driver code
if __name__ == '__main__':
n = 4
k = 3
a = [ 20, 7, 5, 4 ]
print(minSum(a, n, k))
# This code is contributed by mohit kumar 29
C#
// C# program to implement the
// above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to obtain the minimum possible
// sum from the array by K reductions
static int minSum(int[] a, int n, int k)
{
// Implements the MaxHeap
List q = new List();
for(int i = 0; i < n; i++)
{
// Insert elements into the MaxHeap
q.Add(a[i]);
}
q.Sort();
while (q.Count != 0 && k > 0)
{
int top = q[q.Count - 1] / 2;
// Remove the maximum
// Insert maximum / 2
q[q.Count - 1] = top;
k--;
q.Sort();
}
// Stores the sum of remaining elements
int sum = 0;
while (q.Count != 0)
{
sum += q[0];
q.RemoveAt(0);
}
return sum;
}
// Driver Code
static public void Main()
{
int n = 4;
int k = 3;
int[] a = { 20, 7, 5, 4 };
Console.WriteLine(minSum(a, n, k));
}
}
// This code is contributed by avanitrachhadiya2155
Javascript
输出:
17
时间复杂度: O(Klog(N))
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。