📌  相关文章
📜  使用两次操作等于数组所有元素的最小成本

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

使用两次操作等于数组所有元素的最小成本

给定一个包含 n 个正整数的数组 arr[]。允许进行两种操作:

  • 操作1:选择任意两个指标,一个指标加1,另一个指标减1
  • 操作 2:选择任何索引并将其值增加 1。它将花费b

任务是找到使数组中所有元素相等的最小成本。
例子:

Input : n = 4, a = 2, b = 3
        arr[] = { 3, 4, 2, 2 }
Output : 5
Perform operation 2 on 3rd index 
(0 based indexing). It will cost 2.
Perform operation 1 on index 1 (decrease) 
and index 2 (increase). It will cost 3.

Input : n = 3, a = 2, b = 1
        arr[] = { 5, 5, 5 }
Output : 0

方法:观察,最终数组中的元素不会大于给定数组的最大元素,因为增加所有元素是没有意义的。此外,它们将大于原始数组中的最小元素。现在,遍历需要相等的最终数组元素的所有可能值,并检查必须执行多少次第二类型的操作。对于要为 i 的元素(这是最终数组元素的可能值之一),该数字为 (n * i – s),其中 s 是数组中所有数字的总和。最终数组中元素为 i 的第一种类型的操作数可以通过以下方式找到:

for (int j = 0; j < n; j++)
  op1 += max(0, a[j] - i)

在每次迭代结束时,只需检查新值 ans = (n * i – s) * b + op1 * a 是否小于 ans 的先前值。如果小于,更新最终的ans。
下面是上述方法的实现。

C++
// CPP Program to find the minimum cost to equal
// all elements of array using two operation
#include 
using namespace std;
 
// Return the minimum cost required
int minCost(int n, int arr[], int a, int b)
{
    int sum = 0, ans = INT_MAX;
    int maxval = 0;
 
    // finding the maximum element and sum of the array.
    for (int i = 0; i < n; i++) {
        sum += arr[i];
        maxval = max(maxval, arr[i]);
    }
 
    // for each of the possible value
    for (int i = 1; i <= maxval; i++) {
        int op1 = 0;
 
        // finding the number of operation 1 required
        for (int j = 0; j < n; j++)
            op1 += max(0, arr[j] - i);
 
        // finding the minimum cost.
        if (sum <= n * i)
            ans = min(ans, (n * i - sum) * b + op1 * a);
    }
 
    return ans;
}
 
// Driven Code
int main()
{
    int n = 4, a = 2, b = 3;
    int arr[] = { 3, 4, 2, 2 };
 
    cout << minCost(n, arr, a, b) << endl;
    return 0;
}


Java
// Java Program to find the minimum cost
// to equal all elements of array using
// two operation
import java.lang.*;
 
class GFG {
     
    // Return the minimum cost required
    static int minCost(int n, int arr[],
                                 int a, int b)
    {
        int sum = 0, ans = Integer.MAX_VALUE;
        int maxval = 0;
     
        // finding the maximum element and
        // sum of the array.
        for (int i = 0; i < n; i++) {
            sum += arr[i];
            maxval = Math.max(maxval, arr[i]);
        }
     
        // for each of the possible value
        for (int i = 1; i <= maxval; i++) {
            int op1 = 0;
     
            // finding the number of operation
            // 1 required
            for (int j = 0; j < n; j++)
                op1 += Math.max(0, arr[j] - i);
     
            // finding the minimum cost.
            if (sum <= n * i)
                ans = Math.min(ans, (n * i - sum)
                                  * b + op1 * a);
        }
     
        return ans;
    }
     
    // Driven Code
    public static void main(String [] args)
    {
        int n = 4, a = 2, b = 3;
        int arr[] = { 3, 4, 2, 2 };
     
        System.out.println(minCost(n, arr, a, b));
    }
}
 
// This code is contributed by Smitha.


python3
# Python 3 Program to find the minimum
# cost to equal all elements of array
# using two operation
import sys
 
# Return the minimum cost required
def minCost(n, arr, a, b):
 
    sum = 0
    ans = sys.maxsize
    maxval = 0
 
    # finding the maximum element and
    # sum of the array.
    for i in range(0, n) :
        sum += arr[i]
        maxval = max(maxval, arr[i])
     
 
    # for each of the possible value
    for i in range(0, n) :
        op1 = 0
 
        # finding the number of operation
        # 1 required
        for j in range(0, n) :
            op1 += max(0, arr[j] - i)
 
        # finding the minimum cost.
        if (sum <= n * i):
            ans = min(ans, (n * i - sum)
                          * b + op1 * a)
 
    return ans
 
# Driven Code
n = 4
a = 2
b = 3
arr = [3, 4, 2, 2]
print(minCost(n, arr, a, b))
 
# This code is contributed by Smitha


C#
// C# Program to find the minimum
// cost to equal all elements of
// array using two operation
using System;
 
class GFG {
     
    // Return the minimum cost required
    static int minCost(int n, int [] arr,
                            int a, int b)
    {
        int sum = 0, ans = int.MaxValue;
        int maxval = 0;
     
        // finding the maximum element and
        // sum of the array.
        for (int i = 0; i < n; i++) {
            sum += arr[i];
            maxval = Math.Max(maxval, arr[i]);
        }
     
        // for each of the possible value
        for (int i = 1; i <= maxval; i++) {
            int op1 = 0;
     
            // finding the number of operation
            // 1 required
            for (int j = 0; j < n; j++)
                op1 += Math.Max(0, arr[j] - i);
     
            // finding the minimum cost.
            if (sum <= n * i)
                ans = Math.Min(ans, (n * i - sum)
                                  * b + op1 * a);
        }
     
        return ans;
    }
     
    // Driven Code
    public static void Main()
    {
        int n = 4, a = 2, b = 3;
        int []arr= { 3, 4, 2, 2 };
     
        Console.Write(minCost(n, arr, a, b));
    }
}
 
// This code is contributed by Smitha


PHP


Javascript


输出:
5