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

📅  最后修改于: 2021-10-26 05:29:48             🧑  作者: Mango

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

例子:

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

  • 按降序对给定数组进行排序。
  • 初始化一个变量,比如totalCost ,以存储将数组拆分为K个子集的最小成本。
  • 初始化一个变量,比如X ,以存储子集中元素的位置。
  • 使用变量i在范围[1, N] 上迭代。对于每个i操作,将totalCost的值增加((arr[i]+ …+ arr[i + K]) * X)并更新i = i + K , X += 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


Javascript


输出:
59

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

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