📌  相关文章
📜  通过最多减少K个数来最小化一个数组的总和

📅  最后修改于: 2021-05-17 16:57:37             🧑  作者: Mango

给定一个由N个整数组成的整数数组arr [] ,任务是通过最多执行K个运算来最小化给定数组的总和,其中每个运算都涉及将数组元素arr [i]减少为floor(arr [i] / 2)

例子 :

方法:为了获得最小可能的和,每个操作的主要思想是在每次操作之前减少数组中的最大元素。可以使用MaxHeap来实现。请按照以下步骤解决问题:

  • 将所有数组元素插入MaxHeap中
  • 弹出MaxHeap的根,然后将(弹出元素)/ 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


输出:
17

时间复杂度: O(Klog(N))
辅助空间: O(N)