📌  相关文章
📜  空数组的最小成本,其中删除元素的成本为 2^(removed_count) * arr[i]

📅  最后修改于: 2021-10-26 06:39:08             🧑  作者: Mango

给定一个数组arr[],任务是找到从数组中删除所有元素的最小成本,其中删除元素的成本是2^j * arr[i]。这里,j 是已经被删除的元素的数量。

例子:

方法:思路是用贪心的编程范式来解决这个问题。  
我们必须最小化表达式( 2^j * arr[i] )。这可以通过以下方式完成:

  • 按降序对数组进行排序。
  • 将 pow(2, i) 与每个元素 i 相乘,从 0 开始到数组的大小。

因此,从数组中删除元素的总成本为:

\text{Total Cost = }arr[0]*2^{0} + arr[1] * 2^{1} + .... arr[n]*2^{n}

当数组按降序排列时。

下面是上述方法的实现:

C++
// C++ implementation to find the
// minimum cost of removing all
// elements from the array
 
#include 
using namespace std;
 
#define ll long long int
// Function to find the minimum
// cost of removing elements from
// the array
int removeElements(ll arr[], int n)
{
 
    // Sorting in Increasing order
    sort(arr, arr + n, greater());
    ll ans = 0;
     
    // Loop to find the minimum
    // cost of removing elements
    for (int i = 0; i < n; i++) {
        ans += arr[i] * pow(2, i);
    }
 
    return ans;
}
 
// Driver Code
int main()
{
    int n = 4;
    ll arr[n] = { 3, 1, 2, 3 };
 
    // Function Call
    cout << removeElements(arr, n);
}


Java
// Java implementation to find the
// minimum cost of removing all
// elements from the array
import java.util.*;
 
class GFG{
 
// Reverse array in decreasing order
static long[] reverse(long a[])
{
    int i, n = a.length;
    long t;
     
    for(i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
}
 
// Function to find the minimum
// cost of removing elements from
// the array
static long removeElements(long arr[],
                           int n)
{
     
    // Sorting in Increasing order
    Arrays.sort(arr);
    arr = reverse(arr);
 
    long ans = 0;
 
    // Loop to find the minimum
    // cost of removing elements
    for(int i = 0; i < n; i++)
    {
        ans += arr[i] * Math.pow(2, i);
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 4;
    long arr[] = { 3, 1, 2, 3 };
 
    // Function call
    System.out.print(removeElements(arr, n));
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 implementation to find the
# minimum cost of removing all
# elements from the array
 
# Function to find the minimum
# cost of removing elements from
# the array
def removeElements(arr, n):
 
    # Sorting in Increasing order
    arr.sort(reverse = True)
    ans = 0
 
    # Loop to find the minimum
    # cost of removing elements
    for i in range(n):
        ans += arr[i] * pow(2, i)
 
    return ans
 
# Driver Code
if __name__ == "__main__":
     
    n = 4
    arr = [ 3, 1, 2, 3 ]
 
    # Function call
    print(removeElements(arr, n))
     
# This code is contributed by chitranayal


C#
// C# implementation to find the
// minimum cost of removing all
// elements from the array
using System;
 
class GFG{
 
// Reverse array in decreasing order
static long[] reverse(long []a)
{
    int i, n = a.Length;
    long t;
     
    for(i = 0; i < n / 2; i++)
    {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
}
 
// Function to find the minimum
// cost of removing elements from
// the array
static long removeElements(long []arr,
                           int n)
{
     
    // Sorting in Increasing order
    Array.Sort(arr);
    arr = reverse(arr);
 
    long ans = 0;
 
    // Loop to find the minimum
    // cost of removing elements
    for(int i = 0; i < n; i++)
    {
        ans += (long)(arr[i] * Math.Pow(2, i));
    }
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 4;
    long []arr = { 3, 1, 2, 3 };
 
    // Function call
    Console.Write(removeElements(arr, n));
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出
25

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