在执行给定操作后,使用长度为 K 的跳转来最小化到达给定二进制数组末尾的成本
给定一个包含N个整数和一个整数P的二进制数组arr[] ,任务是使用长度为K的跳转找到从第 P个索引到达数组末尾的最小成本。如果arr[i] = 1跳转到索引i是有效的。可以使用以下操作修改数组:
- 将值为0的索引替换为1 。此操作的成本为X 。
- 删除索引P处的整数。此操作的成本为Y 。
例子:
Input: arr[] = {0, 0, 0, 1, 1, 0, 0, 1, 1, 1}, P = 6, K = 2, X = 1, Y = 2
Output: 1
Explanation: In 1st operation, replace the value at index 6 to 1. Hence, arr[] = {0, 0, 0, 1, 1, 1, 0, 1, 1, 1}. Initially, the current index = P = 6. Jump from P to P + K => from 6 => 8. Again jump to the next possible index i.e, 8 => 10, which is the end of the array.
Input: arr[] = {0, 1, 0, 0, 0, 1, 0}, P = 4, K = 1, X = 2, Y = 1
Output: 4
方法:可以根据以下观察解决给定的问题:
- 对于给定的P ,检查索引P 、 P+K 、 P+2K...的值是否为 1。如果不是,则将它们替换为1并保持它们的计数。因此,最终成本= count * X。
- 在应用第二次操作i次后,起始索引将变为P+i 。因此最终成本 = (i * Y) + (count * X) 。
因此,给定的问题可以通过迭代[1, NP)范围内i的所有可能值并计算每一步的成本来解决。这可以通过维护一个数组zeroes[]来完成,其中zeroes[i]存储索引i、i+K、i+2K处0 的频率……
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum cost to
// reach the end of the given array
int minimumCost(int arr[], int N,
int P, int K, int X,
int Y)
{
// Convert P to 0-based indexing
P = P - 1;
// Vector to store the count of zeros
// till the current index
vector zeros(N, 0);
// Traverse the array and store the
// count of zeros in vector
for (int i = 0; i < N; i++) {
// If element is 0
if (arr[i] == 0) {
zeros[i]++;
}
}
// Iterate in the range [N-K-1, 0]
// and increment zeros[i] by zeros[i+K]
for (int i = N - K - 1; i >= 0; i--) {
zeros[i] += zeros[i + K];
}
// Variable to store the min cost
int cost = INT_MAX;
// Loop to calculate the cost for all
// values of i in range [P, N]
for (int i = P; i < N; i++) {
cost = min(cost,
((i - P) * Y)
+ (zeros[i] * X));
}
// Return Answer
return cost;
}
// Driver Code
int main()
{
int arr[] = { 0, 1, 0, 0, 0, 1, 0 };
int P = 4;
int K = 1;
int X = 2;
int Y = 1;
int N = sizeof(arr) / sizeof(arr[0]);
cout << minimumCost(arr, N, P, K, X, Y);
return 0;
}
Java
// Java program for the above approach
public class GFG {
// Function to find the minimum cost to
// reach the end of the given array
static int minimumCost(int arr[], int N,
int P, int K, int X,
int Y)
{
// Convert P to 0-based indexing
P = P - 1;
// Vector to store the count of zeros
// till the current index
int zeros[] = new int[N] ;
// Traverse the array and store the
// count of zeros in vector
for (int i = 0; i < N; i++) {
// If element is 0
if (arr[i] == 0) {
zeros[i]++;
}
}
// Iterate in the range [N-K-1, 0]
// and increment zeros[i] by zeros[i+K]
for (int i = N - K - 1; i >= 0; i--) {
zeros[i] += zeros[i + K];
}
// Variable to store the min cost
int cost = Integer.MAX_VALUE;
// Loop to calculate the cost for all
// values of i in range [P, N]
for (int i = P; i < N; i++) {
cost = Math.min(cost,
((i - P) * Y)
+ (zeros[i] * X));
}
// Return Answer
return cost;
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 0, 1, 0, 0, 0, 1, 0 };
int P = 4;
int K = 1;
int X = 2;
int Y = 1;
int N = arr.length;
System.out.println(minimumCost(arr, N, P, K, X, Y));
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
import sys
# Function to find the minimum cost to
# reach the end of the given array
def minimumCost(arr, N, P, K, X, Y) :
# Convert P to 0-based indexing
P = P - 1;
# Vector to store the count of zeros
# till the current index
zeros = [0] * N ;
# Traverse the array and store the
# count of zeros in vector
for i in range(N) :
# If element is 0
if (arr[i] == 0) :
zeros[i] += 1;
# Iterate in the range [N-K-1, 0]
# and increment zeros[i] by zeros[i+K]
for i in range( N - K - 1, -1, -1) :
zeros[i] += zeros[i + K];
# Variable to store the min cost
cost = sys.maxsize;
# Loop to calculate the cost for all
# values of i in range [P, N]
for i in range(P, N) :
cost = min(cost,((i - P) * Y) + (zeros[i] * X));
# Return Answer
return cost;
# Driver Code
if __name__ == "__main__" :
arr = [ 0, 1, 0, 0, 0, 1, 0 ];
P = 4;
K = 1;
X = 2;
Y = 1;
N = len(arr);
print(minimumCost(arr, N, P, K, X, Y));
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the minimum cost to
// reach the end of the given array
static int minimumCost(int[] arr, int N, int P, int K, int X, int Y)
{
// Convert P to 0-based indexing
P = P - 1;
// Vector to store the count of zeros
// till the current index
int[] zeros = new int[N];
// Traverse the array and store the
// count of zeros in vector
for (int i = 0; i < N; i++)
{
// If element is 0
if (arr[i] == 0)
{
zeros[i]++;
}
}
// Iterate in the range [N-K-1, 0]
// and increment zeros[i] by zeros[i+K]
for (int i = N - K - 1; i >= 0; i--)
{
zeros[i] += zeros[i + K];
}
// Variable to store the min cost
int cost = int.MaxValue;
// Loop to calculate the cost for all
// values of i in range [P, N]
for (int i = P; i < N; i++)
{
cost = Math.Min(cost,
((i - P) * Y)
+ (zeros[i] * X));
}
// Return Answer
return cost;
}
// Driver Code
public static void Main()
{
int[] arr = { 0, 1, 0, 0, 0, 1, 0 };
int P = 4;
int K = 1;
int X = 2;
int Y = 1;
int N = arr.Length;
Console.WriteLine(minimumCost(arr, N, P, K, X, Y));
}
}
// This code is contributed by _Saurabh_Jaiswal
Javascript
4
时间复杂度: O(N)
辅助空间: O(N)