📌  相关文章
📜  排序数组的所有可能的 K 个长度子序列的中位数的最小总和

📅  最后修改于: 2021-10-25 10:34:20             🧑  作者: Mango

给定一个由N 个整数和一个正整数K组成的排序数组arr[] (使得N%K0 ),任务是找到大小为 K的所有可能子序列的中位数的最小和,使得每个元素属于只有一个子序列。

例子:

朴素方法:可以通过生成所有可能的K大小的排序子序列并打印所有这些子序列的中值作为结果来解决给定的问题。

时间复杂度: O(2 N )
辅助空间: O(1)

高效方法:上述方法也可以通过使用贪婪方法来优化所有子序列的构造。这个想法是从开始选择K/2 个元素 数组和数组末尾的K/2 个元素,使得中位数始终出现在第一部分。请按照以下步骤解决问题:

  • 初始化一个变量,比如res存储中位数的结果总和。
  • 初始化一个变量,比如TN/K以存储所需子序列的数量,并将变量D初始化为(K + 1)/2以存储中位数之间的距离。
  • 初始化一个变量,比如i as (D – 1)来存储要添加到结果中的第一个中值的索引。
  • 迭代直到i < NT > 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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程