📌  相关文章
📜  通过重复删除任意两个元素并将它们的总和插入数组来最小化计算出的总和

📅  最后修改于: 2021-10-28 02:05:21             🧑  作者: Mango

给定N 个元素,您可以从列表中删除任意两个元素,记下它们的总和,然后将总和添加到列表中。当列表中有多个元素时重复这些步骤。任务是最终最小化这些所选总和的总和。
例子:

方法:为了最小化总和,每一步选择的元素必须是列表中的最小元素。为了有效地做到这一点,可以使用优先级队列。在每一步,当列表中有多个元素时,选择最小值和第二个最小值,将它们从列表中删除,在更新运行总和后将它们的总和添加到列表中。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
 
// Function to return the minimized sum
int getMinSum(int arr[], int n)
{
    int i, sum = 0;
 
    // Priority queue to store the elements of the array
    // and retrieve the minimum element efficiently
    priority_queue, greater > pq;
 
    // Add all the elements
    // to the priority queue
    for (i = 0; i < n; i++)
        pq.push(arr[i]);
 
    // While there are more than 1 elements
    // left in the queue
    while (pq.size() > 1)
    {
 
        // Remove and get the minimum
        // element from the queue
        int min = pq.top();
 
        pq.pop();
 
        // Remove and get the second minimum
        // element (currently minimum)
        int secondMin = pq.top();
         
        pq.pop();
 
        // Update the sum
        sum += (min + secondMin);
 
        // Add the sum of the minimum
        // elements to the queue
        pq.push(min + secondMin);
    }
 
    // Return the minimized sum
    return sum;
}
 
// Driver code
int main()
{
 
    int arr[] = { 1, 3, 7, 5, 6 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << (getMinSum(arr, n));
}
 
// This code is contributed by mohit


Java
// Java implementation of the approach
import java.util.PriorityQueue;
 
class GFG
{
 
    // Function to return the minimized sum
    static int getMinSum(int arr[], int n)
    {
        int i, sum = 0;
 
        // Priority queue to store the elements of the array
        // and retrieve the minimum element efficiently
        PriorityQueue pq = new PriorityQueue<>();
 
        // Add all the elements
        // to the prioriry queue
        for (i = 0; i < n; i++)
            pq.add(arr[i]);
 
        // While there are more than 1 elements
        // left in the queue
        while (pq.size() > 1)
        {
 
            // Remove and get the minimum
            // element from the queue
            int min = pq.poll();
 
            // Remove and get the second minimum
            // element (currently minimum)
            int secondMin = pq.poll();
 
            // Update the sum
            sum += (min + secondMin);
 
            // Add the sum of the minimum
            // elements to the queue
            pq.add(min + secondMin);
        }
 
        // Return the minimized sum
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 3, 7, 5, 6 };
        int n = arr.length;
        System.out.print(getMinSum(arr, n));
    }
}


Python3
# Python3 implementation of the approach
 
# Function to return the minimized sum
def getMinSum(arr, n):
 
    sum = 0
  
    # Priority queue to store the elements of the array
    # and retrieve the minimum element efficiently
    pq = []
  
    # Add all the elements
    # to the priority queue
    for i in range( n ):
        pq.append(arr[i])
      
    # While there are more than 1 elements
    # left in the queue
    while (len(pq) > 1) :
         
        pq.sort(reverse=True)
   
        # Remove and get the minimum
        # element from the queue
        min = pq[-1];
        
        pq.pop();
  
        # Remove and get the second minimum
        # element (currently minimum)
        secondMin = pq[-1];
          
        pq.pop();
  
        # Update the sum
        sum += (min + secondMin);
  
        # Add the sum of the minimum
        # elements to the queue
        pq.append(min + secondMin)
     
    # Return the minimized sum
    return sum
  
# Driver code
if __name__ == "__main__":
  
    arr = [ 1, 3, 7, 5, 6 ]
    n = len(arr)
    print(getMinSum(arr, n))
 
# This code is contributed by chitranayal


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
 
  // Function to return the minimized sum
  static int getMinSum(int[] arr, int n)
  {
    int i, sum = 0;
 
    // Priority queue to store the elements of the array
    // and retrieve the minimum element efficiently
    List pq = new List();
 
    // Add all the elements
    // to the priority queue
    for (i = 0; i < n; i++)
    {
      pq.Add(arr[i]);
    }
 
    // While there are more than 1 elements
    // left in the queue
    while(pq.Count > 1)
    {
      pq.Sort();
 
      // Remove and get the minimum
      // element from the queue
      int min = pq[0];
      pq.RemoveAt(0);
 
      // Remove and get the second minimum
      // element (currently minimum)
      int secondMin = pq[0];
      pq.RemoveAt(0);
 
      // Update the sum
      sum += (min + secondMin);
 
      // Add the sum of the minimum
      // elements to the queue
      pq.Add(min + secondMin);
    }
 
    // Return the minimized sum
    return sum;
  }
 
  // Driver code
  static public void Main ()
  {
    int[] arr = { 1, 3, 7, 5, 6 };
    int n = arr.Length;
    Console.WriteLine(getMinSum(arr, n));
  }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出:

48

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程