给定整数N , N是需要过河的村民人数,但只有一条船,最多可乘2个人。每个人我必须支付一些特定的价格P i才能独自乘船旅行。如果两个人i,j乘船旅行,那么他们必须支付max(P i ,P j ) 。任务是找到所有村民过河所需支付的最低金额。
例子:
Input: Price[] = {30, 40, 60, 70}
Output: 220
P1 and P2 go together (which costs 40)
and P1 comes back (total cost 70 now).
Now P3 and P4 go (total cost 140) and
P2 comes back (total cost 180) and
finally P1 and P2 go together (total cost 220).
Input: Price[] = {892, 124}
Output: 892
方法:两个最昂贵的人过河有两种方法:
- 他们俩轮流与最便宜的人过河。因此,总成本将是两个昂贵人员的成本+ 2 *(最便宜人员的成本)(由于回来)。
- 两个最便宜的人越过河,最便宜的人回来了。现在,第二个最昂贵的人过河而第二个最便宜的人回来了。因此,总成本将是最便宜和最昂贵的人的成本加上第二便宜的人的2 *成本。
- 总费用将是上述两种方法中的最小值。
Let’s consider the example we used above to understand the approach:
P1 = 30, P2 = 40, P3 = 60, P4 = 70
According to first method, P4 goes with P1 and P1 comes back (cost is P4+P1).
Now, P3 goes with P1 and P1 comes back (cost for this ride will be P4+P1).
So, total cost for sending two most costly person according to method 1 is P4+2*P1+P3 = 190
According to second method, P2 goes with P1 and P1 comes back (cost is P2+P1).
Now, P3 goes with P4 and P2 comes back (cost for this ride will be P4+P2).
So, total cost for sending two most costly person according to method 2 is P4+2*P2+P1 = 180
Hence, cost for sending P3 and P4 will be minimum of 2 methods, i.e., 180.
Now, we are left with P1 and P2 whom we have to send together and cost will
be P2 = 40.
So, total cost for travelling is 180 + 40 = 220.
下面是上述方法的实现:
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