给定一个大小为N的数组arr[]和一个正整数K ,任务是找到将数组拆分为K个子集的最小可能成本,其中每个子集的第i个元素(基于 1 的索引)的成本相等到该元素和i的乘积。
例子:
Input: arr[] = { 2, 3, 4, 1 }, K = 3
Output: 11
Explanation:
Split the array arr[] into K(= 3) subsets { { 4, 1 }, { 2 }, { 3 } }
Total cost of 1st subset = 4 * 1 + 1 * 2 = 6
Total cost of 2nd subset = 2 * 1 = 2
Total cost of 3rd subset = 3 * 1 = 3
Therefore, the total cost of K(= 3) subsets is 6 + 2 + 3 = 11.
Input: arr[] = { 9, 20, 7, 8 }, K=2
Output: 59
Explanation:
Dividing the array arr[] into K(= 3) subsets { { 20, 8 }, { 9, 7 } }
Total cost of 1st subset = 20 * 1 + 8 * 2 = 36
Total cost of 2nd subset = 9 * 1 + 7 * 2 = 23
Therefore, the total cost of K(= 3) subsets is 36 + 23 = 59
方法:该问题可以使用贪心技术解决。这个想法是将数组元素划分为各个子集中的所有元素按降序排列。请按照以下步骤解决问题:
- 按降序对给定数组进行排序。
- 初始化一个变量,比如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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live