给定两个正整数,X和Y和阵列ARR []由N的正整数,使得ARR [I]代表第i人的身高和有高度H的隧道,任务是找到总的最小成本要求让所有N 个人通过给定的隧道,根据以下规则,最多可以同时通过两个身高总和小于H 的人:
- 当两个人同时通过隧道时,成本为Y 。
- 当一个人一次穿过隧道时,成本为X 。
注意:所有数组元素都小于H 。
例子:
Input: arr[] = {1, 3, 4, 4, 2}, X = 4, Y = 6, H = 9
Output: 16
Explanation:
Consider the passing of persons according to the below order:
- Person 1 and Person 4 having heights 1 and 4 respectively has the sum of heights as 1 + 4 = 5 < H(= 9). Therefore, the cost for this operation is Y(= 6).
- Person 2 and Person 3 having heights 3 and 4 respectively has the sum of heights as 3 + 4 = 7 < H(= 9). Therefore, the cost for this operation is Y(= 6).
- Person 5 has height 3 which is less than H(= 9). Therefore, the cost for this operation is is X( = 4).
Therefore, the total cost is 6 + 6 + 4 = 16, which is minimum among all possible combinations.
Input: arr[] = {1, 3, 4}, X = 4, Y = 6, H = 9
Output: 10
方法:给定的问题可以通过使用贪婪方法和使用双指针技术来解决。这个想法是选择身高总和小于H 的那两个人,代价是Y 。否则,在两人中选择身高最大的人,以X为代价将他们送入隧道。请按照以下步骤解决问题:
- 按升序对给定数组arr[]进行排序。
- 初始化两个指针,比如i和j 分别为0和(N – 1)以指向数组的末端。
- 迭代直到i的值小于j并执行以下步骤:
- 如果arr[i]和arr[j]的值之和小于H ,则将cost的值增加Y并增加i的值并将j的值减少1 。
- 否则,将j的值减1并将cost的值更新X 。
- 如果i和j的值相等,则将 cost 的值增加X 。
- 完成上述步骤后,打印成本值作为结果最小成本。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Function to find the minimum total
// cost of passing at most two-person
// at a time through the tunnel
int minimumCost(int arr[], int N, int H,
int X, int Y)
{
// Stores the resultant cost
int cost = 0;
// Sort the given array
sort(arr, arr + N);
// Initialize two pointers
int i = 0, j = N - 1;
// Iterate until i is less than j
while (i < j) {
// If the sum of values at
// i and j is less than H
if (arr[i] + arr[j] < H) {
// Increment the cost
cost += Y;
// Update the pointers
i++;
j--;
}
// Otherwise
else {
cost += X;
j--;
}
}
// If i and j points to the same
// element, then that person is
// not passed to the tunnel
if (i == j)
cost += X;
// Return the minimum of the total
// cost and cost of passing all the
// person individually
return min(cost, N * H);
}
// Driver Code
int main()
{
int arr[] = { 1, 3, 4, 4, 2 };
int X = 4, Y = 6, H = 9;
int N = sizeof(arr) / sizeof(arr[0]);
cout << minimumCost(arr, N, H, X, Y);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.Arrays;
class GFG{
// Function to find the minimum total
// cost of passing at most two-person
// at a time through the tunnel
public static int minimumCost(int arr[], int N, int H,
int X, int Y)
{
// Stores the resultant cost
int cost = 0;
// Sort the given array
Arrays.sort(arr);
// Initialize two pointers
int i = 0, j = N - 1;
// Iterate until i is less than j
while (i < j)
{
// If the sum of values at
// i and j is less than H
if (arr[i] + arr[j] < H)
{
// Increment the cost
cost += Y;
// Update the pointers
i++;
j--;
}
// Otherwise
else
{
cost += X;
j--;
}
}
// If i and j points to the same
// element, then that person is
// not passed to the tunnel
if (i == j)
cost += X;
// Return the minimum of the total
// cost and cost of passing all the
// person individually
return Math.min(cost, N * H);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 3, 4, 4, 2 };
int X = 4, Y = 6, H = 9;
int N = arr.length;
System.out.println(minimumCost(arr, N, H, X, Y));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python 3 program for the above approach
# Function to find the minimum total
# cost of passing at most two-person
# at a time through the tunnel
def minimumCost(arr, N, H, X, Y):
# Stores the resultant cost
cost = 0
# Sort the given array
arr.sort()
# Initialize two pointers
i = 0
j = N - 1
# Iterate until i is less than j
while (i < j):
# If the sum of values at
# i and j is less than H
if (arr[i] + arr[j] < H):
# Increment the cost
cost += Y
# Update the pointers
i += 1
j -= 1
# Otherwise
else:
cost += X
j -= 1
# If i and j points to the same
# element, then that person is
# not passed to the tunnel
if (i == j):
cost += X
# Return the minimum of the total
# cost and cost of passing all the
# person individually
return min(cost, N * H)
# Driver Code
if __name__ == '__main__':
arr = [1, 3, 4, 4, 2]
X = 4
Y = 6
H = 9
N = len(arr)
print(minimumCost(arr, N, H, X, Y))
# This code is contributed by bgangwar59.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the minimum total
// cost of passing at most two-person
// at a time through the tunnel
static int minimumCost(int[] arr, int N, int H, int X,
int Y)
{
// Stores the resultant cost
int cost = 0;
// Sort the given array
Array.Sort(arr);
// Initialize two pointers
int i = 0, j = N - 1;
// Iterate until i is less than j
while (i < j) {
// If the sum of values at
// i and j is less than H
if (arr[i] + arr[j] < H) {
// Increment the cost
cost += Y;
// Update the pointers
i++;
j--;
}
// Otherwise
else {
cost += X;
j--;
}
}
// If i and j points to the same
// element, then that person is
// not passed to the tunnel
if (i == j)
cost += X;
// Return the minimum of the total
// cost and cost of passing all the
// person individually
return Math.Min(cost, N * H);
}
// Driver code
public static void Main()
{
int[] arr = { 1, 3, 4, 4, 2 };
int X = 4, Y = 6, H = 9;
int N = arr.Length;
Console.WriteLine(minimumCost(arr, N, H, X, Y));
}
}
// This code is contributed by subhammahato348.
Javascript
输出
16
时间复杂度: O(N*log N)
辅助空间: O(1)