📌  相关文章
📜  最大化由给定数组形成的四元组中的第三个元素和

📅  最后修改于: 2021-09-04 11:22:45             🧑  作者: Mango

给定一个包含N 个值的数组arr ,描述 N 个作业的优先级。任务是形成每天要完成的四元组 (W, X, Y, Z) 的集合,使得 W >= X >= Y >= Z 并且在这样做时,最大化所有四元组中所有 Y 的总和。

注意:N 将始终是 4 的倍数。

例子:

方法:为了解决上述问题,我们可以观察到:

  1. 与 Y 无关,(W, X) >= Y,即 W 和 X 的较高值总是丢失并且对答案没有贡献。因此,我们必须保持这些值尽可能低但大于或等于 Y。
  2. 同样,Z 的值总是丢失并且必须小于 Y。因此,它必须尽可能低。

因此,要满足上述条件,我们必须:

  • 首先按降序对给定数组进行排序。
  • 初始化一个指针,该指针指向从索引 0 开始的每对的第三个元素。
  • 从数组的大小中减去这些对的数量,即 N。

下面是上述方法的实现:

C++
// C++ code to Maximize 3rd element
// sum in quadruplet sets formed
// from given Array
  
#include 
using namespace std;
  
// Function to find the maximum
// possible value of Y
int formQuadruplets(int arr[], int n)
{
  
    int ans = 0, pairs = 0;
  
    // pairs contain count
    // of minimum elements
    // that will be utilized
    // at place of Z.
    // it is equal to count of
    // possible pairs that
    // is size of array divided by 4
    pairs = n / 4;
  
    // sorting the array in descending order
    // so as to bring values with minimal
    // difference closer to arr[i]
    sort(arr, arr + n, greater());
  
    for (int i = 0; i < n - pairs; i += 3) {
  
        // here, i+2 acts as a
        // pointer that points
        // to the third value of
        // every possible quadruplet
        ans += arr[i + 2];
    }
  
    // returning the optimally
    // maximum possible value
    return ans;
}
  
// Driver code
int main()
{
    // array declaration
    int arr[] = { 2, 1, 7, 5, 5,
                  4, 1, 1, 3, 3,
                  2, 2 };
  
    // size of array
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << formQuadruplets(arr, n)
         << endl;
  
    return 0;
}


Java
// Java code to Maximize 3rd element
// sum in quadruplet sets formed
// from given Array
import java.util.*;
class GFG{
  
// Function to find the maximum
// possible value of Y
static int formQuadruplets(Integer arr[], int n)
{
    int ans = 0, pairs = 0;
  
    // pairs contain count
    // of minimum elements
    // that will be utilized
    // at place of Z.
    // it is equal to count of
    // possible pairs that
    // is size of array divided by 4
    pairs = n / 4;
  
    // sorting the array in descending order
    // so as to bring values with minimal
    // difference closer to arr[i]
    Arrays.sort(arr, Collections.reverseOrder());
  
    for (int i = 0; i < n - pairs; i += 3) 
    {
  
        // here, i+2 acts as a
        // pointer that points
        // to the third value of
        // every possible quadruplet
        ans += arr[i + 2];
    }
  
    // returning the optimally
    // maximum possible value
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    // array declaration
    Integer arr[] = { 2, 1, 7, 5, 5, 4, 
                      1, 1, 3, 3, 2, 2 };
  
    // size of array
    int n = arr.length;
  
    System.out.print(
           formQuadruplets(arr, n) + "\n");
  
}
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 code to maximize 3rd element 
# sum in quadruplet sets formed 
# from given Array 
  
# Function to find the maximum 
# possible value of Y 
def formQuadruplets(arr, n): 
  
    ans = 0
    pairs = 0
  
    # Pairs contain count of minimum
    # elements that will be utilized 
    # at place of Z. It is equal to  
    # count of possible pairs that 
    # is size of array divided by 4 
    pairs = n // 4
  
    # Sorting the array in descending order 
    # so as to bring values with minimal 
    # difference closer to arr[i] 
    arr.sort(reverse = True)
  
    for i in range(0, n - pairs, 3): 
  
        # Here, i+2 acts as a pointer that  
        # points to the third value of 
        # every possible quadruplet 
        ans += arr[i + 2] 
  
    # Returning the optimally 
    # maximum possible value 
    return ans 
  
# Driver code 
  
# Array declaration 
arr = [ 2, 1, 7, 5, 5, 4, 1, 1, 3, 3, 2, 2 ] 
  
# Size of array 
n = len(arr) 
  
print(formQuadruplets(arr, n)) 
  
# This code is contributed by divyamohan123


C#
// C# code to maximize 3rd element
// sum in quadruplet sets formed
// from given Array
using System;
  
class GFG{
  
// Function to find the maximum
// possible value of Y
static int formQuadruplets(int []arr, int n)
{
    int ans = 0, pairs = 0;
  
    // Pairs contain count of minimum  
    // elements that will be utilized at 
    // place of Z. It is equal to count of
    // possible pairs that is size of 
    // array divided by 4
    pairs = n / 4;
  
    // Sorting the array in descending order
    // so as to bring values with minimal
    // difference closer to arr[i]
    Array.Sort(arr);
    Array.Reverse(arr);
    for(int i = 0; i < n - pairs; i += 3) 
    {
         
       // Here, i+2 acts as a
       // pointer that points
       // to the third value of
       // every possible quadruplet
       ans += arr[i + 2];
    }
  
    // Returning the optimally
    // maximum possible value
    return ans;
}
  
// Driver code
public static void Main(String[] args)
{
      
    // Array declaration
    int []arr = { 2, 1, 7, 5, 5, 4, 
                  1, 1, 3, 3, 2, 2 };
  
    // Size of array
    int n = arr.Length;
  
    Console.Write(formQuadruplets(arr, n) + "\n");
}
}
  
// This code is contributed by amal kumar choubey


输出:
10

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live