📌  相关文章
📜  通过反复删除任意两个元素并将它们的和插入到数组中,将求和的值最小化

📅  最后修改于: 2021-04-21 23:41:03             🧑  作者: 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


输出:
48