给定两个由N和K个整数组成的数组arr []和S [] ,任务是在将数组拆分为K个子集后,找到每个子集的最小值和最大值的最大和,以使每个子集的大小等于1数组S []中元素的数量。
例子:
Input: arr[] = {1, 13, 7, 17}, S[] = {1, 3}
Output: 48
Explanation: Consider splitting the array as {17}, {1, 7, 13}, such that the size of subsets are 1 and 3 respectively, which is present in the array S[].
The sum of the maximum and minimum of each subset is (17 + 17 + 1 + 13) = 48.
Input: arr[ ] = {5, 1, -30, 0, 11, 20, 19}, S[] = {4, 1, 2}
Output: 45
方法:可以使用贪婪方法解决给定的问题,其思想是在每个组中插入前K个最大元素,然后开始用较大的元素填充较小的组。请按照以下步骤解决问题:
- 初始化一个变量,例如ans为0 ,以存储所有子集的最大值和最小值之和。
- 以降序对数组arr []进行排序。
- 以升序对数组S []进行排序。
- 找到数组的前K个元素的总和,并将其存储在变量ans中,然后将S []的所有元素减1 。
- 初始化一个变量,例如counter为K – 1 ,以存储每个子集的最小元素的索引。
- 遍历数组S []并以S [i]递增计数器,并将arr [counter]的值添加到ans 。
- 完成上述步骤后,输出ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function find maximum sum of
// minimum and maximum of K subsets
int maximumSum(int arr[], int S[],
int N, int K)
{
// Stores the result
int ans = 0;
// Sort the array arr[] in
// decreasing order
sort(arr, arr + N, greater());
// Traverse the range [0, K]
for (int i = 0; i < K; i++)
ans += arr[i];
// Sort the array S[] in
// ascending order
sort(S, S + K);
// Traverse the array S[]
for (int i = 0; i < K; i++) {
// If S{i] is 1
if (S[i] == 1)
ans += arr[i];
S[i]--;
}
// Stores the index of the minimum
// element of the i-th subset
int counter = K - 1;
// Traverse the array S[]
for (int i = 0; i < K; i++) {
// Update the counter
counter = counter + S[i];
if (S[i] != 0)
ans += arr[counter];
}
// Return the resultant sum
return ans;
}
// Driver Code
int main()
{
int arr[] = { 1, 13, 7, 17 };
int S[] = { 1, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = sizeof(S) / sizeof(S[0]);
cout << maximumSum(arr, S, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function find maximum sum of
// minimum and maximum of K subsets
static int maximumSum(int arr[], int S[],
int N, int K)
{
// Stores the result
int ans = 0;
// Sort the array arr[] in
// decreasing order
Arrays.sort(arr);
for(int i = 0; i < N / 2; i++)
{
int temp = arr[i];
arr[i] = arr[N - 1 - i];
arr[N - 1 - i] = temp;
}
// Traverse the range [0, K]
for(int i = 0; i < K; i++)
ans += arr[i];
// Sort the array S[] in
// ascending order
Arrays.sort(S);
// Traverse the array S[]
for(int i = 0; i < K; i++)
{
// If S{i] is 1
if (S[i] == 1)
ans += arr[i];
S[i]--;
}
// Stores the index of the minimum
// element of the i-th subset
int counter = K - 1;
// Traverse the array S[]
for(int i = 0; i < K; i++)
{
// Update the counter
counter = counter + S[i];
if (S[i] != 0)
ans += arr[counter];
}
// Return the resultant sum
return ans;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 13, 7, 17 };
int S[] = { 1, 3 };
int N = arr.length;
int K = S.length;
System.out.println(maximumSum(arr, S, N, K));
}
}
// This code is contributed by Kingash
Python3
# Python3 program for the above approach
# Function find maximum sum of
# minimum and maximum of K subsets
def maximumSum(arr, S, N, K):
# Stores the result
ans = 0
# Sort the array arr[] in
# decreasing order
arr = sorted(arr)[::-1]
# Traverse the range [0, K]
for i in range(K):
ans += arr[i]
# Sort the array S[] in
# ascending order
S = sorted(S)
# Traverse the array S[]
for i in range(K):
# If S{i] is 1
if (S[i] == 1):
ans += arr[i]
S[i] -= 1
# Stores the index of the minimum
# element of the i-th subset
counter = K - 1
# Traverse the array S[]
for i in range(K):
# Update the counter
counter = counter + S[i]
if (S[i] != 0):
ans += arr[counter]
# Return the resultant sum
return ans
# Driver Code
if __name__ == '__main__':
arr = [ 1, 13, 7, 17 ]
S = [ 1, 3 ]
N = len(arr)
K = len(S)
print (maximumSum(arr, S, N, K))
# This code is contributed by mohit kumar 29
输出:
48
时间复杂度: O(N)
辅助空间: O(1)