给定一个大小为N的数组A[]和一个正整数K (它总是N 的一个因数),任务是通过将数组分区为数组的每个分区的第二个最小元素的最大可能总和(N / K) 个大小相等的分区。
例子:
Input: A[] = {2, 3, 1, 4, 7, 5, 6, 1}, K = 4
Output: 7
Explanation: Split the array as {1, 2, 3, 4} and {1, 5, 6, 7}. Therefore, sum = 2 + 5 = 7, which is the maximum possible sum.
Input: A[] = {12, 43, 15, 32, 45, 23}, K = 3
Output : 66
Explanation: Split the array as {12, 23, 32} and {15, 43, 45}. Therefore, sum = 23 + 43 = 66, which is the maximum possible sum.
方法:想法是按升序对给定数组进行排序,并且为了最大化所需的总和,将A[]的前N / K 个元素除以每个数组作为它们的第一项,然后选择每个(K – 1 ) A[] 的第 th 个元素,从N/K 开始。
请按照以下步骤解决问题:
- 按升序对数组A[]进行排序。
- 用0初始化sum以存储所需的总和。
- 现在,用N / K初始化一个变量i 。
- 当i小于N 时,执行以下步骤:
- 通过A[i]增加总和。
- 将i增加K – 1 。
- 遍历后,打印sum作为所需答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum sum of
// second smallest of each partition
// of size K
void findSum(int A[], int N, int K)
{
// Sort the array A[]
// in ascending order
sort(A, A + N);
// Store the maximum sum of second
// smallest of each partition
// of size K
int sum = 0;
// Select every (K-1)th element as
// second smallest element
for (int i = N / K; i < N; i += K - 1) {
// Update sum
sum += A[i];
}
// Print the maximum sum
cout << sum;
}
// Driver Code
int main()
{
// Given size of partitions
int K = 4;
// Given array A[]
int A[] = { 2, 3, 1, 4, 7, 5, 6, 1 };
// Size of the given array
int N = sizeof(A) / sizeof(A[0]);
// Function Call
findSum(A, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the maximum sum of
// second smallest of each partition
// of size K
static void findSum(int A[], int N, int K)
{
// Sort the array A[]
// in ascending order
Arrays.sort(A);
// Store the maximum sum of second
// smallest of each partition
// of size K
int sum = 0;
// Select every (K-1)th element as
// second smallest element
for(int i = N / K; i < N; i += K - 1)
{
// Update sum
sum += A[i];
}
// Print the maximum sum
System.out.print(sum);
}
// Driver Code
public static void main(String[] args)
{
// Given size of partitions
int K = 4;
// Given array A[]
int A[] = { 2, 3, 1, 4, 7, 5, 6, 1 };
// Size of the given array
int N = A.length;
// Function Call
findSum(A, N, K);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# Function to find the maximum sum of
# second smallest of each partition
# of size K
def findSum(A, N, K):
# Sort the array A
# in ascending order
A.sort();
# Store the maximum sum of second
# smallest of each partition
# of size K
sum = 0;
# Select every (K-1)th element as
# second smallest element
for i in range(N // K, N, K - 1):
# Update sum
sum += A[i];
# Print the maximum sum
print(sum);
# Driver Code
if __name__ == '__main__':
# Given size of partitions
K = 4;
# Given array A
A = [2, 3, 1, 4, 7, 5, 6, 1];
# Size of the given array
N = len(A);
# Function Call
findSum(A, N, K);
# This code contributed by shikhasingrajput
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the maximum sum of
// second smallest of each partition
// of size K
static void findSum(int []A, int N, int K)
{
// Sort the array []A
// in ascending order
Array.Sort(A);
// Store the maximum sum of second
// smallest of each partition
// of size K
int sum = 0;
// Select every (K-1)th element as
// second smallest element
for(int i = N / K; i < N; i += K - 1)
{
// Update sum
sum += A[i];
}
// Print the maximum sum
Console.Write(sum);
}
// Driver Code
public static void Main(String[] args)
{
// Given size of partitions
int K = 4;
// Given array []A
int []A = { 2, 3, 1, 4, 7, 5, 6, 1 };
// Size of the given array
int N = A.Length;
// Function Call
findSum(A, N, K);
}
}
// This code is contributed by shikhasingrajput
Javascript
7
时间复杂度: O(N * log(N))
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。