给定一个由N 个整数和一个正整数K组成的排序数组arr[] (使得N%K为0 ),任务是找到大小为 K的所有可能子序列的中位数的最小和,使得每个元素属于只有一个子序列。
例子:
Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 2
Output: 6
Explanation:
Consider the subsequences of size K as {1, 4}, {2, 5}, and {3, 6}.
The sum of medians of all the subsequences is (1 + 2 + 3) = 6 which is the minimum possible sum.
Input: K = 3, arr[] = {3, 11, 12, 22, 33, 35, 38, 67, 69, 71, 94, 99}, K = 3
Output: 135
朴素方法:可以通过生成所有可能的K大小的排序子序列并打印所有这些子序列的中值作为结果来解决给定的问题。
时间复杂度: O(2 N )
辅助空间: O(1)
高效方法:上述方法也可以通过使用贪婪方法来优化所有子序列的构造。这个想法是从开始选择K/2 个元素 数组和数组末尾的K/2 个元素,使得中位数始终出现在第一部分。请按照以下步骤解决问题:
- 初始化一个变量,比如res存储中位数的结果总和。
- 初始化一个变量,比如T为N/K以存储所需子序列的数量,并将变量D初始化为(K + 1)/2以存储中位数之间的距离。
- 初始化一个变量,比如i as (D – 1)来存储要添加到结果中的第一个中值的索引。
- 迭代直到i < N和T > 0 的值,并执行以下步骤:
- 将arr[i]的值添加到变量res 。
- 将i的值增加D以获得下一个中位数的索引。
- 将T的值减少1 。
- 完成上述步骤后,打印res的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum sum of
// all the medians of the K sized sorted
// arrays formed from the given array
void sumOfMedians(int arr[], int N,
int K)
{
// Stores the distance between
// the medians
int selectMedian = (K + 1) / 2;
// Stores the number of subsequences
// required
int totalArrays = N / K;
// Stores the resultant sum
int minSum = 0;
// Iterate from start and add
// all the medians
int i = selectMedian - 1;
while (i < N and totalArrays != 0) {
// Add the value of arr[i]
// to the variable minsum
minSum = minSum + arr[i];
// Increment i by select the
// median to get the next
// median index
i = i + selectMedian;
// Decrement the value of
// totalArrays by 1
totalArrays--;
}
// Print the resultant minimum sum
cout << minSum;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = sizeof(arr) / sizeof(int);
int K = 2;
sumOfMedians(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
// Function to find the minimum sum of
// all the medians of the K sized sorted
// arrays formed from the given array
static void sumOfMedians(int arr[], int N, int K)
{
// Stores the distance between
// the medians
int selectMedian = (K + 1) / 2;
// Stores the number of subsequences
// required
int totalArrays = N / K;
// Stores the resultant sum
int minSum = 0;
// Iterate from start and add
// all the medians
int i = selectMedian - 1;
while (i < N && totalArrays != 0) {
// Add the value of arr[i]
// to the variable minsum
minSum = minSum + arr[i];
// Increment i by select the
// median to get the next
// median index
i = i + selectMedian;
// Decrement the value of
// totalArrays by 1
totalArrays--;
}
// Print the resultant minimum sum
System.out.println(minSum);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = arr.length;
int K = 2;
sumOfMedians(arr, N, K);
}
}
// This code is ccontributed by Kingash.
Python3
# Python3 program for the above approach
# Function to find the minimum sum of
# all the medians of the K sized sorted
# arrays formed from the given array
def sumOfMedians(arr, N, K):
# Stores the distance between
# the medians
selectMedian = (K + 1) // 2
# Stores the number of subsequences
# required
totalArrays = N // K
# Stores the resultant sum
minSum = 0
# Iterate from start and add
# all the medians
i = selectMedian - 1
while (i < N and totalArrays != 0):
# Add the value of arr[i]
# to the variable minsum
minSum = minSum + arr[i]
# Increment i by select the
# median to get the next
# median index
i = i + selectMedian
# Decrement the value of
# totalArrays by 1
totalArrays -= 1
# Print the resultant minimum sum
print(minSum)
# Driver Code
if __name__ == '__main__':
arr = [ 1, 2, 3, 4, 5, 6 ]
N = len(arr)
K = 2
sumOfMedians(arr, N, K)
# This code is contributed by nirajgsuain5
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the minimum sum of
// all the medians of the K sized sorted
// arrays formed from the given array
static void sumOfMedians(int[] arr, int N, int K)
{
// Stores the distance between
// the medians
int selectMedian = (K + 1) / 2;
// Stores the number of subsequences
// required
int totalArrays = N / K;
// Stores the resultant sum
int minSum = 0;
// Iterate from start and add
// all the medians
int i = selectMedian - 1;
while (i < N && totalArrays != 0) {
// Add the value of arr[i]
// to the variable minsum
minSum = minSum + arr[i];
// Increment i by select the
// median to get the next
// median index
i = i + selectMedian;
// Decrement the value of
// totalArrays by 1
totalArrays--;
}
// Print the resultant minimum sum
Console.WriteLine(minSum);
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 3, 4, 5, 6 };
int N = arr.Length;
int K = 2;
sumOfMedians(arr, N, K);
}
}
// This code is contributed by code_hunt.
Javascript
6
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live