最小化修改数组的成本,使偶数索引具有偶数元素,反之亦然
给定一个大小为N的数组arr[]和两个整数X和Y ,任务是找到修改数组所需的最小成本,使得偶数索引具有 偶数元素和奇数索引上有奇数元素,可以通过交换成本为X的数组的两个元素,或者通过从成本为Y的元素递增或递减1来实现。
例子:
Input: arr[] = {5, 3, 7, 2, 1}, X = 1, Y = 2
Output: 5
Explanation:
Following are the operations performed:
Operation 1: Swap the array elements at indices 0 and 3, modifies the array to {2, 3, 7, 5, 1}. The cost of this operations is X(= 1).
Operation 2: Incrementing the array elements at index 2, modifies the array to {2, 3, 8, 5, 1}. The cost of this operations is Y(= 2).
Operation 3: Incrementing the array elements at index 4, modifies the array to {2, 3, 8, 5, 2}. The cost of this operations is Y(= 2).
Therefore, the total cost is 1 + 2 + 2 = 5, which is minimum.
Input: arr[] = {1, 2, 3, 4}, X = 1, Y = 2
Output: 2
方法:给定的问题可以通过使用以下观察来解决,首先找到错误放置在奇数和偶数索引处的元素计数分别为oddCount和evenCount :
- 情况 1:可以通过以X的代价对最小的奇数计数和偶数计数执行交换操作并以Y的代价对剩余元素执行递减操作来计算成本。
- 情况 2:可以通过对剩余元素执行递减操作来计算成本,成本为Y。
以上两种情况下得到的最小成本就是要求的结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum cost to
// modify the array according to the
// given criteria
int minimumCost(int arr[], int N,
int X, int Y)
{
// Count of wrong positioned odd
// and even elements
int even_count = 0, odd_count = 0;
for (int i = 0; i < N; i++) {
// Odd Count
if ((arr[i] & 1)
&& (i % 2 == 0)) {
odd_count++;
}
// Even Count
if ((arr[i] % 2) == 0
&& (i & 1)) {
even_count++;
}
}
// Cost for Case 1
// Swapping Cost
int cost1 = X * min(odd_count, even_count);
// Decrementing cost after swapping
int cost2 = Y
* (max(odd_count, even_count)
- min(odd_count, even_count));
// Cost for Case 2
// Only decrementing cost
int cost3 = (odd_count + even_count) * Y;
// Return the minimum cost of the
// two cases
return min(cost1 + cost2, cost3);
}
// Driver Code
int main()
{
int arr[] = { 5, 3, 7, 2, 1 }, X = 10, Y = 2;
int N = sizeof(arr) / sizeof(arr[0]);
cout << minimumCost(arr, N, X, Y);
return 0;
}
Java
// Java program for the above approach
class GFG {
// Function to find the minimum cost to
// modify the array according to the
// given criteria
public static int minimumCost(int arr[], int N, int X, int Y)
{
// Count of wrong positioned odd
// and even elements
int even_count = 0, odd_count = 0;
for (int i = 0; i < N; i++) {
// Odd Count
if ((arr[i] & 1) > 0 && (i % 2 == 0)) {
odd_count++;
}
// Even Count
if ((arr[i] % 2) == 0 && (i & 1) > 0) {
even_count++;
}
}
// Cost for Case 1
// Swapping Cost
int cost1 = X * Math.min(odd_count, even_count);
// Decrementing cost after swapping
int cost2 = Y * (Math.max(odd_count, even_count) - Math.min(odd_count, even_count));
// Cost for Case 2
// Only decrementing cost
int cost3 = (odd_count + even_count) * Y;
// Return the minimum cost of the
// two cases
return Math.min(cost1 + cost2, cost3);
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 5, 3, 7, 2, 1 }, X = 10, Y = 2;
int N = arr.length;
System.out.println(minimumCost(arr, N, X, Y));
}
}
// This code is contributed by gfgking.
Python3
# python program for the above approach
# Function to find the minimum cost to
# modify the array according to the
# given criteria
def minimumCost(arr, N, X, Y):
# Count of wrong positioned odd
# and even elements
even_count = 0
odd_count = 0
for i in range(0, N):
# Odd Count
if ((arr[i] & 1) and (i % 2 == 0)):
odd_count += 1
# Even Count
if ((arr[i] % 2) == 0 and (i & 1)):
even_count += 1
# Cost for Case 1
# Swapping Cost
cost1 = X * min(odd_count, even_count)
# Decrementing cost after swapping
cost2 = Y * (max(odd_count, even_count) - min(odd_count, even_count))
# Cost for Case 2
# Only decrementing cost
cost3 = (odd_count + even_count) * Y
# Return the minimum cost of the
# two cases
return min(cost1 + cost2, cost3)
# Driver Code
if __name__ == "__main__":
arr = [5, 3, 7, 2, 1]
X = 10
Y = 2
N = len(arr)
print(minimumCost(arr, N, X, Y))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
public class GFG {
// Function to find the minimum cost to
// modify the array according to the
// given criteria
public static int minimumCost(int []arr, int N, int X, int Y)
{
// Count of wrong positioned odd
// and even elements
int even_count = 0, odd_count = 0;
for (int i = 0; i < N; i++) {
// Odd Count
if ((arr[i] & 1) > 0 && (i % 2 == 0)) {
odd_count++;
}
// Even Count
if ((arr[i] % 2) == 0 && (i & 1) > 0) {
even_count++;
}
}
// Cost for Case 1
// Swapping Cost
int cost1 = X * Math.Min(odd_count, even_count);
// Decrementing cost after swapping
int cost2 = Y * (Math.Max(odd_count, even_count) - Math.Min(odd_count, even_count));
// Cost for Case 2
// Only decrementing cost
int cost3 = (odd_count + even_count) * Y;
// Return the minimum cost of the
// two cases
return Math.Min(cost1 + cost2, cost3);
}
// Driver Code
public static void Main(string []args)
{
int []arr= { 5, 3, 7, 2, 1 };
int X = 10, Y = 2;
int N = arr.Length;
Console.WriteLine(minimumCost(arr, N, X, Y));
}
}
// This code is contributed by rutvik_56.
Javascript
8
时间复杂度: O(N)
辅助空间: O(1)