给定两个由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]增加 counter 并将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
C#
// C# program for the above approach
using System;
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
Array.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
Array.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;
Console.WriteLine(maximumSum(arr, S, N, K));
}
}
// This code is contributed by ukasp.
Javascript
输出:
48
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live