📌  相关文章
📜  最大化分布中所有组的最小值和最大值之和

📅  最后修改于: 2022-05-13 01:56:06.981000             🧑  作者: Mango

最大化分布中所有组的最小值和最大值之和

给定一个数组arr[]和一个整数N。任务是最大化N组中数组元素分布中每个组的最小值和最大值之和,其中每个组的大小在数组b[]中给出大小为N

例子:

方法:这个问题可以通过使用贪心方法和一些实现来解决。请按照以下步骤解决给定的问题。

  • 对数组arr[]按降序排序,对b[]按升序排序。
  • 初始化一个变量ans来存储输出。
  • 在数组a[]中从i = 0 迭代到 i = N-1并将每个元素添加到ans.
  • 初始化变量ind以存储数组arr[]元素的索引。将N分配给变量ind。
  • i=0 到 N-1 循环。
    • 如果b[i] > 0则用(b[i]-1)递增ind
    • arr[ind]添加到ans ,因为arr[ind]将是该组的最小元素
    • ind1。
  • 输出答案。

请参阅下图以更好地理解。

插图:

下面是上述方法的实现。

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to maximum possible sum of minimum
// and maximum elements of each group
int geeksforgeeks(int a[], int b[], int n, int k)
{
    // Sorting array a in descending order
    // and array a in ascending order.
    sort(a, a + n, greater());
 
    sort(b, b + k);
 
    // Variable to store the required output.
    int ans = 0;
 
    // Loop to store sum of first k greatest
    // element of array a[] in ans.
    // since they will be gretest element
    // of each group when distributed
    // in group one by one.
    for (int i = 0; i < k; i++) {
        if (b[i] == 1) {
            ans += 2 * a[i];
        }
        else {
            ans += a[i];
        }
 
        --b[i];
    }
    // Variable to store index of element a .
    int ind = k;
 
    // Then after when each grouped is filled,
    // then add a[ind] to ans as a[ind] will be
    // lowest element of each group.
    for (int i = 0; i < k; i++) {
        if (b[i] > 0) {
            ind += b[i] - 1;
            ans += a[ind];
            ind++;
        }
    }
 
    return ans;
}
 
// Driver code
int main()
{
 
    int N = 3;
 
    // Size of array a[]
    int siz = 9;
 
    int a[] = { 17, 12, 11, 9, 8, 8, 5, 4, 3 };
    int b[] = { 2, 3, 4 };
 
    // Function Call
    cout << geeksforgeeks(a, b, 9, N);
}


Java
// Java code to find the maximum median
// of a sub array having length at least K.
import java.util.*;
public class GFG
{
 
// Utility function to sort an array in
// descending order
static void sort(int arr[])
{
    for (int i = 0; i < arr.length; i++)
    {    
        for (int j = i + 1; j < arr.length; j++)
        {    
              if(arr[i] < arr[j])
              {   
                  int temp = arr[i];   
                  arr[i] = arr[j];   
                  arr[j] = temp;   
              }    
        }    
    }   
}
 
// Function to maximum possible sum of minimum
// and maximum elements of each group
static int geeksforgeeks(int a[], int b[], int n, int k)
{
    // Sorting array a in descending order
    // and array b in ascending order.
    sort(a);
 
    Arrays.sort(b);
 
    // Variable to store the required output.
    int ans = 0;
 
    // Loop to store sum of first k greatest
    // element of array a[] in ans.
    // since they will be gretest element
    // of each group when distributed
    // in group one by one.
    for (int i = 0; i < k; i++) {
        if (b[i] == 1) {
            ans += 2 * a[i];
        }
        else {
            ans += a[i];
        }
 
        --b[i];
    }
    // Variable to store index of element a .
    int ind = k;
 
    // Then after when each grouped is filled,
    // then add a[ind] to ans as a[ind] will be
    // lowest element of each group.
    for (int i = 0; i < k; i++) {
        if (b[i] > 0) {
            ind += b[i] - 1;
            ans += a[ind];
            ind++;
        }
    }
 
    return ans;
}
 
// Driver code
public static void main(String args[])
{
    int N = 3;
 
    // Size of array a[]
    int siz = 9;
 
    int a[] = { 17, 12, 11, 9, 8, 8, 5, 4, 3 };
    int b[] = { 2, 3, 4 };
 
    // Function Call
    System.out.println(geeksforgeeks(a, b, 9, N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python
# Python program for the above approach
 
# Function to maximum possible sum of minimum
# and maximum elements of each group
def geeksforgeeks(a, b, n, k):
     
    # Sorting array a in descending order
    # and array a in ascending order.
    a.sort(reverse=True)
 
    b.sort()
 
    # Variable to store the required output.
    ans = 0
 
    # Loop to store sum of first k greatest
    # element of array a[] in ans.
    # since they will be gretest element
    # of each group when distributed
    # in group one by one.
    for i in range(0, k):
        if (b[i] == 1):
            ans = ans + (2 * a[i])
         
        else:
            ans = ans + a[i]
 
        b[i] = b[i] - 1
         
    # Variable to store index of element a .
    ind = k
 
    # Then after when each grouped is filled,
    # then add a[ind] to ans as a[ind] will be
    # lowest element of each group.
    for i in range(0, k):
        if (b[i] > 0):
            ind = ind + b[i] - 1
            ans = ans + a[ind]
            ind = ind + 1
 
    return ans
 
# Driver code
N = 3
 
# Size of array a[]
siz = 9;
 
a = [ 17, 12, 11, 9, 8, 8, 5, 4, 3 ]
b = [ 2, 3, 4 ]
 
# Function Call
print(geeksforgeeks(a, b, 9, N))
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# code to find the maximum median
// of a sub array having length at least K.
using System;
public class GFG
{
 
  // Utility function to sort an array in
  // descending order
  static void sort(int[] arr)
  {
    for (int i = 0; i < arr.Length; i++)
    {    
      for (int j = i + 1; j < arr.Length; j++)
      {    
        if(arr[i] < arr[j])
        {   
          int temp = arr[i];   
          arr[i] = arr[j];   
          arr[j] = temp;   
        }    
      }    
    }   
  }
 
  // Function to maximum possible sum of minimum
  // and maximum elements of each group
  static int geeksforgeeks(int[] a, int[] b, int n, int k)
  {
     
    // Sorting array a in descending order
    // and array b in ascending order.
    sort(a);
 
    Array.Sort(b);
 
    // Variable to store the required output.
    int ans = 0;
 
    // Loop to store sum of first k greatest
    // element of array a[] in ans.
    // since they will be gretest element
    // of each group when distributed
    // in group one by one.
    for (int i = 0; i < k; i++) {
      if (b[i] == 1) {
        ans += 2 * a[i];
      }
      else {
        ans += a[i];
      }
 
      --b[i];
    }
    // Variable to store index of element a .
    int ind = k;
 
    // Then after when each grouped is filled,
    // then add a[ind] to ans as a[ind] will be
    // lowest element of each group.
    for (int i = 0; i < k; i++) {
      if (b[i] > 0) {
        ind += b[i] - 1;
        ans += a[ind];
        ind++;
      }
    }
 
    return ans;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 3;
 
    // Size of array a[]
    int siz = 9;
 
    int[] a = { 17, 12, 11, 9, 8, 8, 5, 4, 3 };
    int[] b = { 2, 3, 4 };
 
    // Function Call
    Console.Write(geeksforgeeks(a, b, 9, N));
  }
}
 
// This code is contributed by Saurabh Jaiswal


Javascript



输出
60

时间复杂度: O(M * logM) 其中 M 是数组 arr 的大小。
辅助空间: O(1)