通过将给定的 Array 拆分为大小为 K 的子集并将每个子集的最高 K/2 个元素添加到成本中来最小化成本
给定一个包含N个整数的数组arr[]和一个整数K ,任务是通过将数组元素溢出到大小为K的子集并将最大⌈K/2⌉元素添加到成本中来计算最小成本。
注: ⌈K/2⌉表示K/2的上限。
例子:
Input: arr[] = {1, 1, 2, 2}, K = 2
Output: 3
Explanation: The given array elements can be divided into subsets {2, 2} and {1, 1}.
Hence, the cost will be 2 + 1 = 3, which is the minimum possible.
Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 3
Output: 16
Explanation: The array can be split like {1, 2, 3} and {4, 5, 6}.
Now ceil(3/2) = ceil(1.5) = 2.
So take 2 highest elements from each set.
The sum becomes 2 + 3 + 5 + 6 = 16
方法:给定的问题可以通过使用贪心方法来解决。这个想法是按降序对元素进行排序,并开始连续形成K个元素的组,并在变量中保持每个组的最高⌈K/2⌉元素的总和。这将确保不考虑成本的元素是最大可能的,从而最小化所选元素的总成本。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to find minimum cost by splitting
// the array into subsets of size at most K
// and adding maximum K/2 elements into cost
int minimizeCost(vector arr, int K)
{
// Stores the final cost
int ans = 0;
// Sort the array arr[]
sort(arr.begin(), arr.end(),
greater());
// Loop to iterate over each
// group of K elements
for (int i = 0; i < arr.size();
i += K) {
// Loop to iterate over
// maximum K/2 elements
for (int j = i; j < i + ceil(K / 2.0)
&& j < arr.size();
j++) {
ans += arr[j];
}
}
// Return Answer
return ans;
}
// Driver code
int main()
{
vector arr = { 1, 2, 3, 4, 5, 6 };
int K = 3;
cout << minimizeCost(arr, K);
return 0;
}
Java
// JAVA program of the above approach
import java.util.*;
class GFG
{
// Function to find minimum cost by splitting
// the array into subsets of size at most K
// and adding maximum K/2 elements into cost
public static int minimizeCost(ArrayList arr,
int K)
{
// Stores the final cost
int ans = 0;
// Sort the array arr[]
Collections.sort(arr, Collections.reverseOrder());
// Loop to iterate over each
// group of K elements
for (int i = 0; i < arr.size(); i += K) {
// Loop to iterate over
// maximum K/2 elements
for (int j = i; j < i + Math.ceil(K / 2.0)
&& j < arr.size();
j++) {
ans += arr.get(j);
}
}
// Return Answer
return ans;
}
// Driver code
public static void main(String[] args)
{
ArrayList arr = new ArrayList<>(
Arrays.asList(1, 2, 3, 4, 5, 6));
int K = 3;
System.out.print(minimizeCost(arr, K));
}
}
// This code is contributed by Taranpreet
Python
# Pyhton program of the above approach
import math
# Function to find minimum cost by splitting
# the array into subsets of size at most K
# and adding maximum K/2 elements into cost
def minimizeCost(arr, K):
# Stores the final cost
ans = 0
# Sort the array arr[]
arr.sort(reverse = True)
# Loop to iterate over each
# group of K elements
i = 0
while(i < len(arr)):
# Loop to iterate over
# maximum K/2 elements
j = i
while(j < i + math.ceil(K / 2.0) and j < len(arr)):
ans += arr[j]
j += 1
i += K
# Return Answer
return ans
# Driver code
arr = [ 1, 2, 3, 4, 5, 6 ]
K = 3
print(minimizeCost(arr, K))
# This code is contributed by samim2000.
C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find minimum cost by splitting
// the array into subsets of size at most K
// and adding maximum K/2 elements into cost
static int minimizeCost(List arr, int K)
{
// Stores the final cost
int ans = 0;
// Sort the array arr[]
arr.Sort((a, b) => b - a);
// Loop to iterate over each
// group of K elements
for (int i = 0; i < arr.Count;
i += K) {
// Loop to iterate over
// maximum K/2 elements
for (int j = i; j < i + Math.Ceiling(K / 2.0)
&& j < arr.Count;
j++) {
ans += arr[j];
}
}
// Return Answer
return ans;
}
// Driver Code
public static void Main()
{
List arr = new List{ 1, 2, 3, 4, 5, 6 };
int K = 3;
Console.Write(minimizeCost(arr, K));
}
}
// This code is contributed by sanjoy_62.
Javascript
16
时间复杂度: O(N*log N)
辅助空间: O(1)