📜  找到过河的最低费用

📅  最后修改于: 2021-05-06 19:25:17             🧑  作者: Mango

给定整数NN是需要过河的村民人数,但只有一条船,最多可乘2个人。每个人必须支付一些特定的价格P i才能独自乘船旅行。如果两个人i,j乘船旅行,那么他们必须支付max(P i ,P j ) 。任务是找到所有村民过河所需支付的最低金额。

例子:

方法:两个最昂贵的人过河有两种方法:

  • 他们俩轮流与最便宜的人过河。因此,总成本将是两个昂贵人员的成本+ 2 *(最便宜人员的成本)(由于回来)。
  • 两个最便宜的人越过河,最便宜的人回来了。现在,第二个最昂贵的人过河而第二个最便宜的人回来了。因此,总成本将是最便宜和最昂贵的人的成本加上第二便宜的人的2 *成本。
  • 总费用将是上述两种方法中的最小值。

下面是上述方法的实现:

CPP
// C++ implementation of the approach
#include 
using namespace std;
  
#define ll long long int
  
// Function to return the minimum cost
int minimumCost(ll price[], int n)
{
  
    // Sort the price array
    sort(price, price + n);
    ll totalCost = 0;
  
    // Calcualte minimum price
    // of n-2 most costly person
    for (int i = n - 1; i > 1; i -= 2) {
        if (i == 2) {
            totalCost += price[2] + price[0];
        }
        else {
  
            // Both the ways as discussed above
            ll price_first = price[i] + price[0] + 2 * price[1];
            ll price_second = price[i] + price[i - 1] + 2 * price[0];
            totalCost += min(price_first, price_second);
        }
    }
  
    // Calculate the minimum price
    // of the two cheapest person
    if (n == 1) {
        totalCost += price[0];
    }
    else {
        totalCost += price[1];
    }
  
    return totalCost;
}
  
// Driver code
int main()
{
    ll price[] = { 30, 40, 60, 70 };
    int n = sizeof(price) / sizeof(price[0]);
  
    cout << minimumCost(price, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
      
    // Function to return the minimum cost
    static long minimumCost(long price[], int n)
    {
      
        // Sort the price array
        Arrays.sort(price);
          
        long totalCost = 0;
      
        // Calcualte minimum price
        // of n-2 most costly person
        for (int i = n - 1; i > 1; i -= 2)
        {
            if (i == 2)
            {
                totalCost += price[2] + price[0];
            }
            else
            {
      
                // Both the ways as discussed above
                long price_first = price[i] + price[0] + 2 * price[1];
                long price_second = price[i] + price[i - 1] + 2 * price[0];
                totalCost += Math.min(price_first, price_second);
            }
        }
      
        // Calculate the minimum price
        // of the two cheapest person
        if (n == 1)
        {
            totalCost += price[0];
        }
        else
        {
            totalCost += price[1];
        }
      
        return totalCost;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        long price[] = { 30, 40, 60, 70 };
        int n = price.length;
      
        System.out.println(minimumCost(price, n));
    }
}
  
// This code is contributed by AnkitRai01


Python
# Python3 implementation of the approach
  
# Function to return the minimum cost
def minimumCost(price, n):
  
    # Sort the price array
    price = sorted(price)
    totalCost = 0
  
    # Calcualte minimum price
    # of n-2 most costly person
    for i in range(n - 1, 1, -2):
        if (i == 2):
            totalCost += price[2] + price[0]
  
        else:
  
            # Both the ways as discussed above
            price_first = price[i] + price[0] + 2 * price[1]
            price_second = price[i] + price[i - 1] + 2 * price[0]
            totalCost += min(price_first, price_second)
  
    # Calculate the minimum price
    # of the two cheapest person
    if (n == 1):
        totalCost += price[0]
  
    else:
        totalCost += price[1]
  
    return totalCost
  
# Driver code
  
price = [30, 40, 60, 70]
n = len(price)
  
print(minimumCost(price, n))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
    // Function to return the minimum cost
    static long minimumCost(long []price, int n)
    {
      
        // Sort the price array
        Array.Sort(price);
          
        long totalCost = 0;
      
        // Calcualte minimum price
        // of n-2 most costly person
        for (int i = n - 1; i > 1; i -= 2)
        {
            if (i == 2)
            {
                totalCost += price[2] + price[0];
            }
            else
            {
      
                // Both the ways as discussed above
                long price_first = price[i] + price[0] + 2 * price[1];
                long price_second = price[i] + price[i - 1] + 2 * price[0];
                totalCost += Math.Min(price_first, price_second);
            }
        }
      
        // Calculate the minimum price
        // of the two cheapest person
        if (n == 1)
        {
            totalCost += price[0];
        }
        else
        {
            totalCost += price[1];
        }
      
        return totalCost;
    }
      
    // Driver code
    public static void Main ()
    {
        long []price = { 30, 40, 60, 70 };
        int n = price.Length;
      
        Console.WriteLine(minimumCost(price, n));
    }
}
  
// This code is contributed by AnkitRai01


输出:
220