📌  相关文章
📜  允许两种不同的操作,从 0 到 N 点的最小成本

📅  最后修改于: 2021-09-17 16:12:19             🧑  作者: Mango

给定整数N、P 和 Q ,其中N表示目标位置。任务是以尽可能低的成本从位置0移动到位置N并打印计算出的成本。所有有效的动作是:

  1. 您可以从位置X转到位置X + 1 ,成本为P
  2. 或者,您可以以Q的成本转到位置2 * X

例子:

方法:我们可以开始从目的地移动到初始位置,并跟踪跳跃的成本,而不是从开始到目的地。

  • 如果N是奇数,那么唯一可以引导我们到这里的有效移动是N-1 到 N ,成本为P
  • 如果N是偶数,那么我们计算从N 到 N/2位置的成本,并取其中的最小值。
  • N等于0 时,我们返回计算的总成本。

下面是上述方法的实现:

C++
// CPP implementation of above approach
 
#include 
using namespace std;
 
// Function to return minimum
// cost to reach destination
int minCost(int N, int P, int Q)
{
    // Initialize cost to 0
    int cost = 0;
 
    // going backwards until we
    // reach initial position
    while (N > 0) {
 
        if (N & 1) {
            cost += P;
            N--;
        }
        else {
            int temp = N / 2;
 
            // if 2*X jump is
            // better than X+1
            if (temp * P > Q)
                cost += Q;
            // if X+1 jump is better
            else
                cost += P * temp;
 
            N /= 2;
        }
    }
 
    // return cost
    return cost;
}
 
// Driver program
int main()
{
    int N = 9, P = 5, Q = 1;
 
    cout << minCost(N, P, Q);
 
    return 0;
}


Java
// Java implementation of above approach
 
class GFG{
// Function to return minimum
// cost to reach destination
static int minCost(int N, int P, int Q)
{
    // Initialize cost to 0
    int cost = 0;
 
    // going backwards until we
    // reach initial position
    while (N > 0) {
 
        if ((N & 1)>0) {
            cost += P;
            N--;
        }
        else {
            int temp = N / 2;
 
            // if 2*X jump is
            // better than X+1
            if (temp * P > Q)
                cost += Q;
            // if X+1 jump is better
            else
                cost += P * temp;
 
            N /= 2;
        }
    }
 
    // return cost
    return cost;
}
 
// Driver program
public static void main(String[] args)
{
    int N = 9, P = 5, Q = 1;
 
    System.out.println(minCost(N, P, Q));
}
}
// This code is contributed by mits


Python3
# Python implementation of above approach
 
 
# Function to return minimum
# cost to reach destination
 
def minCost(N,P,Q):
    # Initialize cost to 0
    cost = 0
   
    # going backwards until we
    # reach initial position
    while (N > 0): 
        if (N & 1):
            cost += P
            N-=1
        else:
            temp = N // 2;
   
            # if 2*X jump is
            # better than X+1
            if (temp * P > Q):
                cost += Q
            # if X+1 jump is better
            else:
                cost += P * temp
            N //= 2
    return cost
 
   
# Driver program
N = 9
P = 5
Q = 1
print(minCost(N, P, Q))
#this code is improved by sahilshelangia


C#
// C# implementation of above approach
 
class GFG
{
// Function to return minimum
// cost to reach destination
static int minCost(int N, int P, int Q)
{
    // Initialize cost to 0
    int cost = 0;
 
    // going backwards until we
    // reach initial position
    while (N > 0)
    {
 
        if ((N & 1) > 0)
        {
            cost += P;
            N--;
        }
        else
        {
            int temp = N / 2;
 
            // if 2*X jump is
            // better than X+1
            if (temp * P > Q)
                cost += Q;
                 
            // if X+1 jump is better
            else
                cost += P * temp;
 
            N /= 2;
        }
    }
 
    // return cost
    return cost;
}
 
// Driver Code
static void Main()
{
    int N = 9, P = 5, Q = 1;
 
    System.Console.WriteLine(minCost(N, P, Q));
}
}
 
// This code is contributed by mits


PHP
 0)
    {
 
        if ($N & 1)
        {
            $cost += $P;
            $N--;
        }
        else
        {
            $temp = $N / 2;
 
            // if 2*X jump is
            // better than X+1
            if ($temp * $P > $Q)
                $cost += $Q;
                 
            // if X+1 jump is better
            else
                $cost += $P * $temp;
 
            $N /= 2;
        }
    }
 
    // return cost
    return $cost;
}
 
// Driver Code
$N = 9; $P = 5; $Q = 1;
 
echo minCost($N, $P, $Q);
 
// This code is contributed
// by Akanksha Rai
?>


Javascript


输出:
13

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程