📜  以 M 为增量最大化 N 对比率的平均值

📅  最后修改于: 2021-10-28 02:03:39             🧑  作者: Mango

给定一个由N对和一个正整数M组成的数组 arr[] ,任务是通过将任何对的第一个和第二个元素增加1 M次来最大化对比率的平均值。

例子:

方法:给定的问题可以通过使用 Max-heap 在对上应用操作来存储比率的增加,然后继续从堆中弹出值 M次并添加到总和中来解决给定的问题。请按照以下步骤解决问题:

  • 初始化一个优先级队列,比如PQ对存储相应的平均变化和对的索引,如果对它应用了一个操作。
  • 初始化一个变量,比如sum0来存储对比率的最大平均值。
  • 遍历给定的数组对arr[]并找到对的两个值都加 1后比率的增加,并将值推送到优先级队列PQ 。此外,一对添加的第i的比率变量sum。
  • 迭代直到M 的值为正,然后执行以下步骤:
    • 弹出优先队列PQ的顶部元素并将比率的增加添加到总和
    • 更新该对的值并推动该对在优先级队列PQ中的比率的新增加。
  • 完成上述步骤后,打印总和除以N的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the change in the
// ratio in pair after applying operation
double change(int pass, int total)
{
    double currentPassRatio, newPassRatio;
    double increase;
 
    // Stores the current ratio
    currentPassRatio = ((double)pass)
                       / total;
 
    // Stores the new ratio
    newPassRatio = ((double)(pass + 1))
                   / (total + 1);
 
    // Stores the increase in ratio
    increase = newPassRatio
               - currentPassRatio;
 
    // Returns the change
    return increase;
}
 
// Function to find the maximum
// average of the ratio of the
// pairs by applying M increments
double maximumAverage(
    vector > v, int M,
    int N)
{
    // Stores the required result
    double sum = 0;
 
    double increase, average;
 
    // Declare a priority queue
    // for storing the increments
    priority_queue > pq;
    for (int i = 0; i < N; i++) {
 
        // Store the increase in the ratio
        // after applying one operation
        increase = change(v[i][0], v[i][1]);
 
        // Push the increased value and
        // index value in priority queue
        pq.push({ increase, i });
 
        // Store the ratio
        average = v[i][0] * 1.0 / v[i][1];
 
        // Update the value of sum
        sum += average;
    }
 
    // Iterate while M > 0
    while (M > 0) {
 
        // Add the maximum change
        // to the sum
        sum += pq.top().first;
 
        int i = pq.top().second;
 
        // Remove the element from
        // the priority queue
        pq.pop();
 
        // Increase the pairs elements
        // by 1 on which operation
        // is applied
        v[i][0] += 1;
        v[i][1] += 1;
 
        // Push the updated change of
        // the pair in priority queue
        pq.push({ change(v[i][0], v[i][1]), i });
 
        // Decrease the operation count
        M--;
    }
 
    // Update the value of the sum by
    // dividing it by N
    double ans = sum / N;
 
    // Return the result
    return ans;
}
 
// Driver Code
int main()
{
    vector > V
        = { { 1, 2 }, { 3, 5 }, { 2, 2 } };
    int M = 2;
    int N = V.size();
    cout << maximumAverage(V, M, N);
 
    return 0;
}


Python3
# python program for the above approach
 
# Function to find the change in the
# ratio in pair after applying operation
def change(pas, total):
 
    # Stores the current ratio
    currentPassRatio = pas/ total
 
    # Stores the new ratio
    newPassRatio = (pas + 1) / (total + 1)
 
    # Stores the increase in ratio
    increase = newPassRatio - currentPassRatio
 
    # Returns the change
    return increase
 
# Function to find the maximum
# average of the ratio of the
# pairs by applying M increments
def maximumAverage(v, M, N):
   
    # Stores the required result
    sum = 0
 
    increase, average = 0, 0
 
    # Declare a priority queue
    # for storing the increments
    pq = []
    for i in range(N):
        # Store the increase in the ratio
        # after applying one operation
        increase = change(v[i][0], v[i][1])
 
        # Push the increased value and
        # index value in priority queue
        pq.append([increase, i ])
 
        # Store the ratio
        average = v[i][0] * 1.0 / v[i][1]
 
        # Update the value of sum
        sum += average
    pq = sorted(pq)
 
    # Iterate while M > 0
    while (M > 0):
 
        # Add the maximum change
        # to the sum
        sum += pq[-1][0]
 
        i = pq[-1][1]
 
        # Remove the element from
        # the priority queue
        del pq[-1]
 
        # Increase the pairs elements
        # by 1 on which operation
        # is applied
        v[i][0] += 1
        v[i][1] += 1
 
        # Push the updated change of
        # the pair in priority queue
        pq.append([change(v[i][0], v[i][1]), i])
 
        # Decrease the operation count
        M -= 1
        pq = sorted(pq)
 
    # Update the value of the sum by
    # dividing it by N
    ans = sum / N
 
    # Return the result
    return ans
 
# Driver Code
if __name__ == '__main__':
    V =  [[1, 2],  [3, 5],  [2, 2]]
    M = 2
    N = len(V)
    print (round(maximumAverage(V, M, N),6))
 
    # This code is contributed by mohit kumar 29.


Javascript


输出:
0.783333

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

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