📌  相关文章
📜  最小化将数组拆分为K个子集的成本,以便每个元素的成本为其在子集中的位置的乘积

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

给定大小为N的数组arr []和正整数K ,任务是找到将数组拆分为K个子集的最小可能开销,其中每个子集的i元素(基于1的索引)的开销相等到那个元素与i的乘积。

例子:

方法:可以使用贪婪技术解决问题。想法是将数组元素划分为各个子集中的所有元素以降序排列。请按照以下步骤解决问题:

  • 按降序对给定数组进行排序。
  • 初始化一个变量,例如totalCost ,以存储将数组拆分为K个子集的最小开销。
  • 初始化一个变量,例如X ,以将元素的位置存储在子集中。
  • 使用变量i[1,N]范围内迭代。对于每个i操作,将totalCost的值增加(((arr [i] +…+ arr [i + K])* X)并更新i = i + KX + = 1
  • 最后,打印totalCost的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the minimum cost to
// split array into K subsets
int getMinCost(int* arr, int n, int k)
{
    // Sort the array in descending order
    sort(arr, arr + n, greater());
 
    // Stores minimum cost to split
    // the array into K subsets
    int min_cost = 0;
 
    // Stores position of
    // elements of a subset
    int X = 0;
 
    // Iterate over the range [1, N]
    for (int i = 0; i < n; i += k) {
 
        // Calculate the cost to select
        // X-th element of every subset
        for (int j = i; j < i + k && j < n; j++) {
 
            // Update min_cost
            min_cost += arr[j] * (X + 1);
        }
 
        // Update X
        X++;
    }
 
    return min_cost;
}
 
// Driver Code
int main()
{
    int arr[] = { 9, 20, 7, 8 };
 
    int K = 2;
 
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    // Function call
    cout << getMinCost(arr, N, K) << endl;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
 
// reverses an array
static void reverse(int a[], int n)
{
    int i, k, t;
    for (i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
}
  
// Function to find the minimum cost to
// split array into K subsets
static int getMinCost(int[] arr, int n, int k)
{
   
    // Sort the array in descending order
    Arrays.sort(arr);
    reverse(arr, n);
  
    // Stores minimum cost to split
    // the array into K subsets
    int min_cost = 0;
  
    // Stores position of
    // elements of a subset
    int X = 0;
  
    // Iterate over the range [1, N]
    for (int i = 0; i < n; i += k)
    {
  
        // Calculate the cost to select
        // X-th element of every subset
        for (int j = i; j < i + k && j < n; j++)
        {
  
            // Update min_cost
            min_cost += arr[j] * (X + 1);
        }
  
        // Update X
        X++;
    }
    return min_cost;
}
   
// Driver code
public static void main(String[] args)
{
    int arr[] = { 9, 20, 7, 8 };
    int K = 2;
    int N = arr.length;
  
    // Function call
    System.out.println( getMinCost(arr, N, K));
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python program to implement
# the above approach
 
# Function to find the minimum cost to
# split array into K subsets
def getMinCost(arr, n, k):
   
    # Sort the array in descending order
    arr.sort(reverse = True)
 
    # Stores minimum cost to split
    # the array into K subsets
    min_cost = 0;
 
    # Stores position of
    # elements of a subset
    X = 0;
 
    # Iterate over the range [1, N]
    for i in range(0, n, k):
 
        # Calculate the cost to select
        # X-th element of every subset
        for j in range(i, n, 1):
           
            # Update min_cost
            if(j < i + k):
                min_cost += arr[j] * (X + 1);
 
        # Update X
        X += 1;
    return min_cost;
 
# Driver code
if __name__ == '__main__':
    arr = [9, 20, 7, 8];
    K = 2;
    N = len(arr);
 
    # Function call
    print(getMinCost(arr, N, K));
 
# This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
class GFG
{
 
// reverses an array
static void reverse(int []a, int n)
{
    int i, k, t;
    for (i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
}
  
// Function to find the minimum cost to
// split array into K subsets
static int getMinCost(int[] arr, int n, int k)
{
   
    // Sort the array in descending order
    Array.Sort(arr);
    reverse(arr, n);
  
    // Stores minimum cost to split
    // the array into K subsets
    int min_cost = 0;
  
    // Stores position of
    // elements of a subset
    int X = 0;
  
    // Iterate over the range [1, N]
    for (int i = 0; i < n; i += k)
    {
  
        // Calculate the cost to select
        // X-th element of every subset
        for (int j = i; j < i + k && j < n; j++)
        {
  
            // Update min_cost
            min_cost += arr[j] * (X + 1);
        }
  
        // Update X
        X++;
    }
    return min_cost;
}
   
// Driver code
public static void Main(String[] args)
{
    int []arr = { 9, 20, 7, 8 };
    int K = 2;
    int N = arr.Length;
  
    // Function call
    Console.WriteLine( getMinCost(arr, N, K));
}
}
 
// This code is contributed by shikhasingrajput


输出:
59

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