📌  相关文章
📜  最大化位于给定范围内的长度子序列的平均值之和

📅  最后修改于: 2021-10-26 05:41:37             🧑  作者: Mango

给定一个由N 个整数和两个整数XY组成的数组A[] ,任务是找到通过将数组拆分为长度在[X, Y]范围内的子序列而获得的每个子序列的平均值的最大和。

注意: XY的值总是可以组成这样的组。

例子:

方法:可以使用贪心方法解决给定的问题。这个想法是按升序对数组进行排序并选择大小为X的组,因为平均值与元素数量成反比。最后,将剩余元素添加到最后一组。请按照以下步骤解决问题:

  • 按升序对给定的数组arr[]进行排序。
  • 将变量sum、rescount初始化为0,以存储数组元素的总和、结果和当前组中元素的计数。
  • 使用变量i在范围[0, N] 上迭代并执行以下步骤:
    • arr[i]的值添加到变量sum并将count的值增加1
    • 如果count的值等于X ,并且剩余的数组元素不能组成一个组,则将它们添加到当前组并在变量res 中添加平均值。
    • 否则,将迄今为止形成的组的平均值添加到变量res 中
  • 完成以上步骤后,打印res的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum sum
// of average of groups
void maxAverage(int A[], int N, int X,
                int Y)
{
    // Sort the given array
    sort(A, A + N);
 
    int sum = 0;
 
    // Stores the sum of averages
    double res = 0;
 
    // Stores count of array element
    int count = 0;
 
    for (int i = 0; i < N; i++) {
 
        // Add the current value to
        // the variable sum
        sum += A[i];
 
        // Increment the count by 1
        count++;
 
        // If the current size is X
        if (count == X) {
 
            // If the remaining elements
            // can't become a group
            if (N - i - 1 < X) {
                i++;
                int cnt = 0;
 
                // Iterate until i is
                // less than N
                while (i < N) {
                    cnt++;
                    sum += A[i];
                    i++;
                }
 
                // Update the value of X
                X = X + cnt;
 
                // Update the average
                res += (double)sum / double(X);
                break;
            }
 
            // Find the average
            res += (double)sum / double(X);
 
            // Reset the sum and count
            sum = 0;
            count = 0;
        }
    }
 
    // Print maximum sum of averages
    cout << fixed << setprecision(2)
         << res << "\n";
}
 
// Driver Code
int main()
{
    int A[] = { 4, 10, 6, 5 };
    int N = sizeof(A) / sizeof(A[0]);
    int X = 2, Y = 3;
 
    maxAverage(A, N, X, Y);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum sum
// of average of groups
static void maxAverage(int A[], int N, int X,
                       int Y)
{
     
    // Sort the given array
    Arrays.sort(A);
 
    int sum = 0;
 
    // Stores the sum of averages
    double res = 0;
 
    // Stores count of array element
    int count = 0;
 
    for(int i = 0; i < N; i++)
    {
         
        // Add the current value to
        // the variable sum
        sum += A[i];
 
        // Increment the count by 1
        count++;
 
        // If the current size is X
        if (count == X)
        {
             
            // If the remaining elements
            // can't become a group
            if (N - i - 1 < X)
            {
                i++;
                int cnt = 0;
 
                // Iterate until i is
                // less than N
                while (i < N)
                {
                    cnt++;
                    sum += A[i];
                    i++;
                }
 
                // Update the value of X
                X = X + cnt;
 
                // Update the average
                res += (double)sum / (double)(X);
                break;
            }
 
            // Find the average
            res += (double)sum / (double)(X);
 
            // Reset the sum and count
            sum = 0;
            count = 0;
        }
    }
 
    // Print maximum sum of averages
    System.out.println(res);
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 4, 10, 6, 5 };
    int N = A.length;
    int X = 2, Y = 3;
 
    maxAverage(A, N, X, Y);
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python 3 program for the above approach
 
# Function to find the maximum sum
# of average of groups
def maxAverage(A,N,X,Y):
    # Sort the given array
    A.sort()
 
    sum = 0
 
    # Stores the sum of averages
    res = 0
 
    # Stores count of array element
    count = 0
 
    for i in range(N):
        # Add the current value to
        # the variable sum
        sum += A[i]
 
        # Increment the count by 1
        count += 1
 
        # If the current size is X
        if (count == X):
 
            # If the remaining elements
            # can't become a group
            if (N - i - 1 < X):
                i += 1
                cnt = 0
 
                # Iterate until i is
                # less than N
                while (i < N):
                    cnt += 1
                    sum += A[i]
                    i += 1
 
                # Update the value of X
                X = X + cnt
 
                # Update the average
                res += sum / X
                break
 
            # Find the average
            res += sum / X
 
            # Reset the sum and count
            sum = 0
            count = 0
 
    # Print maximum sum of averages
    print(res)
 
# Driver Code
if __name__ == '__main__':
    A = [4, 10, 6, 5]
    N = len(A)
    X = 2
    Y = 3
 
    maxAverage(A, N, X, Y)
 
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
 
public class GFG{
 
// Function to find the maximum sum
// of average of groups
static void maxAverage(int []A, int N, int X,
                       int Y)
{
     
    // Sort the given array
    Array.Sort(A);
 
    int sum = 0;
 
    // Stores the sum of averages
    double res = 0;
 
    // Stores count of array element
    int count = 0;
 
    for(int i = 0; i < N; i++)
    {
         
        // Add the current value to
        // the variable sum
        sum += A[i];
 
        // Increment the count by 1
        count++;
 
        // If the current size is X
        if (count == X)
        {
             
            // If the remaining elements
            // can't become a group
            if (N - i - 1 < X)
            {
                i++;
                int cnt = 0;
 
                // Iterate until i is
                // less than N
                while (i < N)
                {
                    cnt++;
                    sum += A[i];
                    i++;
                }
 
                // Update the value of X
                X = X + cnt;
 
                // Update the average
                res += (double)sum / (double)(X);
                break;
            }
 
            // Find the average
            res += (double)sum / (double)(X);
 
            // Reset the sum and count
            sum = 0;
            count = 0;
        }
    }
 
    // Print maximum sum of averages
    Console.WriteLine(res);
}
 
// Driver Code
public static void Main(String[] args)
{
    int []A = { 4, 10, 6, 5 };
    int N = A.Length;
    int X = 2, Y = 3;
 
    maxAverage(A, N, X, Y);
}
}
 
  
 
// This code is contributed by 29AjayKumar


Javascript


输出:
12.50

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

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